diff --git a/app/controllers/guidances_controller.rb b/app/controllers/guidances_controller.rb index 123b334..5387dc9 100644 --- a/app/controllers/guidances_controller.rb +++ b/app/controllers/guidances_controller.rb @@ -13,62 +13,24 @@ ## # GET /guidances/1 def admin_show - @guidance = Guidance.includes(:guidance_group, :themes).find(params[:id]) + @guidance = Guidance.eager_load(:guidance_group, :themes).find(params[:id]) authorize @guidance end def admin_new @guidance = Guidance.new authorize @guidance - - load_select_box_content + @themes = Theme.all.order('title') + @guidance_groups = GuidanceGroup.where(org_id: current_user.org_id).order('name ASC') end - #setup variables for use in the dynamic updating - def update_phases - authorize Guidance - # updates phases, versions, sections and questions based on template selected - dmptemplate = Template.find(params[:dmptemplate_id]) - # map to title and id for use in our options_for_select - @phases = dmptemplate.phases.map{|a| [a.title, a.id]}.insert(0, _('Select a phase')) - @versions = dmptemplate.versions.map{|s| [s.title, s.id]}.insert(0, _('Select a version')) - @sections = dmptemplate.sections.map{|s| [s.title, s.id]}.insert(0, _('Select a section')) - @questions = dmptemplate.questions.map{|s| [s.text, s.id]}.insert(0, _('Select a question')) - end - - def update_versions - authorize Guidance - # updates versions, sections and questions based on phase selected - phase = Phase.find(params[:phase_id]) - # map to name and id for use in our options_for_select - @versions = phase.versions.map{|s| [s.title, s.id]}.insert(0, _('Select a version')) - @sections = phase.sections.map{|s| [s.title, s.id]}.insert(0, _('Select a section')) - @questions = phase.questions.map{|s| [s.text, s.id]}.insert(0, _('Select a question')) - end - - def update_sections - authorize Guidance - # updates sections and questions based on version selected - version = Version.find(params[:version_id]) - # map to name and id for use in our options_for_select - @sections = version.sections.map{|s| [s.title, s.id]}.insert(0, _('Select a section')) - @questions = version.questions.map{|s| [s.text, s.id]}.insert(0, _('Select a question')) - end - - def update_questions - authorize Guidance - # updates songs based on artist selected - section = Section.find(params[:section_id]) - @questions = section.questions.map{|s| [s.text, s.id]}.insert(0, _('Select a question')) - end - ## # GET /guidances/1/edit def admin_edit - @guidance = Guidance.includes(:themes, :guidance_group).find(params[:id]) + @guidance = Guidance.eager_load(:themes, :guidance_group).find(params[:id]) authorize @guidance - - load_select_box_content + @themes = Theme.all.order('title') + @guidance_groups = GuidanceGroup.where(org_id: current_user.org_id).order('name ASC') end ## @@ -78,6 +40,10 @@ authorize @guidance @guidance.text = params["guidance-text"] @guidance.question_id = params["question_id"] + + @guidance.themes = [] + 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 @@ -89,7 +55,8 @@ if @guidance.save redirect_to admin_show_guidance_path(@guidance), notice: _('Guidance was successfully created.') else - load_select_box_content + @themes = Theme.all.order('title') + @guidance_groups = GuidanceGroup.where(org_id: current_user.org_id).order('name ASC') render action: "admin_new" end end @@ -101,11 +68,15 @@ authorize @guidance @guidance.text = params["guidance-text"] @guidance.question_id = params["question_id"] - - if @guidance.update_attributes(params[:guidance]) - redirect_to admin_show_guidance_path(params[:guidance]), notice: _('Guidance was successfully updated.') + + @guidance.themes = [] + guidance_params[:theme_ids].map{|t| @guidance.themes << Theme.find(t.to_i) unless t.empty? } + + if @guidance.update_attributes(guidance_params) + redirect_to admin_show_guidance_path(guidance_params), notice: _('Guidance was successfully updated.') else - load_select_box_content + @themes = Theme.all.order('title') + @guidance_groups = GuidanceGroup.where(org_id: current_user.org_id).order('name ASC') render action: "admin_edit" end end @@ -124,39 +95,6 @@ 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') + params.require(:guidance).permit(:guidance_group_id, :published, theme_ids: []) end end \ No newline at end of file diff --git a/app/models/guidance.rb b/app/models/guidance.rb index 26a83ff..00b912c 100644 --- a/app/models/guidance.rb +++ b/app/models/guidance.rb @@ -16,7 +16,7 @@ # belongs_to :question has_and_belongs_to_many :themes, join_table: "themes_in_guidance" # depricated, but required for migration "single_group_for_guidance" - has_and_belongs_to_many :guidance_groups, join_table: "guidance_in_group" +# has_and_belongs_to_many :guidance_groups, join_table: "guidance_in_group" diff --git a/app/views/guidances/_add_guidance.html.erb b/app/views/guidances/_add_guidance.html.erb index 19579e5..28f75f6 100644 --- a/app/views/guidances/_add_guidance.html.erb +++ b/app/views/guidances/_add_guidance.html.erb @@ -17,15 +17,6 @@ <%= _('Should this guidance apply:') %> -
- <%= select_tag "g_options", options_for_select([[_('by themes'), 1], - [_('by question'), 2]]) %> -
-
- <%= link_to( image_tag("help_button.png"), "#", class: "guidance_apply_to_popover", rel: "popover", "data-html" => "true", "data-content" => _('Decide whether your guidance should display by themes (default) or if it only pertains to a specific question in one of the funder templates.'))%> -
-
- diff --git a/app/views/guidances/admin_edit.html.erb b/app/views/guidances/admin_edit.html.erb index 5851286..565f5d4 100644 --- a/app/views/guidances/admin_edit.html.erb +++ b/app/views/guidances/admin_edit.html.erb @@ -34,24 +34,9 @@ - <%= _('Should this guidance apply:') %> -
- <% if !@guidance.question_id.nil? then %> - <% select_op = 2 %> - <% else %> - <% select_op = 1%> - <% end %> - <%= hidden_field 'select_op', value: select_op, id: 'edit_guid_ques_flag' %> - - <%= select_tag "g_options", options_for_select({_('by themes') => 1, - _('by question') => 2}, select_op) %> -
-
- <%= link_to( image_tag('help_button.png'), '#', class: 'guidance_apply_to_popover', rel: "popover", 'data-html' => "true", 'data-content' => _('Decide whether your guidance should display by themes (default) or if it only pertains to a specific question in one of the funder templates.'))%> -
-
- -