diff --git a/app/controllers/org_admin/template_options_controller.rb b/app/controllers/org_admin/template_options_controller.rb deleted file mode 100644 index 5d0260b..0000000 --- a/app/controllers/org_admin/template_options_controller.rb +++ /dev/null @@ -1,67 +0,0 @@ -# frozen_string_literal: true - -class OrgAdmin::TemplateOptionsController < ApplicationController - - after_action :verify_authorized - - # GET /org_admin/template_options (AJAX) - # Collect all of the templates available for the org+funder combination - def index - org_id = (plan_params[:org_id] == "-1" ? "" : plan_params[:org_id]) - funder_id = (plan_params[:funder_id] == "-1" ? "" : plan_params[:funder_id]) - authorize Template.new, :template_options? - @templates = [] - - if org_id.present? || funder_id.present? - unless funder_id.blank? - # Load the funder's template(s) minus the default template (that gets swapped - # in below if NO other templates are available) - @templates = Template.latest_customizable - .where(org_id: funder_id, is_default: false) - unless org_id.blank? - # Swap out any organisational cusotmizations of a funder template - @templates = @templates.map do |tmplt| - customization = Template.published - .latest_customized_version(tmplt.family_id, - org_id).first - # Only provide the customized version if its still up to date with the - # funder template! - if customization.present? && !customization.upgrade_customization? - customization - else - tmplt - end - end - end - end - - # If the no funder was specified OR the funder matches the org - if funder_id.blank? || funder_id == org_id - # Retrieve the Org's templates - @templates << Template.published - .organisationally_visible - .where(org_id: org_id, customization_of: nil).to_a - end - @templates = @templates.flatten.uniq - end - - # If no templates were available use the default template - if @templates.empty? - if Template.default.present? - customization = Template.published - .latest_customized_version(Template.default.family_id, - org_id).first - - @templates << (customization.present? ? customization : Template.default) - end - end - @templates = @templates.sort_by(&:title) - end - - private - - def plan_params - params.require(:plan).permit(:org_id, :funder_id) - end - -end diff --git a/app/controllers/template_options_controller.rb b/app/controllers/template_options_controller.rb new file mode 100644 index 0000000..3132971 --- /dev/null +++ b/app/controllers/template_options_controller.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +class TemplateOptionsController < ApplicationController + + after_action :verify_authorized + + # GET /template_options (AJAX) + # Collect all of the templates available for the org+funder combination + def index + org_id = (plan_params[:org_id] == "-1" ? "" : plan_params[:org_id]) + funder_id = (plan_params[:funder_id] == "-1" ? "" : plan_params[:funder_id]) + authorize Template.new, :template_options? + @templates = [] + + if org_id.present? || funder_id.present? + unless funder_id.blank? + # Load the funder's template(s) minus the default template (that gets swapped + # in below if NO other templates are available) + @templates = Template.latest_customizable + .where(org_id: funder_id, is_default: false) + unless org_id.blank? + # Swap out any organisational cusotmizations of a funder template + @templates = @templates.map do |tmplt| + customization = Template.published + .latest_customized_version(tmplt.family_id, + org_id).first + # Only provide the customized version if its still up to date with the + # funder template! + if customization.present? && !customization.upgrade_customization? + customization + else + tmplt + end + end + end + end + + # If the no funder was specified OR the funder matches the org + if funder_id.blank? || funder_id == org_id + # Retrieve the Org's templates + @templates << Template.published + .organisationally_visible + .where(org_id: org_id, customization_of: nil).to_a + end + @templates = @templates.flatten.uniq + end + + # If no templates were available use the default template + if @templates.empty? + if Template.default.present? + customization = Template.published + .latest_customized_version(Template.default.family_id, + org_id).first + + @templates << (customization.present? ? customization : Template.default) + end + end + @templates = @templates.sort_by(&:title) + end + + private + + def plan_params + params.require(:plan).permit(:org_id, :funder_id) + end + +end diff --git a/app/views/org_admin/template_options/index.json.jbuilder b/app/views/org_admin/template_options/index.json.jbuilder deleted file mode 100644 index e7faf66..0000000 --- a/app/views/org_admin/template_options/index.json.jbuilder +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -json.templates do - json.array! @templates do |template| - json.id template.id - json.title template.title - end -end diff --git a/app/views/plans/new.html.erb b/app/views/plans/new.html.erb index 42f96f9..820f7af 100644 --- a/app/views/plans/new.html.erb +++ b/app/views/plans/new.html.erb @@ -86,7 +86,7 @@
- <%= hidden_field_tag 'template-option-target', org_admin_template_options_path %> + <%= hidden_field_tag 'template-option-target', template_options_path %>

<%= _('Which DMP template would you like to use?') %>

@@ -94,7 +94,9 @@ class: 'form-control', 'aria-describedby': 'template-selection') %>
- <%= _('We found multiple DMP templates corresponding to your funder.') %> + + <%= _('We found multiple DMP templates corresponding to your funder.') %> +
diff --git a/app/views/template_options/index.json.jbuilder b/app/views/template_options/index.json.jbuilder new file mode 100644 index 0000000..e7faf66 --- /dev/null +++ b/app/views/template_options/index.json.jbuilder @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +json.templates do + json.array! @templates do |template| + json.id template.id + json.title template.title + end +end diff --git a/config/routes.rb b/config/routes.rb index ee50470..5671499 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -188,6 +188,8 @@ end end + resources :template_options, only: [:index], constraints: { format: /json/ } + # ORG ADMIN specific pages namespace :org_admin do resources :plans, only: [:index] do @@ -196,8 +198,6 @@ end end - resources :template_options, only: [:index], constraints: { format: /json/ } - resources :templates do resources :customizations, only: [:create], controller: "template_customizations"