Skip to content

Commit

Permalink
Merge branch '4478-notes' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed Aug 30, 2022
2 parents ad2391f + 1b19d18 commit ccc5d43
Show file tree
Hide file tree
Showing 26 changed files with 722 additions and 0 deletions.
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_tag, :notable_id, :notable_type).permit!)
end

def note_params
translatable_params(
params.require(:note),
translated_keys: [:locale, :body],
general_keys: [:notable_tag, :notable_id, :notable_type]
)
end
end
3 changes: 3 additions & 0 deletions app/controllers/admin/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def show
@tag
)

@notes = Note.distinct.where(notable_tag: @tag).
paginate(page: params[:page], per_page: 50)

@taggings = current_klass.with_tag(@tag).distinct.
joins(:tags).merge(
apply_filters(HasTagString::HasTagStringTag.all)
Expand Down
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
25 changes: 25 additions & 0 deletions app/models/concerns/notable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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 + tagged_notes.with_translations
end

def tagged_notes
Note.where(notable_tag: notable_tags)
end

private

def notable_tags
tags.map(&:name_and_value)
end
end
31 changes: 31 additions & 0 deletions app/models/note.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# == 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_or_notable_tag, presence: true

private

def notable_or_notable_tag
notable || notable_tag
end
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
38 changes: 38 additions & 0 deletions app/views/admin/notes/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<%= foi_error_messages_for :note %>

<div class="well">
<% if @note.notable %>
Applies to <%= both_links(@note.notable) %>
<%= f.hidden_field :notable_id %>
<%= f.hidden_field :notable_type %>
<% else %>
Applies to objects tagged with <%= render_tag @note.notable_tag %>
<%= f.hidden_field :notable_tag %>
<% end %>
</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 %>
13 changes: 13 additions & 0 deletions app/views/admin/tags/_notes.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h2>Notes</h2>

<div class="btn-toolbar">
<div class="btn-group">
<%= link_to 'New note', new_admin_note_path(notable_tag: @tag), class: 'btn' %>
</div>
</div>

<% if @notes.empty? %>
<p>No notes.</p>
<% else %>
<%= render partial: 'tagging', collection: @notes %>
<% end %>
5 changes: 5 additions & 0 deletions app/views/admin/tags/_tagging.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
snippet: tagging
} %>

<% when Note %>
<%= render partial: 'admin/notes/note', locals: {
note: tagging
} %>

<% else %>
<pre><%= tagging.to_json %></pre>
<% end %>
2 changes: 2 additions & 0 deletions app/views/admin/tags/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<h1><%= @title = "Tag – #{@tag}" %></h1>

<%= render partial: 'notes', locals: { notes: @notes } %>

<div class="row">
<div class="span12">
<h2>Taggings</h2>
Expand Down
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 } %>
22 changes: 22 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,22 @@
end
####

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

direct :admin_note_parent do |note|
if note.notable_tag
admin_tag_path(tag: note.notable_tag)
elsif 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 +514,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 +588,9 @@
:only => [:new, :create]
end
end
direct :admin_info_request do |ir|
admin_request_path(ir)
end
####

#### AdminComment controller
Expand Down
Loading

0 comments on commit ccc5d43

Please sign in to comment.