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

[#7255] Add category notes #8178

Merged
merged 7 commits into from
Apr 12, 2024
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
21 changes: 13 additions & 8 deletions app/controllers/admin/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
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]
before_action :set_root, except: [:destroy, :reorder]
before_action :check_klass

def index
end

def show
redirect_to action: :edit
end

def new
@category = Category.new
@category.build_all_translations
Expand Down Expand Up @@ -75,24 +79,25 @@ def reorder_categories(category_ids)
def category_params
category_params = translatable_params(
params.require(:category),
translated_keys: [:locale, :title, :description],
translated_keys: [:locale, :title, :description, :body],
general_keys: [:category_tag, :parent_ids]
)
category_params[:parent_ids] ||= [@root.id]
category_params
end

def set_root
@root = current_klass.category_root
end

def set_category
@category = Category.find(params[:id])
end

def set_root
@root = current_klass&.category_root
end

helper_method :current_klass
def current_klass
params.fetch(:model_type, 'PublicBody').safe_constantize
@klass ||= @category.root.title.safe_constantize if @category&.root
@klass ||= params.fetch(:model_type, 'PublicBody').safe_constantize
end

def check_klass
Expand Down
6 changes: 6 additions & 0 deletions app/helpers/admin/link_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ def track_thing_both_links(track_thing)
link_to(icon, icon_link, title: title) + ' ' + "#{track_thing.id}:"
end

def category_both_links(category)
# No public links, yet?
link_to(category.title, edit_admin_category_path(category),
title: admin_title)
end

def admin_title
'View full details'
end
Expand Down
22 changes: 20 additions & 2 deletions app/models/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# categories and translatable titles/descriptions.
#
class Category < ApplicationRecord
include Notable

has_many :parent_relationships,
class_name: 'CategoryRelationship',
foreign_key: 'child_id',
Expand All @@ -39,7 +41,10 @@ class Category < ApplicationRecord
class_name: 'HasTagString::HasTagStringTag'

translates :title, :description
translates :body, touch: true
include Translatable
delegate :body, :body=, :body?, to: :translation
after_save { body.save if body.changed? }

validates :title, presence: true
validate :check_tag_assignments, on: :update
Expand All @@ -54,6 +59,16 @@ def tree
end

def list
Category.where(id: list_ids).includes(:translations)
end

def root
Category.roots.find { _1.list_ids.include?(id) }
end

protected

def list_ids
sql = <<~SQL.squish
WITH RECURSIVE nested_categories AS (
SELECT child_id
Expand All @@ -70,8 +85,7 @@ def list
INNER JOIN nested_categories nc ON c.id = nc.child_id;
SQL

ids = Category.find_by_sql([sql, { parent_id: id }]).map(&:id)
Category.where(id: ids).includes(:translations)
Category.find_by_sql([sql, { parent_id: id }]).map(&:id)
end

private
Expand All @@ -85,4 +99,8 @@ def check_tag_assignments
message: "can't be changed as there are associated objects present"
)
end

class Translation # :nodoc:
has_rich_text :body
end
end
5 changes: 4 additions & 1 deletion app/models/concerns/notable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ module Notable
end

def all_notes
concrete_notes.with_translations + tagged_notes.with_translations
notes = concrete_notes.with_translations
return notes.to_a unless Taggable.models.include?(self.class)

notes + tagged_notes.with_translations
end

def tagged_notes
Expand Down
6 changes: 6 additions & 0 deletions app/views/admin/categories/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@
<% end %>
</div>
<% end %>


<h2>Notes</h2>

<%= render partial: 'admin/notes/show',
locals: { notes: @category.all_notes, notable: @category } %>
7 changes: 7 additions & 0 deletions app/views/admin/categories/_locale_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@
<%= t.text_field :description, class: "span4" %>
</div>
</div>

<div class="control-group">
<%= t.label :body, class: 'control-label' %>
<div class="controls">
<%= t.rich_text_area :body %>
</div>
</div>
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 categories to have notes associated with them (Graeme Porteous)
* Add styling option and rich text editor to the notes admin (Graeme Porteous)
* Strengthen 2FA warning. Users *must* remember to keep this code safe (Gareth
Rees)
Expand Down
12 changes: 10 additions & 2 deletions spec/controllers/admin/categories_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
end
end

describe 'GET show' do
it 'redirects to the edit action' do
category = FactoryBot.create(:category)
get :show, params: { id: category.id }
expect(response).to redirect_to(edit_admin_category_path(category))
end
end

describe 'GET new' do
it 'assigns root for correct model' do
get :new, params: { model_type: 'PublicBody' }
Expand Down Expand Up @@ -255,7 +263,7 @@
}
end

it 'assigns root for correct model' do
it 'overrides model type param in favor of the categories root' do
patch :update, params: {
model_type: 'PublicBody',
id: category.id,
Expand All @@ -268,7 +276,7 @@
id: category.id,
category: params
}
expect(assigns(:root)).to eq(InfoRequest.category_root)
expect(assigns(:root)).to eq(PublicBody.category_root)
end

it 'finds the category to update' do
Expand Down
35 changes: 35 additions & 0 deletions spec/models/category_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
#

require 'spec_helper'
require 'models/concerns/notable'

RSpec.describe Category, type: :model do
it_behaves_like 'concerns/notable', :category

set_fixture_class has_tag_string_tags: HasTagString::HasTagStringTag

let(:category) { FactoryBot.build(:category) }
Expand Down Expand Up @@ -214,4 +217,36 @@
expect { list.each(&:title) }.to_not change { @query_count }
end
end

describe '#root' do
subject { branch.root }

let(:root) do
FactoryBot.create(:category, title: 'PublicBody')
end

let(:trunk) do
FactoryBot.create(:category, title: 'Trunk', parents: [root])
end

let(:branch) do
FactoryBot.create(:category, title: 'Branch', parents: [trunk])
end

it 'returns uppermost root category' do
expect(branch.root).to eq(root)
end
end

describe 'translations' do
def plain_body
category.body_translations.transform_values(&:to_plain_text)
end

it 'adds translated body' do
expect(plain_body).to_not include(es: 'content')
AlaveteliLocalization.with_locale(:es) { category.body = 'content' }
expect(plain_body).to include(es: 'content')
end
end
end
Loading