diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb index 781649d38..3324c203e 100644 --- a/app/controllers/admin/base_controller.rb +++ b/app/controllers/admin/base_controller.rb @@ -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." diff --git a/app/controllers/admin/items/notes_controller.rb b/app/controllers/admin/items/notes_controller.rb new file mode 100644 index 000000000..a5f2aaa25 --- /dev/null +++ b/app/controllers/admin/items/notes_controller.rb @@ -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 diff --git a/app/controllers/admin/items_controller.rb b/app/controllers/admin/items_controller.rb index 9cde6eb09..86edffc98 100644 --- a/app/controllers/admin/items_controller.rb +++ b/app/controllers/admin/items_controller.rb @@ -25,6 +25,7 @@ def index end def show + @notes = @item.notes.with_all_rich_text end def number diff --git a/app/controllers/admin/members/notes_controller.rb b/app/controllers/admin/members/notes_controller.rb new file mode 100644 index 000000000..b1474b966 --- /dev/null +++ b/app/controllers/admin/members/notes_controller.rb @@ -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 diff --git a/app/controllers/admin/notes_controller.rb b/app/controllers/admin/notes_controller.rb deleted file mode 100644 index 2016652d0..000000000 --- a/app/controllers/admin/notes_controller.rb +++ /dev/null @@ -1,67 +0,0 @@ -module Admin - class NotesController < BaseController - include ActionView::RecordIdentifier - include PortalRendering - - before_action :load_parent - - def create - @note = @parent.notes.create(note_params.merge(creator: current_user)) - - if @note.save - redirect_to [:admin, @parent, anchor: dom_id(@note)], status: :see_other - else - render_to_portal "form", locals: {parent: @parent, note: @note}, status: 422 - end - end - - def update - @note = @parent.notes.find(params[:id]) - - if @note.update(note_params) - redirect_to [:admin, @parent, anchor: dom_id(@note)], status: :see_other - else - render_to_portal "form", locals: {parent: @parent, note: @note}, status: 422 - end - end - - def new - @note = @parent.notes.new - end - - def edit - @note = @parent.notes.find(params[:id]) - - if @parent.is_a?(Member) - render "_form", locals: {parent: @parent, note: @note}, status: :see_other - else - render_to_portal "form", locals: {parent: @parent, note: @note} - end - end - - def show - @note = @parent.notes.find(params[:id]) - render_to_portal "show", locals: {parent: @parent, note: @note} - end - - def destroy - @parent.notes.find(params[:id]).destroy! - redirect_to [:admin, @parent], flash: {success: "Note has been deleted."}, status: :see_other - end - - private - - def note_params - params.require(:note).permit(:body) - end - - def load_parent - if params[:item_id] - @parent = Item.find(params[:item_id]) - elsif params[:member_id] - @parent = Member.find(params[:member_id]) - end - raise ActiveRecord::RecordNotFound unless @parent - end - end -end diff --git a/app/models/item.rb b/app/models/item.rb index 1f47348fe..291953ec9 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -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 diff --git a/app/views/admin/items/notes/_form.html.erb b/app/views/admin/items/notes/_form.html.erb new file mode 100644 index 000000000..fb7126d4e --- /dev/null +++ b/app/views/admin/items/notes/_form.html.erb @@ -0,0 +1,25 @@ +
+ <%= 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? %> +
+ <%= 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 %> +
diff --git a/app/views/admin/items/notes/_list.html.erb b/app/views/admin/items/notes/_list.html.erb new file mode 100644 index 000000000..c3e027613 --- /dev/null +++ b/app/views/admin/items/notes/_list.html.erb @@ -0,0 +1,7 @@ +
+ <% @notes.by_creation_date.each do |note| %> + <%= render partial: "admin/items/notes/note", locals: { note: note, item: @item } %> + <% end %> +
+ +<%= render partial: "admin/items/notes/new_button", locals: { item: @item } %> diff --git a/app/views/admin/items/notes/_new_button.html.erb b/app/views/admin/items/notes/_new_button.html.erb new file mode 100644 index 000000000..b4156a89f --- /dev/null +++ b/app/views/admin/items/notes/_new_button.html.erb @@ -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 %> \ No newline at end of file diff --git a/app/views/admin/items/notes/_note.html.erb b/app/views/admin/items/notes/_note.html.erb new file mode 100644 index 000000000..a01ebd266 --- /dev/null +++ b/app/views/admin/items/notes/_note.html.erb @@ -0,0 +1,10 @@ +<%= turbo_frame_tag note do %> + <%= tag.blockquote class: "note" do %> + <%= note.body %> +
+ + <%= 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 %> \ No newline at end of file diff --git a/app/views/admin/items/notes/create.turbo_stream.erb b/app/views/admin/items/notes/create.turbo_stream.erb new file mode 100644 index 000000000..d1582efd1 --- /dev/null +++ b/app/views/admin/items/notes/create.turbo_stream.erb @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/app/views/admin/items/notes/destroy.html.erb b/app/views/admin/items/notes/destroy.html.erb new file mode 100644 index 000000000..fd05290ab --- /dev/null +++ b/app/views/admin/items/notes/destroy.html.erb @@ -0,0 +1 @@ +<%= turbo_frame_tag @note do %><% end %> \ No newline at end of file diff --git a/app/views/admin/items/notes/edit.html.erb b/app/views/admin/items/notes/edit.html.erb new file mode 100644 index 000000000..1a4719341 --- /dev/null +++ b/app/views/admin/items/notes/edit.html.erb @@ -0,0 +1,3 @@ +<%= turbo_frame_tag @note do %> + <%= render partial: "form", locals: {note: @note, item: @item} %> +<% end %> diff --git a/app/views/admin/items/notes/new.html.erb b/app/views/admin/items/notes/new.html.erb new file mode 100644 index 000000000..592928914 --- /dev/null +++ b/app/views/admin/items/notes/new.html.erb @@ -0,0 +1,3 @@ +<%= turbo_frame_tag "new-note" do %> + <%= render partial: "form", locals: {note: @note, item: @item} %> +<% end %> diff --git a/app/views/admin/items/notes/show.html.erb b/app/views/admin/items/notes/show.html.erb new file mode 100644 index 000000000..383612e25 --- /dev/null +++ b/app/views/admin/items/notes/show.html.erb @@ -0,0 +1 @@ +<%= render partial: "admin/items/notes/note", locals: { note: @note, item: @item } %> \ No newline at end of file diff --git a/app/views/admin/items/show.html.erb b/app/views/admin/items/show.html.erb index 6b7010c44..3eb518d06 100644 --- a/app/views/admin/items/show.html.erb +++ b/app/views/admin/items/show.html.erb @@ -12,5 +12,5 @@ <% end %> - <%= render partial: "admin/notes/list", locals: {parent: @item} %> + <%= render partial: "admin/items/notes/list", locals: { item: @item } %> <% end %> diff --git a/app/views/admin/members/_profile.html.erb b/app/views/admin/members/_profile.html.erb index 5dd6a3e9b..5108ab561 100644 --- a/app/views/admin/members/_profile.html.erb +++ b/app/views/admin/members/_profile.html.erb @@ -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 %> @@ -22,18 +22,9 @@ <% end %> +
- <% @member.notes.newest_first.each do |note| %> -
- <%= link_to "Edit Note", [:edit, :admin, @member, note], method: :get, class: "btn btn-sm edit-button" %> -
- <%= note.body -%> -
-

- - <%= note.creator.member.preferred_name %>, <%= time_ago_in_words(note.created_at) %> ago -

-
- <% end %> + <%= render partial: "admin/members/notes/list", locals: { member: @member } %>