diff --git a/app/controllers/org_admin/template_options_controller.rb b/app/controllers/org_admin/template_options_controller.rb new file mode 100644 index 0000000..5d0260b --- /dev/null +++ b/app/controllers/org_admin/template_options_controller.rb @@ -0,0 +1,67 @@ +# 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/org_admin/templates_controller.rb b/app/controllers/org_admin/templates_controller.rb index 3a9e399..eb83004 100644 --- a/app/controllers/org_admin/templates_controller.rb +++ b/app/controllers/org_admin/templates_controller.rb @@ -278,67 +278,8 @@ redirect_to request.referrer.present? ? request.referrer : org_admin_templates_path end - # GET /org_admin/template_options (AJAX) - # Collect all of the templates available for the org+funder combination - def template_options - 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 - 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).select { |t| !t.is_default? } - 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? - default = Template.default - if default.present? - customization = Template.published.latest_customized_version(default.family_id, - org_id).first - templates << (customization.present? ? customization : default) - end - end - render json: { - templates: templates.sort(&:title).collect { |t| { id: t.id, title: t.title } } - } - end - private - def plan_params - params.require(:plan).permit(:org_id, :funder_id) - end - def template_params params.require(:template).permit(:title, :description, :visibility, :links) end diff --git a/app/views/org_admin/template_options/index.json.jbuilder b/app/views/org_admin/template_options/index.json.jbuilder new file mode 100644 index 0000000..e7faf66 --- /dev/null +++ b/app/views/org_admin/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 acab4da..ee50470 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,11 +1,12 @@ Rails.application.routes.draw do - devise_for :users, controllers: { - registrations: "registrations", - passwords: 'passwords', - sessions: 'sessions', - omniauth_callbacks: 'users/omniauth_callbacks', - invitations: 'users/invitations' } do + devise_for( :users, controllers: { + registrations: "registrations", + passwords: 'passwords', + sessions: 'sessions', + omniauth_callbacks: 'users/omniauth_callbacks', + invitations: 'users/invitations' + }) do get "/users/sign_out", :to => "devise/sessions#destroy" end @@ -42,205 +43,207 @@ patch 'locale/:locale' => 'session_locales#update', as: 'locale' root :to => 'home#index' - get "about_us" => 'static_pages#about_us' - get "help" => 'static_pages#help' - get "roadmap" => 'static_pages#roadmap' - get "terms" => 'static_pages#termsuse' - get "privacy" => 'static_pages#privacy' - get "public_plans" => 'public_pages#plan_index' - get "public_templates" => 'public_pages#template_index' - get "template_export/:id" => 'public_pages#template_export', as: 'template_export' - get "plan_export/:id" => 'public_pages#plan_export', as: 'plan_export' + get "about_us" => 'static_pages#about_us' + get "help" => 'static_pages#help' + get "roadmap" => 'static_pages#roadmap' + get "terms" => 'static_pages#termsuse' + get "privacy" => 'static_pages#privacy' + get "public_plans" => 'public_pages#plan_index' + get "public_templates" => 'public_pages#template_index' + get "template_export/:id" => 'public_pages#template_export', as: 'template_export' + get "plan_export/:id" => 'public_pages#plan_export', as: 'plan_export' - #post 'contact_form' => 'contacts', as: 'localized_contact_creation' - #get 'contact_form' => 'contacts#new', as: 'localized_contact_form' + #post 'contact_form' => 'contacts', as: 'localized_contact_creation' + #get 'contact_form' => 'contacts#new', as: 'localized_contact_form' - resources :orgs, :path => 'org/admin', only: [] do - member do - get 'admin_edit' - put 'admin_update' - end + resources :orgs, :path => 'org/admin', only: [] do + member do + get 'admin_edit' + put 'admin_update' end + end - resources :guidances, :path => 'org/admin/guidance', only: [] do - member do - get 'admin_index' - get 'admin_edit' - get 'admin_new' - delete 'admin_destroy' - post 'admin_create' - put 'admin_update' - put 'admin_publish' - put 'admin_unpublish' - end + resources :guidances, :path => 'org/admin/guidance', only: [] do + member do + get 'admin_index' + get 'admin_edit' + get 'admin_new' + delete 'admin_destroy' + post 'admin_create' + put 'admin_update' + put 'admin_publish' + put 'admin_unpublish' end + end - resources :guidance_groups, :path => 'org/admin/guidancegroup', only: [] do - member do - get 'admin_show' - get 'admin_new' - get 'admin_edit' - delete 'admin_destroy' - post 'admin_create' - put 'admin_update' - put 'admin_update_publish' - put 'admin_update_unpublish' - end + resources :guidance_groups, :path => 'org/admin/guidancegroup', only: [] do + member do + get 'admin_show' + get 'admin_new' + get 'admin_edit' + delete 'admin_destroy' + post 'admin_create' + put 'admin_update' + put 'admin_update_publish' + put 'admin_update_unpublish' end + end - resources :answers, only: [] do - post 'create_or_update', on: :collection + resources :answers, only: [] do + post 'create_or_update', on: :collection + end + + # Question Formats controller, currently just the one action + get 'question_formats/rda_api_address' => 'question_formats#rda_api_address' + + resources :notes, only: [:create, :update, :archive] do + member do + patch 'archive' end + end - # Question Formats controller, currently just the one action - get 'question_formats/rda_api_address' => 'question_formats#rda_api_address' + resources :feedback_requests, only: [:create] - resources :notes, only: [:create, :update, :archive] do - member do - patch 'archive' - end + resources :plans do + member do + get 'answer' + get 'share' + get 'download' + post 'duplicate' + get 'export' + post 'visibility', constraints: {format: [:json]} + post 'set_test', constraints: {format: [:json]} + get 'overview' end + end - resources :feedback_requests, only: [:create] + resources :usage, only: [:index] - resources :plans do - member do - get 'answer' - get 'share' - get 'download' - post 'duplicate' - get 'export' - post 'visibility', constraints: {format: [:json]} - post 'set_test', constraints: {format: [:json]} - get 'overview' - end + resources :roles, only: [:create, :update, :destroy] do + member do + put :deactivate end + end - resources :usage, only: [:index] + namespace :settings do + resources :plans, only: [:update] + end - resources :roles, only: [:create, :update, :destroy] do - member do - put :deactivate - end - end - - namespace :settings do - resources :plans, only: [:update] - end - - namespace :api, defaults: {format: :json} do - namespace :v0 do - resources :guidances, only: [:index], controller: 'guidance_groups', path: 'guidances' - resources :plans, only: :create - resources :templates, only: :index - resource :statistics, only: [], controller: "statistics", path: "statistics" do - member do - get :users_joined - get :completed_plans - get :created_plans - get :using_template - get :plans_by_template - get :plans - end - end - end - end - - namespace :paginable do - resources :orgs, only: [] do - get 'index/:page', action: :index, on: :collection, as: :index - end - # Paginable actions for plans - resources :plans, only: [] do - get 'privately_visible/:page', action: :privately_visible, on: :collection, as: :privately_visible - get 'organisationally_or_publicly_visible/:page', action: :organisationally_or_publicly_visible, on: :collection, as: :organisationally_or_publicly_visible - get 'publicly_visible/:page', action: :publicly_visible, on: :collection, as: :publicly_visible - get 'org_admin/:page', action: :org_admin, on: :collection, as: :org_admin - end - # Paginable actions for users - resources :users, only: [] do - get 'index/:page', action: :index, on: :collection, as: :index - end - # Paginable actions for themes - resources :themes, only: [] do - get 'index/:page', action: :index, on: :collection, as: :index - end - # Paginable actions for notifications - resources :notifications, only: [] do - get 'index/:page', action: :index, on: :collection, as: :index - end - # Paginable actions for templates - resources :templates, only: [] do - get 'index/:page', action: :index, on: :collection, as: :index - get 'customisable/:page', action: :customisable, on: :collection, as: :customisable - get 'organisational/:page', action: :organisational, on: :collection, as: :organisational - get 'publicly_visible/:page', action: :publicly_visible, on: :collection, as: :publicly_visible - get ':id/history/:page', action: :history, on: :collection, as: :history - end - # Paginable actions for guidances - resources :guidances, only: [] do - get 'index/:page', action: :index, on: :collection, as: :index - end - # Paginable actions for guidance_groups - resources :guidance_groups, only: [] do - get 'index/:page', action: :index, on: :collection, as: :index - end - end - - # ORG ADMIN specific pages - namespace :org_admin do - resources :plans, only: [:index] do + namespace :api, defaults: {format: :json} do + namespace :v0 do + resources :guidances, only: [:index], controller: 'guidance_groups', path: 'guidances' + resources :plans, only: :create + resources :templates, only: :index + resource :statistics, only: [], controller: "statistics", path: "statistics" do member do - get 'feedback_complete' + get :users_joined + get :completed_plans + get :created_plans + get :using_template + get :plans_by_template + get :plans end end - resources :templates do + end + end - resources :customizations, only: [:create], controller: "template_customizations" + namespace :paginable do + resources :orgs, only: [] do + get 'index/:page', action: :index, on: :collection, as: :index + end + # Paginable actions for plans + resources :plans, only: [] do + get 'privately_visible/:page', action: :privately_visible, on: :collection, as: :privately_visible + get 'organisationally_or_publicly_visible/:page', action: :organisationally_or_publicly_visible, on: :collection, as: :organisationally_or_publicly_visible + get 'publicly_visible/:page', action: :publicly_visible, on: :collection, as: :publicly_visible + get 'org_admin/:page', action: :org_admin, on: :collection, as: :org_admin + end + # Paginable actions for users + resources :users, only: [] do + get 'index/:page', action: :index, on: :collection, as: :index + end + # Paginable actions for themes + resources :themes, only: [] do + get 'index/:page', action: :index, on: :collection, as: :index + end + # Paginable actions for notifications + resources :notifications, only: [] do + get 'index/:page', action: :index, on: :collection, as: :index + end + # Paginable actions for templates + resources :templates, only: [] do + get 'index/:page', action: :index, on: :collection, as: :index + get 'customisable/:page', action: :customisable, on: :collection, as: :customisable + get 'organisational/:page', action: :organisational, on: :collection, as: :organisational + get 'publicly_visible/:page', action: :publicly_visible, on: :collection, as: :publicly_visible + get ':id/history/:page', action: :history, on: :collection, as: :history + end + # Paginable actions for guidances + resources :guidances, only: [] do + get 'index/:page', action: :index, on: :collection, as: :index + end + # Paginable actions for guidance_groups + resources :guidance_groups, only: [] do + get 'index/:page', action: :index, on: :collection, as: :index + end + end - resources :copies, only: [:create], - controller: "template_copies", - constraints: { format: [:json] } + # ORG ADMIN specific pages + namespace :org_admin do + resources :plans, only: [:index] do + member do + get 'feedback_complete' + end + end - resources :customization_transfers, only: [:create], - controller: "template_customization_transfers" + resources :template_options, only: [:index], constraints: { format: /json/ } + + resources :templates do + + resources :customizations, only: [:create], controller: "template_customizations" + + resources :copies, only: [:create], + controller: "template_copies", + constraints: { format: [:json] } + + resources :customization_transfers, only: [:create], + controller: "template_customization_transfers" + + member do + get 'history' + patch 'publish', action: :publish, constraints: {format: [:json]} + patch 'unpublish', action: :unpublish, constraints: {format: [:json]} + end + + # Used for the organisational and customizable views of index + collection do + get 'organisational' + get 'customisable' + end + + resources :phases, except: [:index] do + + resources :versions, only: [:create], controller: "phase_versions" member do - get 'history' - patch 'publish', action: :publish, constraints: {format: [:json]} - patch 'unpublish', action: :unpublish, constraints: {format: [:json]} + get 'preview' + post 'sort' end - # Used for the organisational and customizable views of index - collection do - get 'organisational' - get 'customisable' - end - - resources :phases, except: [:index] do - - resources :versions, only: [:create], controller: "phase_versions" - - member do - get 'preview' - post 'sort' - end - - resources :sections, only: [:index, :show, :edit, :update, :create, :destroy] do - resources :questions, only: [:show, :edit, :new, :update, :create, :destroy] do - end + resources :sections, only: [:index, :show, :edit, :update, :create, :destroy] do + resources :questions, only: [:show, :edit, :new, :update, :create, :destroy] do end end end - - get 'template_options' => 'templates#template_options', constraints: {format: [:json]} - get 'download_plans' => 'plans#download_plans' end - namespace :super_admin do - resources :orgs, only: [:index, :new, :create, :destroy] - resources :themes, only: [:index, :new, :create, :edit, :update, :destroy] - resources :users, only: [:edit, :update] - resources :notifications, except: [:show] - end + get 'download_plans' => 'plans#download_plans' + end + + namespace :super_admin do + resources :orgs, only: [:index, :new, :create, :destroy] + resources :themes, only: [:index, :new, :create, :edit, :update, :destroy] + resources :users, only: [:edit, :update] + resources :notifications, except: [:show] + end end