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

[#4478] New notes model and admin controller #7212

Merged
merged 5 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
57 changes: 57 additions & 0 deletions app/controllers/admin/notes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class Admin::NotesController < AdminController
include Admin::TagHelper
include TranslatableParams

def new
@note = scope.build
@note.build_all_translations
end

def create
@note = scope.build(note_params)
if @note.save
notice = 'Note successfully created.'
redirect_to admin_note_parent_path(@note), notice: notice
else
@note.build_all_translations
render :new
end
end

def edit
@note = scope.find(params[:id])
@note.build_all_translations
end

def update
@note = scope.find(params[:id])
if @note.update(note_params)
notice = 'Note successfully updated.'
redirect_to admin_note_parent_path(@note), notice: notice
else
@note.build_all_translations
render :edit
end
end

def destroy
@note = Note.find(params[:id])
@note.destroy
notice = 'Note successfully destroyed.'
redirect_to admin_note_parent_path(@note), notice: notice
end

private

def scope
Note.where(params.slice(:notable_id, :notable_type).permit!)
end

def note_params
translatable_params(
params.require(:note),
translated_keys: [:locale, :body],
general_keys: [:notable_id, :notable_type]
)
end
end
4 changes: 4 additions & 0 deletions app/helpers/admin/tag_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def render_tags(tags)

def render_tag(record_tag)
tag.span class: 'label label-info tag' do
if record_tag.is_a?(String)
record_tag = HasTagString::HasTagStringTag.from_string(record_tag)
end

render_tag_href(record_tag)
end
end
Expand Down
15 changes: 15 additions & 0 deletions app/models/concerns/notable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Notable
extend ActiveSupport::Concern

included do
has_many :concrete_notes,
gbp marked this conversation as resolved.
Show resolved Hide resolved
class_name: 'Note',
as: :notable,
inverse_of: :notable,
dependent: :destroy
end

def all_notes
concrete_notes.with_translations
end
end
25 changes: 25 additions & 0 deletions app/models/note.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# == Schema Information
# Schema version: 20220720085105
#
# Table name: notes
#
# id :bigint not null, primary key
# notable_type :string
# notable_id :bigint
# notable_tag :string
# created_at :datetime not null
# updated_at :datetime not null
# body :text
#

class Note < ApplicationRecord
include AdminColumn

translates :body
include Translatable

belongs_to :notable, polymorphic: true

validates :body, presence: true
validates :notable, presence: true
end
1 change: 1 addition & 0 deletions app/models/public_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
class PublicBody < ApplicationRecord
include AdminColumn
include Taggable
include Notable

class ImportCSVDryRun < StandardError; end

Expand Down
33 changes: 33 additions & 0 deletions app/views/admin/notes/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<%= foi_error_messages_for :note %>

<div class="well">
Applies to <%= both_links(@note.notable) %>
<%= f.hidden_field :notable_id %>
<%= f.hidden_field :notable_type %>
</div>

<div id="div-locales">
<ul class="locales nav nav-tabs">
<% @note.ordered_translations.each do |translation| %>
<li>
<a href="#div-locale-<%= translation.locale.to_s %>" data-toggle="tab" >
<%= locale_name(translation.locale.to_s) || translation.locale.to_s %>
</a>
</li>
<% end %>
</ul>

<div class="tab-content">
<% @note.ordered_translations.each do |translation| %>
<% if AlaveteliLocalization.default_locale?(translation.locale) %>
<%= fields_for('note', @note) do |t| %>
<%= render partial: 'locale_fields', locals: { t: t, locale: translation.locale } %>
<% end %>
<% else %>
<%= f.fields_for(:translations, translation, child_index: translation.locale) do |t| %>
<%= render partial: 'locale_fields', locals: { t: t, locale: translation.locale } %>
<% end %>
<% end %>
<% end %>
</div>
</div>
16 changes: 16 additions & 0 deletions app/views/admin/notes/_locale_fields.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="tab-pane" id="div-locale-<%= locale %>">
<div class="control-group">
<%= t.hidden_field :locale, :value => locale %>

<%= t.label :body, class: 'control-label' %>
<div class="controls">
<% if AlaveteliLocalization.default_locale?(locale) && t.object.errors[:body].any? %>
<span class="fieldWithErrors">
<% end %>
<%= t.text_area :body, class: 'span6', rows: 10 %>
<% if AlaveteliLocalization.default_locale?(locale) && t.object.errors[:body].any? %>
</span>
<%end %>
</div>
</div>
</div>
24 changes: 24 additions & 0 deletions app/views/admin/notes/_note.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="accordion-group">
<div class="accordion-heading accordion-toggle row">
<span class="item-title span6">
<%= link_to chevron_right, "##{dom_id(note)}", data: { toggle: 'collapse', parent: 'notes' } %>
<%= link_to(note.body, edit_admin_note_path(note), title: 'view full details') %>
</span>

<span class="item-metadata span6">
<%= render_tag note.notable_tag if note.notable_tag %>
</span>
</div>
<%= tag.div id: dom_id(note), class: 'item-detail accordion-body collapse row' do %>
<% note.for_admin_column do |name, value, type| %>
<div>
<span class="span6">
<b><%= name %></b>
</span>
<span class="span6">
<%= admin_value(value) %>
</span>
</div>
<% end %>
<% end %>
</div>
31 changes: 31 additions & 0 deletions app/views/admin/notes/_show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<div class="row">
<% if notes.size > 0 %>
<table class="table table-condensed table-hover span12 censor-rule-list">
<tr>
<th>ID</th>
<th>Notable ID</th>
<th>Notable type</th>
<th>Notable tag</th>
<th>Actions</th>
</tr>

<% notes.each do |note| %>
<tr class="<%= cycle('odd', 'even') %>">
<td class="id"><%= h note.id %></td>
<td class="notable_id"><%= h note.notable_id %></td>
<td class="notable_type"><%= h note.notable_type %></td>
<td class="notable_tag"><%= h note.notable_tag %></td>
<td><%= link_to "Edit", edit_admin_note_path(note) %></td>
</tr>
<% end %>
</table>
<% else %>
<p class="span12">None yet.</p>
<% end %>
</div>

<div class="row">
<p class="span12">
<%= link_to "New note", new_admin_note_path(notable_type: notable.class, notable_id: notable), class: "btn btn-info" %>
</p>
</div>
21 changes: 21 additions & 0 deletions app/views/admin/notes/edit.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<div class="row">
<div class="span12">
<div class="page-header">
<h1><%= @title = 'Edit note' %></h1>
</div>
</div>
</div>

<%= form_for [:admin, @note], class: 'form form-horizontal' do |f| %>
<%= render partial: 'form', locals: { f: f } %>
<div class="form-actions">
<%= submit_tag 'Save', class: 'btn btn-success' %>
</div>
<% end %>

<%= form_tag [:admin, @note], class: 'form form-inline', method: 'delete' do %>
<%= submit_tag 'Destroy note',
class: 'btn btn-danger',
data: { confirm: 'Are you sure? This is irreversible.' } %>
(this is permanent!)
<% end %>
14 changes: 14 additions & 0 deletions app/views/admin/notes/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="row">
<div class="span12">
<div class="page-header">
<h1><%= @title = 'New note' %></h1>
</div>
</div>
</div>

<%= form_for [:admin, @note], class: 'form form-horizontal' do |f| %>
<%= render partial: 'form', locals: { f: f } %>
<div class="form-actions">
<%= submit_tag 'Create', class: 'btn btn-success' %>
</div>
<% end %>
8 changes: 8 additions & 0 deletions app/views/admin_public_body/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,11 @@
<h2>Censor rules</h2>

<%= render :partial => 'admin_censor_rule/show', :locals => { :censor_rules => @public_body.censor_rules, :public_body => @public_body } %>

<hr>

<h2>Notes</h2>

<%= render partial: 'admin/notes/show',
locals: { notes: @public_body.all_notes,
notable: @public_body } %>
20 changes: 20 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,20 @@
end
####

#### AdminNote controller
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

be nicer to have shallow routes for new so that we can do e.g. /admin/bodies/3/notes/new (which automatically sets notable_type & notable_id), but then editing the created note is simply /admin/note/:id/edit. not essential this minute though

namespace :admin do
resources :notes, except: [:index, :show]
end

direct :admin_note_parent do |note|
if note.notable
url_for([:admin, note.notable])
else
admin_general_index_path
end
end
####

#### AdminPublicBody controller
scope '/admin', :as => 'admin' do
resources :bodies,
Expand All @@ -498,6 +512,9 @@
:only => [:new, :create]
end
end
direct :admin_public_body do |pb|
admin_body_path(pb)
end
####

#### AdminPublicBodyCategory controller
Expand Down Expand Up @@ -569,6 +586,9 @@
:only => [:new, :create]
end
end
direct :admin_info_request do |ir|
admin_request_path(ir)
end
####

#### AdminComment controller
Expand Down
19 changes: 19 additions & 0 deletions db/migrate/20220720085105_create_notes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class CreateNotes < ActiveRecord::Migration[6.1]
def change
create_table :notes do |t|
t.references :notable, polymorphic: true
t.string :notable_tag
t.timestamps
end

reversible do |dir|
dir.up do
Note.create_translation_table!(body: :text)
end

dir.down do
Note.drop_translation_table!
end
end
end
end
Loading