diff --git a/app/controllers/admin/categories_controller.rb b/app/controllers/admin/categories_controller.rb index 93ec162b4e..73e1599e94 100644 --- a/app/controllers/admin/categories_controller.rb +++ b/app/controllers/admin/categories_controller.rb @@ -4,6 +4,7 @@ class Admin::CategoriesController < AdminController include TranslatableParams + before_action :check_klass before_action :set_root, expect: [:destroy, :reorder] before_action :set_category, only: [:edit, :update, :destroy, :reorder] @@ -19,7 +20,7 @@ def create @category = Category.new(category_params) if @category.save flash[:notice] = 'Category was successfully created.' - redirect_to admin_categories_path + redirect_to admin_categories_path(model_type: current_klass) else @category.build_all_translations render action: 'new' @@ -33,7 +34,7 @@ def edit def update if @category.update(category_params) flash[:notice] = 'Category was successfully updated.' - redirect_to admin_categories_path + redirect_to admin_categories_path(model_type: current_klass) else @category.build_all_translations render action: 'edit' @@ -43,7 +44,7 @@ def update def destroy @category.destroy flash[:notice] = 'Category was successfully destroyed.' - redirect_to admin_categories_path + redirect_to admin_categories_path(model_type: current_klass) end def reorder @@ -82,10 +83,19 @@ def category_params end def set_root - @root = Category.public_body_root + @root = current_klass.category_root end def set_category @category = Category.find(params[:id]) end + + helper_method :current_klass + def current_klass + params.fetch(:model_type, 'PublicBody').safe_constantize + end + + def check_klass + raise RouteNotFound unless Categorisable.models.include?(current_klass) + end end diff --git a/app/models/category.rb b/app/models/category.rb index d175aa9640..3ba777ae85 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -49,10 +49,6 @@ class Category < ApplicationRecord joins(:parent_relationships).where(parent_relationships: { parent: parent }) end - def self.public_body_root - Category.roots.find_or_create_by(title: 'PublicBody') - end - def tree children.includes(:translations, children: [:translations]) end diff --git a/app/models/concerns/categorisable.rb b/app/models/concerns/categorisable.rb new file mode 100644 index 0000000000..95b9ac96d0 --- /dev/null +++ b/app/models/concerns/categorisable.rb @@ -0,0 +1,22 @@ +## +# Module concern with methods to help categorise records +# +module Categorisable + extend ActiveSupport::Concern + + def self.models + @models ||= [] + end + + included do + Categorisable.models << self + + def self.category_root + Category.roots.find_or_create_by(title: name) + end + + def self.categories + category_root.tree + end + end +end diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 59b63197a3..8f1ede4157 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -49,6 +49,7 @@ class InfoRequest < ApplicationRecord include InfoRequest::PublicToken include InfoRequest::Sluggable include InfoRequest::TitleValidation + include Categorisable include Taggable include Notable include LinkToHelper diff --git a/app/models/public_body.rb b/app/models/public_body.rb index 68d651d18f..151a9aeb96 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -33,6 +33,7 @@ class PublicBody < ApplicationRecord include CalculatedHomePage + include Categorisable include Taggable include Notable include Rails.application.routes.url_helpers diff --git a/app/views/admin/categories/_scope.html.erb b/app/views/admin/categories/_scope.html.erb new file mode 100644 index 0000000000..613859a2b3 --- /dev/null +++ b/app/views/admin/categories/_scope.html.erb @@ -0,0 +1,7 @@ + diff --git a/app/views/admin/categories/edit.html.erb b/app/views/admin/categories/edit.html.erb index a7c46098c8..a4254d1925 100644 --- a/app/views/admin/categories/edit.html.erb +++ b/app/views/admin/categories/edit.html.erb @@ -5,13 +5,15 @@
- <%= form_for @category, url: admin_category_path(@category), html: { class: "form form-horizontal" } do |f| %> + <%= form_for @category, url: admin_category_path(@category, model_type: current_klass), html: { class: "form form-horizontal" } do |f| %> <%= render partial: 'form', locals: { f: f } %>
<%= f.submit 'Save', accesskey: 's', class: 'btn btn-success' %> - <%= link_to 'Public page', list_public_bodies_by_tag_path(@category.category_tag), class: 'btn' if @category.category_tag %> - <%= link_to 'List all', admin_categories_path, class: 'btn' %> + <% if current_klass == PublicBody && @category.category_tag %> + <%= link_to 'Public page', list_public_bodies_by_tag_path(@category.category_tag), class: 'btn' %> + <% end %> + <%= link_to 'List all', admin_categories_path(model_type: current_klass), class: 'btn' %>
<% end %>
diff --git a/app/views/admin/categories/index.html.erb b/app/views/admin/categories/index.html.erb index a082bf5ee1..ef26fed394 100644 --- a/app/views/admin/categories/index.html.erb +++ b/app/views/admin/categories/index.html.erb @@ -1,10 +1,12 @@ -<% @title = 'Listing public authority categories' %> +<% @title = "Listing #{current_klass.admin_title} categories" %>

<%=@title%>

+<%= render partial: 'scope' %> +
- <%= link_to 'New category', new_admin_category_path, class: "btn btn-primary" %> + <%= link_to 'New category', new_admin_category_path(model_type: current_klass), class: "btn btn-primary" %>
@@ -21,7 +23,7 @@ <%= heading.children.size %> <%= chevron_right %> - <%= link_to(heading.title, edit_admin_category_path(heading), title: "view full details") %> + <%= link_to(heading.title, edit_admin_category_path(heading, model_type: current_klass), title: "view full details") %>
@@ -34,7 +36,7 @@ <% end %> - <%= link_to(category.title, edit_admin_category_path(category), title: "view full details") %> + <%= link_to(category.title, edit_admin_category_path(category, model_type: current_klass), title: "view full details") %>
<% end %> diff --git a/app/views/admin/categories/new.html.erb b/app/views/admin/categories/new.html.erb index 2399a59282..66eb1204db 100644 --- a/app/views/admin/categories/new.html.erb +++ b/app/views/admin/categories/new.html.erb @@ -5,12 +5,12 @@
- <%= form_for @category, url: admin_categories_path, html: { class: "form form-horizontal" } do |f| %> + <%= form_for @category, url: admin_categories_path(model_type: current_klass), html: { class: "form form-horizontal" } do |f| %> <%= render partial: 'form', locals: { f: f } %>
<%= f.submit "Create", class: "btn btn-primary" %> - <%= link_to 'List all', admin_categories_path, class: "btn" %> + <%= link_to 'List all', admin_categories_path(model_type: current_klass), class: "btn" %>
<% end %>
diff --git a/app/views/admin_general/_admin_navbar.html.erb b/app/views/admin_general/_admin_navbar.html.erb index 8838e892bd..f63c41dbe1 100644 --- a/app/views/admin_general/_admin_navbar.html.erb +++ b/app/views/admin_general/_admin_navbar.html.erb @@ -26,7 +26,7 @@ @@ -36,6 +36,7 @@ diff --git a/app/views/alaveteli_pro/batch_request_authority_searches/_browse.html.erb b/app/views/alaveteli_pro/batch_request_authority_searches/_browse.html.erb index 219dfe144f..fe7af56893 100644 --- a/app/views/alaveteli_pro/batch_request_authority_searches/_browse.html.erb +++ b/app/views/alaveteli_pro/batch_request_authority_searches/_browse.html.erb @@ -3,7 +3,7 @@ <% else %> <% show_add_all = feature_enabled?(:pro_batch_category_add_all, current_user) %> - <% Category.public_body_root.tree.each do |heading| %> + <% PublicBody.categories.each do |heading| %>

<%= heading.title %>