Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#7811] Allow InfoRequest to be categorised #8025

Merged
merged 4 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions app/controllers/admin/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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
4 changes: 0 additions & 4 deletions app/models/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions app/models/concerns/categorisable.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions app/models/info_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class InfoRequest < ApplicationRecord
include InfoRequest::PublicToken
include InfoRequest::Sluggable
include InfoRequest::TitleValidation
include Categorisable
include Taggable
include Notable
include LinkToHelper
Expand Down
1 change: 1 addition & 0 deletions app/models/public_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

class PublicBody < ApplicationRecord
include CalculatedHomePage
include Categorisable
include Taggable
include Notable
include Rails.application.routes.url_helpers
Expand Down
7 changes: 7 additions & 0 deletions app/views/admin/categories/_scope.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ul class="nav nav-tabs">
<% Categorisable.models.sort_by(&:admin_title).each do |klass| %>
<%= tag.li class: { active: current_klass == klass } do %>
<%= link_to klass.admin_title.pluralize, url_for(model_type: klass.name, query: params[:query]) %>
<% end %>
<% end %>
</ul>
8 changes: 5 additions & 3 deletions app/views/admin/categories/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
<div class="row">
<div class="span8">
<div id="category_form">
<%= 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 } %>

<div class="form-actions">
<%= 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' %>
</div>
<% end %>
</div>
Expand Down
10 changes: 6 additions & 4 deletions app/views/admin/categories/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<% @title = 'Listing public authority categories' %>
<% @title = "Listing #{current_klass.admin_title} categories" %>

<h1><%=@title%></h1>

<%= render partial: 'scope' %>

<div class="btn-toolbar">
<div class="btn-group">
<%= 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" %>
</div>
</div>

Expand All @@ -21,7 +23,7 @@
<span class="badge"><%= heading.children.size %></span>
<%= chevron_right %>
</a>
<strong><%= link_to(heading.title, edit_admin_category_path(heading), title: "view full details") %></strong>
<strong><%= link_to(heading.title, edit_admin_category_path(heading, model_type: current_klass), title: "view full details") %></strong>
</span>
</div>

Expand All @@ -34,7 +36,7 @@
<i class="icon-move"></i>
<% 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") %>
</div>
<% end %>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/categories/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<div class="row">
<div class="span8">
<div id="public_category_form">
<%= 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 } %>

<div class="form-actions">
<%= 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" %>
</div>
<% end %>
</div>
Expand Down
3 changes: 2 additions & 1 deletion app/views/admin_general/_admin_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Authorities<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><%= link_to 'Authorities', admin_bodies_path %></li>
<li><%= link_to 'Categories', admin_categories_path %></li>
<li><%= link_to 'Categories', admin_categories_path(model_type: 'PublicBody') %></li>
<li><%= link_to 'Tags', admin_tags_path(model_type: 'PublicBody') %></li>
</ul>
</li>
Expand All @@ -36,6 +36,7 @@
<ul class="dropdown-menu" role="menu">
<li><%= link_to 'Requests', admin_requests_path %></li>
<li><%= link_to 'Comments', admin_comments_path %></li>
<li><%= link_to 'Categories', admin_categories_path(model_type: 'InfoRequest') %></li>
<li><%= link_to 'Tags', admin_tags_path(model_type: 'InfoRequest') %></li>
<% if feature_enabled?(:refusal_snippets) %><li><%= link_to 'Snippets', admin_snippets_path %></li><% end %>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<% else %>
<% show_add_all = feature_enabled?(:pro_batch_category_add_all, current_user) %>
<ul class="batch-builder__list">
<% Category.public_body_root.tree.each do |heading| %>
<% PublicBody.categories.each do |heading| %>
<li class="batch-builder__list__group">
<div class="batch-builder__list__item">
<span class="batch-builder__list__item__anchor">
Expand Down
2 changes: 1 addition & 1 deletion app/views/public_body/list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</li>
</ul>

<% Category.public_body_root.tree.each do |heading| %>
<% PublicBody.categories.each do |heading| %>
<h3><%= heading.title %></h3>
<ul>
<% heading.children.each do |category| %>
Expand Down
1 change: 1 addition & 0 deletions doc/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Highlighted Features

* Allow `InfoRequest` to be categorised (Graeme Porteous)
* Replace public body categories with generalised categories (Graeme Porteous)
* Add admin links to and from batch request show action (Graeme Porteous)
* Update request base calculated status for internal reviews (Graeme Porteous)
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/temp.rake
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace :temp do
desc 'Migrate PublicBodyCategory into Category model'
task migrate_public_body_categories: :environment do
next if Category.public_body_root.children.any?
next if PublicBody.categories.any?

scope = PublicBodyCategoryLink.by_display_order.to_a
count = scope.count

root = Category.public_body_root
root = PublicBody.category_root

scope.each.with_index do |link, index|
h = link.public_body_heading
Expand Down
81 changes: 60 additions & 21 deletions spec/controllers/admin/categories_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@
expect(response).to be_successful
end

it 'assigns root' do
get :index
expect(assigns(:root)).to eq(Category.public_body_root)
it 'raise 404 for unknown types' do
expect { get :index, params: { model_type: 'unknown' } }.to(
raise_error ApplicationController::RouteNotFound
)
end

it 'assigns root for correct model' do
get :index, params: { model_type: 'PublicBody' }
expect(assigns(:root)).to eq(PublicBody.category_root)

get :index, params: { model_type: 'InfoRequest' }
expect(assigns(:root)).to eq(InfoRequest.category_root)
end

it 'renders the index template' do
Expand All @@ -19,9 +28,12 @@
end

describe 'GET new' do
it 'assigns root' do
get :new
expect(assigns(:root)).to eq(Category.public_body_root)
it 'assigns root for correct model' do
get :new, params: { model_type: 'PublicBody' }
expect(assigns(:root)).to eq(PublicBody.category_root)

get :new, params: { model_type: 'InfoRequest' }
expect(assigns(:root)).to eq(InfoRequest.category_root)
end

it 'responds successfully' do
Expand All @@ -47,15 +59,24 @@
end

describe 'POST create' do
it 'assigns root' do
post :create, params: { category: { title: 'Title' } }
expect(assigns(:root)).to eq(Category.public_body_root)
it 'assigns root for correct model' do
post :create, params: {
model_type: 'PublicBody',
category: { title: 'Title' }
}
expect(assigns(:root)).to eq(PublicBody.category_root)

post :create, params: {
model_type: 'InfoRequest',
category: { title: 'Title' }
}
expect(assigns(:root)).to eq(InfoRequest.category_root)
end

it "default category's parent associations to root" do
post :create, params: { category: { title: 'Title' } }
expect(assigns(:category).parents).
to match_array(Category.public_body_root)
to match_array(PublicBody.category_root)
end

it "saves new category's parent associations" do
Expand Down Expand Up @@ -93,7 +114,8 @@

it 'redirects to the categories index' do
post :create, params: { category: params }
expect(response).to redirect_to(admin_categories_path)
expect(response).
to redirect_to(admin_categories_path(model_type: 'PublicBody'))
end
end

Expand Down Expand Up @@ -175,9 +197,12 @@
)
end

it 'assigns root' do
get :edit, params: { id: category.id }
expect(assigns(:root)).to eq(Category.public_body_root)
it 'assigns root for correct model' do
get :edit, params: { model_type: 'PublicBody', id: category.id }
expect(assigns(:root)).to eq(PublicBody.category_root)

get :edit, params: { model_type: 'InfoRequest', id: category.id }
expect(assigns(:root)).to eq(InfoRequest.category_root)
end

it 'responds successfully' do
Expand Down Expand Up @@ -230,9 +255,20 @@
}
end

it 'assigns root' do
patch :update, params: { id: category.id, category: params }
expect(assigns(:root)).to eq(Category.public_body_root)
it 'assigns root for correct model' do
patch :update, params: {
model_type: 'PublicBody',
id: category.id,
category: params
}
expect(assigns(:root)).to eq(PublicBody.category_root)

patch :update, params: {
model_type: 'InfoRequest',
id: category.id,
category: params
}
expect(assigns(:root)).to eq(InfoRequest.category_root)
end

it 'finds the category to update' do
Expand All @@ -243,7 +279,7 @@
it "default category's parent associations to root" do
patch :update, params: { id: category.id, category: params }
expect(assigns(:category).parents).
to match_array(Category.public_body_root)
to match_array(PublicBody.category_root)
end

it "saves edits to a category's parent associations" do
Expand Down Expand Up @@ -302,7 +338,8 @@

it 'redirects to the category edit page' do
patch :update, params: { id: category.id, category: params }
expect(response).to redirect_to(admin_categories_path)
expect(response).
to redirect_to(admin_categories_path(model_type: 'PublicBody'))
end

it 'saves edits to category_tag if the category has no associated bodies' do
Expand Down Expand Up @@ -405,7 +442,8 @@

it 'redirects to the edit page after a successful update' do
patch :update, params: { id: category.id, category: { title: 'Title' } }
expect(response).to redirect_to(admin_categories_path)
expect(response).
to redirect_to(admin_categories_path(model_type: 'PublicBody'))
end
end

Expand Down Expand Up @@ -483,7 +521,8 @@

it 'redirects to the categories index' do
delete :destroy, params: { id: category.id }
expect(response).to redirect_to(admin_categories_path)
expect(response).
to redirect_to(admin_categories_path(model_type: 'PublicBody'))
end
end

Expand Down
Loading