-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rebuild note CRUD using turbo frames.
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
Showing
32 changed files
with
261 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ def index | |
end | ||
|
||
def show | ||
@notes = @item.notes.with_all_rich_text | ||
end | ||
|
||
def number | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= turbo_frame_tag @note do %><% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render partial: "admin/items/notes/note", locals: { note: @note, item: @item } %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.