Skip to content

Commit

Permalink
Rebuild note CRUD using turbo frames.
Browse files Browse the repository at this point in the history
There was previously a single controller and set of views that handled
all note CRUD for both Member and Item. As a part of reworking this
code, I created separate controllers and views for the notes on each of
these models. I think this will be eaiser to understand and will allow
the notes placed on each model to diverge more easily.
  • Loading branch information
jim committed Nov 16, 2023
1 parent 65d3bd5 commit e794d42
Show file tree
Hide file tree
Showing 32 changed files with 261 additions and 154 deletions.
8 changes: 7 additions & 1 deletion app/controllers/admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ class BaseController < ApplicationController
before_action :require_staff
before_action :load_requested_renewal_request_count

layout "admin"
layout :custom_layout

private

def custom_layout
return "turbo_rails/frame" if turbo_frame_request?

"admin"
end

def require_staff
unless current_user.roles.include?(:staff)
redirect_to root_url, warning: "You do not have access to that page."
Expand Down
58 changes: 58 additions & 0 deletions app/controllers/admin/items/notes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module Admin
module Items
class NotesController < BaseController
include ActionView::RecordIdentifier

before_action :load_item

def create
@note = @item.notes.create(note_params.merge(creator: current_user))

if @note.save
respond_to do |format|
format.turbo_stream
end
else
render :new, status: :unprocessable_entity
end
end

def update
load_note

if @note.update(note_params)
redirect_to [:admin, @item, anchor: dom_id(@note)], status: :see_other
else
render :edit, status: :unprocessable_entity
end
end

def new
@note = @item.notes.new
end

def edit
load_note
end

def show
load_note
end

def destroy
load_note
@note.destroy!
end

private

def note_params
params.require(:note).permit(:body)
end

def load_note
@note = @item.notes.find(params[:id])
end
end
end
end
1 change: 1 addition & 0 deletions app/controllers/admin/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def index
end

def show
@notes = @item.notes.with_all_rich_text
end

def number
Expand Down
58 changes: 58 additions & 0 deletions app/controllers/admin/members/notes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module Admin
module Members
class NotesController < BaseController
include ActionView::RecordIdentifier

before_action :load_member

def create
@note = @member.notes.create(note_params.merge(creator: current_user))

if @note.save
respond_to do |format|
format.turbo_stream
end
else
render :new, status: :unprocessable_entity
end
end

def update
load_note

if @note.update(note_params)
redirect_to [:admin, @member, anchor: dom_id(@note)], status: :see_other
else
render :edit, status: :unprocessable_entity
end
end

def new
@note = @member.notes.new
end

def edit
load_note
end

def show
load_note
end

def destroy
load_note
@note.destroy!
end

private

def note_params
params.require(:note).permit(:body)
end

def load_note
@note = @member.notes.find(params[:id])
end
end
end
end
67 changes: 0 additions & 67 deletions app/controllers/admin/notes_controller.rb

This file was deleted.

4 changes: 4 additions & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ def self.find_by_complete_number(complete_number)
joins(:borrow_policy).find_by(borrow_policies: {code: code}, number: number.to_i)
end

def hello
10
end

def assign_number
if number.blank?
return unless borrow_policy
Expand Down
25 changes: 25 additions & 0 deletions app/views/admin/items/notes/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<div class="note-form">
<%= form_with(model: note, url: [:admin, item, note], builder: SpectreFormBuilder) do |form| %>
<%= form.errors %>
<%= form.rich_text_area :body, label: (note.new_record? ? "Add a note" : "Edit note") %>
<%= form.actions do %>
<%= form.submit class: "btn btn-primary" %>

or
<% if note.new_record? %>
<%= link_to "Cancel", [:admin, item], data: {"turbo-frame" => "_top"} %>
<% else %>
<%= link_to "Cancel", [:admin, item, note] %>
<% end %>
<% end %>
<% end %>
<% unless note.new_record? %>
<br>
<%= tag.div class: "notes-actions" do %>
<%= button_to "Delete Note", [:admin, item, note], method: :delete, class: "btn btn-link", data: { 'turbo-confirm': 'Are you sute you want to delete this note?'} %>
<% end %>
<% end %>
</div>
7 changes: 7 additions & 0 deletions app/views/admin/items/notes/_list.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div id="notes">
<% @notes.by_creation_date.each do |note| %>
<%= render partial: "admin/items/notes/note", locals: { note: note, item: @item } %>
<% end %>
</div>

<%= render partial: "admin/items/notes/new_button", locals: { item: @item } %>
5 changes: 5 additions & 0 deletions app/views/admin/items/notes/_new_button.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= turbo_frame_tag "new-note" do %>
<%= link_to new_admin_item_note_url(item), class: "btn" do %>
<%= feather_icon "message-square" %>Add a Note
<% end %>
<% end %>
10 changes: 10 additions & 0 deletions app/views/admin/items/notes/_note.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%= turbo_frame_tag note do %>
<%= tag.blockquote class: "note" do %>
<%= note.body %>
<br>

<%= tag.div class: "notes-actions" do %>
<%= link_to "Edit Note", [:edit, :admin, item, note], method: :get, remote: true, class: "btn btn-sm" %>
<% end %>
<% end %>
<% end %>
11 changes: 11 additions & 0 deletions app/views/admin/items/notes/create.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<turbo-stream action="append" target="notes">
<template>
<%= render partial: "admin/items/notes/note", formats: [:html], locals: { note: @note, item: @item } %>
</template>
</turbo-stream>

<turbo-stream action="replace" target="new-note">
<template>
<%= render partial: "admin/items/notes/new_button", formats: [:html], locals: { item: @item } %>
</template>
</turbo-stream>
1 change: 1 addition & 0 deletions app/views/admin/items/notes/destroy.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= turbo_frame_tag @note do %><% end %>
3 changes: 3 additions & 0 deletions app/views/admin/items/notes/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= turbo_frame_tag @note do %>
<%= render partial: "form", locals: {note: @note, item: @item} %>
<% end %>
3 changes: 3 additions & 0 deletions app/views/admin/items/notes/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= turbo_frame_tag "new-note" do %>
<%= render partial: "form", locals: {note: @note, item: @item} %>
<% end %>
1 change: 1 addition & 0 deletions app/views/admin/items/notes/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render partial: "admin/items/notes/note", locals: { note: @note, item: @item } %>
2 changes: 1 addition & 1 deletion app/views/admin/items/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
</div>
<% end %>
<%= render partial: "admin/notes/list", locals: {parent: @item} %>
<%= render partial: "admin/items/notes/list", locals: { item: @item } %>
<% end %>
15 changes: 3 additions & 12 deletions app/views/admin/members/_profile.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% content_for :header do %>
<%= index_header preferred_or_default_name(@member) do %>
<%= link_to "Edit Member", edit_admin_member_path(@member), class: "btn" %>
<%= link_to "Add Note to Member", new_admin_member_note_path(@member), class: "btn" %>
<%= link_to "Add Note to Member", new_admin_member_note_path(@member), class: "btn", data: {turbo_frame: 'new-note'} %>
<%# <%= link_to 'Record Payment', new_admin_member_payment_path(@member), class: "btn" %>
<% end %>
<% end %>
Expand All @@ -22,18 +22,9 @@
</div>
<% end %>


<div class="member-notes">
<% @member.notes.newest_first.each do |note| %>
<div class="toast note">
<%= link_to "Edit Note", [:edit, :admin, @member, note], method: :get, class: "btn btn-sm edit-button" %>
<div class="trix-content">
<%= note.body -%>
</div>
<p class="text-italic">
- <%= note.creator.member.preferred_name %>, <%= time_ago_in_words(note.created_at) %> ago
</p>
</div>
<% end %>
<%= render partial: "admin/members/notes/list", locals: { member: @member } %>
</div>

<ul class="tab member-tabs">
Expand Down
25 changes: 25 additions & 0 deletions app/views/admin/members/notes/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<div class="note-form">
<%= form_with(model: note, url: [:admin, member, note], builder: SpectreFormBuilder) do |form| %>
<%= form.errors %>
<%= form.rich_text_area :body, label: (note.new_record? ? "Add a note" : "Edit note") %>
<%= form.actions do %>
<%= form.submit class: "btn btn-primary" %>

or
<% if note.new_record? %>
<%= link_to "Cancel", [:admin, member], data: {"turbo-frame" => "_top"} %>
<% else %>
<%= link_to "Cancel", [:admin, member, note] %>
<% end %>
<% end %>
<% end %>
<% unless note.new_record? %>
<br>
<%= tag.div class: "notes-actions" do %>
<%= button_to "Delete Note", [:admin, member, note], method: :delete, class: "btn btn-link", data: { 'turbo-confirm': 'Are you sute you want to delete this note?'} %>
<% end %>
<% end %>
</div>
7 changes: 7 additions & 0 deletions app/views/admin/members/notes/_list.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= turbo_frame_tag "new-note" do %><% end %>

<div id="notes">
<% @member.notes.with_all_rich_text.by_creation_date.each do |note| %>
<%= render partial: "admin/members/notes/note", locals: { note: note, member: @member } %>
<% end %>
</div>
10 changes: 10 additions & 0 deletions app/views/admin/members/notes/_note.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%= turbo_frame_tag note do %>
<%= tag.blockquote class: "note" do %>
<%= note.body %>
<br>

<%= tag.div class: "notes-actions" do %>
<%= link_to "Edit Note", [:edit, :admin, member, note], method: :get, remote: true, class: "btn btn-sm" %>
<% end %>
<% end %>
<% end %>
11 changes: 11 additions & 0 deletions app/views/admin/members/notes/create.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<turbo-stream action="append" target="notes">
<template>
<%= render partial: "admin/members/notes/note", formats: [:html], locals: { note: @note, member: @member } %>
</template>
</turbo-stream>

<turbo-stream action="replace" target="new-note">
<template>
<%= turbo_frame_tag "new-note" do %><% end %>
</template>
</turbo-stream>
Loading

0 comments on commit e794d42

Please sign in to comment.