From c8d59334eb2a0b2838db020f654d89d740a09c73 Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Thu, 23 Nov 2023 13:19:54 +0000 Subject: [PATCH 1/4] Add Categorisable concern --- .../admin/categories_controller.rb | 2 +- app/models/category.rb | 4 ---- app/models/concerns/categorisable.rb | 22 +++++++++++++++++++ app/models/public_body.rb | 1 + .../_browse.html.erb | 2 +- app/views/public_body/list.html.erb | 2 +- lib/tasks/temp.rake | 4 ++-- .../admin/categories_controller_spec.rb | 14 ++++++------ spec/models/category_spec.rb | 8 +------ spec/models/concerns/categorisable.rb | 16 ++++++++++++++ spec/models/public_body_spec.rb | 2 ++ 11 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 app/models/concerns/categorisable.rb create mode 100644 spec/models/concerns/categorisable.rb diff --git a/app/controllers/admin/categories_controller.rb b/app/controllers/admin/categories_controller.rb index 93ec162b4e..b758b9deee 100644 --- a/app/controllers/admin/categories_controller.rb +++ b/app/controllers/admin/categories_controller.rb @@ -82,7 +82,7 @@ def category_params end def set_root - @root = Category.public_body_root + @root = PublicBody.category_root end def set_category 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/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/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 %>