diff --git a/app/controllers/admin/notes_controller.rb b/app/controllers/admin/notes_controller.rb index 4c0310ac55e..dd9c7f0941a 100644 --- a/app/controllers/admin/notes_controller.rb +++ b/app/controllers/admin/notes_controller.rb @@ -3,12 +3,12 @@ class Admin::NotesController < AdminController include TranslatableParams def new - @note = Note.new + @note = scope.build @note.build_all_translations end def create - @note = Note.new(note_params) + @note = scope.build(note_params) if @note.save notice = 'Note successfully created.' redirect_to admin_note_parent_path(@note), notice: notice @@ -19,12 +19,12 @@ def create end def edit - @note = Note.find(params[:id]) + @note = scope.find(params[:id]) @note.build_all_translations end def update - @note = Note.find(params[:id]) + @note = scope.find(params[:id]) if @note.update(note_params) notice = 'Note successfully updated.' redirect_to admin_note_parent_path(@note), notice: notice @@ -43,6 +43,10 @@ def destroy private + def scope + Note.where(params.slice(:notable_id, :notable_type).permit!) + end + def note_params translatable_params( params.require(:note), diff --git a/app/models/concerns/notable.rb b/app/models/concerns/notable.rb new file mode 100644 index 00000000000..2e5d2b8bf26 --- /dev/null +++ b/app/models/concerns/notable.rb @@ -0,0 +1,15 @@ +module Notable + extend ActiveSupport::Concern + + included do + has_many :concrete_notes, + class_name: 'Note', + as: :notable, + inverse_of: :notable, + dependent: :destroy + end + + def all_notes + concrete_notes.with_translations + end +end diff --git a/app/models/note.rb b/app/models/note.rb index 13ed304fc8c..7740bba322e 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -21,4 +21,5 @@ class Note < ApplicationRecord belongs_to :notable, polymorphic: true validates :body, presence: true + validates :notable, presence: true end diff --git a/app/models/public_body.rb b/app/models/public_body.rb index 2eafe753ee7..17e31be3832 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -36,6 +36,7 @@ class PublicBody < ApplicationRecord include AdminColumn include Taggable + include Notable class ImportCSVDryRun < StandardError; end diff --git a/app/views/admin/notes/_form.html.erb b/app/views/admin/notes/_form.html.erb index f22f4e245b3..9c56026bd4e 100644 --- a/app/views/admin/notes/_form.html.erb +++ b/app/views/admin/notes/_form.html.erb @@ -1,5 +1,11 @@ <%= foi_error_messages_for :note %> +
+ Applies to <%= both_links(@note.notable) %> + <%= f.hidden_field :notable_id %> + <%= f.hidden_field :notable_type %> +
+