Newer
Older
dmpopidor / app / controllers / guidances_controller.rb
@briley briley on 5 Apr 2017 2 KB fixed rebase issues
class GuidancesController < ApplicationController
  after_action :verify_authorized
  respond_to :html

  ##
  # GET /guidances
  def admin_index
    authorize Guidance
    @guidances = policy_scope(Guidance)
    @guidance_groups = GuidanceGroup.where(org_id: current_user.org_id)
  end

  ##
  # GET /guidances/1
  def admin_show
    @guidance = Guidance.eager_load(:guidance_group, :themes).find(params[:id])
    authorize @guidance
  end

  def admin_new
    @guidance = Guidance.new
    authorize @guidance
    @themes = Theme.all.order('title')
    @guidance_groups = GuidanceGroup.where(org_id: current_user.org_id).order('name ASC')
	end

  ##
  # GET /guidances/1/edit
  def admin_edit
    @guidance = Guidance.eager_load(:themes, :guidance_group).find(params[:id])
    authorize @guidance
    @themes = Theme.all.order('title')
    @guidance_groups = GuidanceGroup.where(org_id: current_user.org_id).order('name ASC')
  end

  ##
  # POST /guidances
  def admin_create
    @guidance = Guidance.new(guidance_params)
    authorize @guidance
    @guidance.text = params["guidance-text"]
    @guidance.question_id = params["question_id"]
    
    @guidance.themes = []
    guidance_params[:theme_ids].map{|t| @guidance.themes << Theme.find(t.to_i) unless t.empty? }
    
    if @guidance.published == true then
      @gg = GuidanceGroup.find(@guidance.guidance_group_id)
      if @gg.published == false || @gg.published.nil? then
        @gg.published = true
        @gg.save
      end
    end

    if @guidance.save
      redirect_to admin_show_guidance_path(@guidance), notice: _('Guidance was successfully created.')
    else
      flash[:notice] = generate_error_notice(@guidance)
      @themes = Theme.all.order('title')
      @guidance_groups = GuidanceGroup.where(org_id: current_user.org_id).order('name ASC')
      render action: "admin_new"
    end
  end

  ##
  # PUT /guidances/1
  def admin_update
 		@guidance = Guidance.find(params[:id])
    authorize @guidance
		@guidance.text = params["guidance-text"]
		@guidance.question_id = params["question_id"]

    if @guidance.save(guidance_params)
      redirect_to admin_show_guidance_path(params[:guidance]), notice: _('Guidance was successfully updated.')
    else
      flash[:notice] = generate_error_notice(@guidance)
      @themes = Theme.all.order('title')
      @guidance_groups = GuidanceGroup.where(org_id: current_user.org_id).order('name ASC')

      render action: "admin_edit"
    end
  end

  ##
  # DELETE /guidances/1
  def admin_destroy
   	@guidance = Guidance.find(params[:id])
    authorize @guidance
    @guidance.destroy

    redirect_to admin_index_guidance_path, notice: _('Guidance was successfully deleted.')
	end


  private
    def guidance_params
      # The form on the page is weird. The text and template/section/question stuff is outside of the normal form params
      params.require(:guidance).permit(:guidance_group_id, :published, theme_ids: [])
    end
end