diff --git a/app/controllers/guidance_groups_controller.rb b/app/controllers/guidance_groups_controller.rb index 585f32c..9d7706f 100644 --- a/app/controllers/guidance_groups_controller.rb +++ b/app/controllers/guidance_groups_controller.rb @@ -9,11 +9,11 @@ end - # GET add new guidance groups - def admin_new + # GET add new guidance groups + def admin_new @guidance_group = GuidanceGroup.new authorize @guidance_group - end + end # POST /guidance_groups @@ -36,16 +36,17 @@ # GET /guidance_groups/1/edit def admin_edit - @guidance_group = GuidanceGroup.find(params[:id]) - authorize @guidance_group + @guidance_group = GuidanceGroup.find(params[:id]) + authorize @guidance_group end # PUT /guidance_groups/1 def admin_update - @guidance_group = GuidanceGroup.find(params[:id]) + @guidance_group = GuidanceGroup.find(params[:id]) authorize @guidance_group @guidance_group.org_id = current_user.org_id + @guidance_group.published = true unless params[:save_publish].nil? if @guidance_group.update_attributes(params[:guidance_group]) redirect_to admin_index_guidance_path(params[:guidance_group]), notice: _('Guidance group was successfully updated.') @@ -57,7 +58,7 @@ # PUT /guidance_groups/1 def admin_update_publish - @guidance_group = GuidanceGroup.find(params[:id]) + @guidance_group = GuidanceGroup.find(params[:id]) authorize @guidance_group @guidance_group.org.id = current_user.org.id @guidance_group.published = true @@ -73,11 +74,11 @@ # DELETE /guidance_groups/1 # DELETE /guidance_groups/1.json def admin_destroy - @guidance_group = GuidanceGroup.find(params[:id]) + @guidance_group = GuidanceGroup.find(params[:id]) authorize @guidance_group @guidance_group.destroy redirect_to admin_index_guidance_path, notice: _('Guidance group was successfully deleted.') - end + end end \ No newline at end of file diff --git a/app/controllers/guidances_controller.rb b/app/controllers/guidances_controller.rb index 674e0f6..123b334 100644 --- a/app/controllers/guidances_controller.rb +++ b/app/controllers/guidances_controller.rb @@ -13,7 +13,7 @@ ## # GET /guidances/1 def admin_show - @guidance = Guidance.includes(:guidance_group, :question, :themes).find(params[:id]) + @guidance = Guidance.includes(:guidance_group, :themes).find(params[:id]) authorize @guidance end @@ -21,36 +21,7 @@ @guidance = Guidance.new authorize @guidance - #@templates = Template.funders_and_own_templates(current_user.org_id) - # Replacing weird accessor on Template - @templates = (Org.funders.collect{|o| o.templates } + current_user.org.templates).flatten - - @phases = nil - @templates.includes(:phases).each do |template| - if @phases.nil? then - @phases = template.phases.all.order('number') - else - @phases = @phases + template.phases.all.order('number') - end - end - @sections = nil - @phases.each do |phase| - if @sections.nil? then - @sections = phase.sections.all.order('number') - else - @sections = @sections + phase.sections.all.order('number') - end - end - @questions = nil - @sections.each do |section| - if @questions.nil? then - @questions = section.questions.all.order('number') - else - @questions = @questions + section.questions.all.order('number') - end - end - @themes = Theme.all.order('title') - @guidance_groups = GuidanceGroup.where(org_id: current_user.org_id).order('name ASC') + load_select_box_content end #setup variables for use in the dynamic updating @@ -96,19 +67,19 @@ def admin_edit @guidance = Guidance.includes(:themes, :guidance_group).find(params[:id]) authorize @guidance - @guidance_groups = GuidanceGroup.where(org_id: current_user.org_id).order('name ASC') - @themes = Theme.all.order('title') + + load_select_box_content end ## # POST /guidances def admin_create - @guidance = Guidance.new(params[:guidance]) + @guidance = Guidance.new(guidance_params) authorize @guidance @guidance.text = params["guidance-text"] @guidance.question_id = params["question_id"] if @guidance.published == true then - @gg = GuidanceGroup.find(@guidance.guidance_group_ids).first + @gg = GuidanceGroup.find(@guidance.guidance_group_id) if @gg.published == false || @gg.published.nil? then @gg.published = true @gg.save @@ -118,7 +89,8 @@ if @guidance.save redirect_to admin_show_guidance_path(@guidance), notice: _('Guidance was successfully created.') else - render action: "new" + load_select_box_content + render action: "admin_new" end end @@ -133,7 +105,8 @@ if @guidance.update_attributes(params[:guidance]) redirect_to admin_show_guidance_path(params[:guidance]), notice: _('Guidance was successfully updated.') else - render action: "edit" + load_select_box_content + render action: "admin_edit" end end @@ -147,4 +120,43 @@ redirect_to admin_index_guidance_path 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, :theme_ids, :published) + end + + def load_select_box_content + #@templates = Template.funders_and_own_templates(current_user.org_id) + # Replacing weird accessor on Template + @templates = (Org.funders.collect{|o| o.templates } + current_user.org.templates).flatten + + @phases = nil + @templates.each do |template| + if @phases.nil? then + @phases = template.phases.all.order('number') + else + @phases = @phases + template.phases.all.order('number') + end + end + @sections = nil + @phases.each do |phase| + if @sections.nil? then + @sections = phase.sections.all.order('number') + else + @sections = @sections + phase.sections.all.order('number') + end + end + @questions = nil + @sections.each do |section| + if @questions.nil? then + @questions = section.questions.all.order('number') + else + @questions = @questions + section.questions.all.order('number') + end + end + @themes = Theme.all.order('title') + @guidance_groups = GuidanceGroup.where(org_id: current_user.org_id).order('name ASC') + end end \ No newline at end of file diff --git a/lib/assets/javascripts/admin.js b/lib/assets/javascripts/admin.js index f3d3aaf..3e0e27d 100644 --- a/lib/assets/javascripts/admin.js +++ b/lib/assets/javascripts/admin.js @@ -336,7 +336,7 @@ } } //verify if guidance group is selected - if ( ($('#guidance_guidance_group_ids').val() == '') || $('#guidance_guidance_group_ids').val() == undefined ) { + if ( ($('#guidance_guidance_group_id').val() == '') || $('#guidance_guidance_group_id').val() == undefined ) { alert_message.push(I18n.t("js.select_guidance_group")); } if(alert_message.length == 0){