From 323dafe67ef8496bc379310095706bf7b94ecfe7 Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Thu, 19 Dec 2024 21:02:11 +0100 Subject: [PATCH 1/3] wip: adjustment reason edit with turbo-frame link and turbo template with no index --- .../edit/component.html.erb | 1 - .../adjustment_reasons/edit/component.rb | 3 +-- .../adjustment_reasons/index/component.rb | 18 ++++++++++++-- .../adjustment_reasons_controller.rb | 10 ++++---- .../solidus_admin/base_controller.rb | 1 + .../controller_helpers/turbo_actions.rb | 24 +++++++++++++++++++ .../solidus_admin/adjustment_reasons_spec.rb | 7 +++++- 7 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 admin/app/controllers/solidus_admin/controller_helpers/turbo_actions.rb diff --git a/admin/app/components/solidus_admin/adjustment_reasons/edit/component.html.erb b/admin/app/components/solidus_admin/adjustment_reasons/edit/component.html.erb index 2ffda1726c3..e69772be33d 100644 --- a/admin/app/components/solidus_admin/adjustment_reasons/edit/component.html.erb +++ b/admin/app/components/solidus_admin/adjustment_reasons/edit/component.html.erb @@ -24,4 +24,3 @@ <% end %> <% end %> <% end %> -<%= render component("adjustment_reasons/index").new(page: @page) %> diff --git a/admin/app/components/solidus_admin/adjustment_reasons/edit/component.rb b/admin/app/components/solidus_admin/adjustment_reasons/edit/component.rb index e2005a5fb23..fed30701a0d 100644 --- a/admin/app/components/solidus_admin/adjustment_reasons/edit/component.rb +++ b/admin/app/components/solidus_admin/adjustment_reasons/edit/component.rb @@ -1,8 +1,7 @@ # frozen_string_literal: true class SolidusAdmin::AdjustmentReasons::Edit::Component < SolidusAdmin::BaseComponent - def initialize(page:, adjustment_reason:) - @page = page + def initialize(adjustment_reason:) @adjustment_reason = adjustment_reason end diff --git a/admin/app/components/solidus_admin/adjustment_reasons/index/component.rb b/admin/app/components/solidus_admin/adjustment_reasons/index/component.rb index 647d97ecc82..b7503b75749 100644 --- a/admin/app/components/solidus_admin/adjustment_reasons/index/component.rb +++ b/admin/app/components/solidus_admin/adjustment_reasons/index/component.rb @@ -47,8 +47,22 @@ def batch_actions def columns [ - :name, - :code, + { + header: :name, + data: ->(adjustment_reason) do + link_to adjustment_reason.name, row_url(adjustment_reason), + class: 'body-link', + data: { turbo_frame: :edit_adjustment_reason_modal, turbo_prefetch: false } + end + }, + { + header: :code, + data: ->(adjustment_reason) do + link_to adjustment_reason.code, row_url(adjustment_reason), + class: 'body-link', + data: { turbo_frame: :edit_adjustment_reason_modal, turbo_prefetch: false } + end + }, { header: :active, data: ->(adjustment_reason) do diff --git a/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb b/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb index 64793387604..5e5939f18fb 100644 --- a/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb +++ b/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb @@ -6,6 +6,8 @@ class AdjustmentReasonsController < SolidusAdmin::BaseController before_action :set_adjustment_reason, only: %i[edit update] + turbo_actions :edit + def index set_index_page @@ -52,10 +54,10 @@ def create end def edit - set_index_page - respond_to do |format| - format.html { render component('adjustment_reasons/edit').new(page: @page, adjustment_reason: @adjustment_reason) } + format.html do + render component('adjustment_reasons/edit').new(adjustment_reason: @adjustment_reason), layout: false + end end end @@ -77,7 +79,7 @@ def update respond_to do |format| format.html do - page_component = component('adjustment_reasons/edit').new(page: @page, adjustment_reason: @adjustment_reason) + page_component = component('adjustment_reasons/edit').new(adjustment_reason: @adjustment_reason) render page_component, status: :unprocessable_entity end end diff --git a/admin/app/controllers/solidus_admin/base_controller.rb b/admin/app/controllers/solidus_admin/base_controller.rb index 44665e05295..af36f8df762 100644 --- a/admin/app/controllers/solidus_admin/base_controller.rb +++ b/admin/app/controllers/solidus_admin/base_controller.rb @@ -12,6 +12,7 @@ class BaseController < ApplicationController include SolidusAdmin::ControllerHelpers::Authorization include SolidusAdmin::ControllerHelpers::Locale include SolidusAdmin::ControllerHelpers::Theme + include SolidusAdmin::ControllerHelpers::TurboActions include SolidusAdmin::ComponentsHelper include SolidusAdmin::AuthenticationAdapters::Backend if defined?(Spree::Backend) diff --git a/admin/app/controllers/solidus_admin/controller_helpers/turbo_actions.rb b/admin/app/controllers/solidus_admin/controller_helpers/turbo_actions.rb new file mode 100644 index 00000000000..24805c2da9b --- /dev/null +++ b/admin/app/controllers/solidus_admin/controller_helpers/turbo_actions.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module SolidusAdmin::ControllerHelpers::TurboActions + extend ActiveSupport::Concern + + included do + class_attribute :registered_turbo_actions, instance_writer: false, default: [] + before_action :ensure_turbo_frame_request, if: ->(controller) do + registered_turbo_actions.include?(controller.action_name.to_sym) + end + end + + class_methods do + def turbo_actions(*actions) + self.registered_turbo_actions += actions.map(&:to_sym) + end + end + + private + + def ensure_turbo_frame_request + redirect_to action: :index unless turbo_frame_request? + end +end diff --git a/admin/spec/requests/solidus_admin/adjustment_reasons_spec.rb b/admin/spec/requests/solidus_admin/adjustment_reasons_spec.rb index 1f4a8bd7ccb..44e6d17ccfc 100644 --- a/admin/spec/requests/solidus_admin/adjustment_reasons_spec.rb +++ b/admin/spec/requests/solidus_admin/adjustment_reasons_spec.rb @@ -64,8 +64,13 @@ end describe "GET /edit" do - it "renders the edit template with a 200 OK status" do + it "redirects when the request is not Turbo-Frame" do get solidus_admin.edit_adjustment_reason_path(adjustment_reason) + expect(response).to have_http_status(:redirect) + end + + it "renders the edit template with a 200 OK status for Turbo-Frame request" do + get solidus_admin.edit_adjustment_reason_path(adjustment_reason), headers: { "Turbo-Frame": :edit_adjustment_reason_modal } expect(response).to have_http_status(:ok) end end From 1346608faf935c66dde31323791cbf5a378cdbdd Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Fri, 20 Dec 2024 19:45:02 +0100 Subject: [PATCH 2/3] wip: revert to allow basic edit route visit --- .../adjustment_reasons_controller.rb | 8 ++++--- .../solidus_admin/base_controller.rb | 1 - .../controller_helpers/turbo_actions.rb | 24 ------------------- .../solidus_admin/adjustment_reasons_spec.rb | 4 ++-- 4 files changed, 7 insertions(+), 30 deletions(-) delete mode 100644 admin/app/controllers/solidus_admin/controller_helpers/turbo_actions.rb diff --git a/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb b/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb index 5e5939f18fb..f8d2df1ecba 100644 --- a/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb +++ b/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb @@ -6,8 +6,6 @@ class AdjustmentReasonsController < SolidusAdmin::BaseController before_action :set_adjustment_reason, only: %i[edit update] - turbo_actions :edit - def index set_index_page @@ -56,7 +54,11 @@ def create def edit respond_to do |format| format.html do - render component('adjustment_reasons/edit').new(adjustment_reason: @adjustment_reason), layout: false + if turbo_frame_request? + render component('adjustment_reasons/edit').new(adjustment_reason: @adjustment_reason), layout: false + else + set_index_page + end end end end diff --git a/admin/app/controllers/solidus_admin/base_controller.rb b/admin/app/controllers/solidus_admin/base_controller.rb index af36f8df762..44665e05295 100644 --- a/admin/app/controllers/solidus_admin/base_controller.rb +++ b/admin/app/controllers/solidus_admin/base_controller.rb @@ -12,7 +12,6 @@ class BaseController < ApplicationController include SolidusAdmin::ControllerHelpers::Authorization include SolidusAdmin::ControllerHelpers::Locale include SolidusAdmin::ControllerHelpers::Theme - include SolidusAdmin::ControllerHelpers::TurboActions include SolidusAdmin::ComponentsHelper include SolidusAdmin::AuthenticationAdapters::Backend if defined?(Spree::Backend) diff --git a/admin/app/controllers/solidus_admin/controller_helpers/turbo_actions.rb b/admin/app/controllers/solidus_admin/controller_helpers/turbo_actions.rb deleted file mode 100644 index 24805c2da9b..00000000000 --- a/admin/app/controllers/solidus_admin/controller_helpers/turbo_actions.rb +++ /dev/null @@ -1,24 +0,0 @@ -# frozen_string_literal: true - -module SolidusAdmin::ControllerHelpers::TurboActions - extend ActiveSupport::Concern - - included do - class_attribute :registered_turbo_actions, instance_writer: false, default: [] - before_action :ensure_turbo_frame_request, if: ->(controller) do - registered_turbo_actions.include?(controller.action_name.to_sym) - end - end - - class_methods do - def turbo_actions(*actions) - self.registered_turbo_actions += actions.map(&:to_sym) - end - end - - private - - def ensure_turbo_frame_request - redirect_to action: :index unless turbo_frame_request? - end -end diff --git a/admin/spec/requests/solidus_admin/adjustment_reasons_spec.rb b/admin/spec/requests/solidus_admin/adjustment_reasons_spec.rb index 44e6d17ccfc..f415f357c4e 100644 --- a/admin/spec/requests/solidus_admin/adjustment_reasons_spec.rb +++ b/admin/spec/requests/solidus_admin/adjustment_reasons_spec.rb @@ -64,9 +64,9 @@ end describe "GET /edit" do - it "redirects when the request is not Turbo-Frame" do + it "renders the edit template with a 200 OK status" do get solidus_admin.edit_adjustment_reason_path(adjustment_reason) - expect(response).to have_http_status(:redirect) + expect(response).to have_http_status(:ok) end it "renders the edit template with a 200 OK status for Turbo-Frame request" do From 542eaa1cb0a8b6ff25f6500d010639553e4f98b6 Mon Sep 17 00:00:00 2001 From: Eugene Chaikin Date: Fri, 20 Dec 2024 21:28:45 +0100 Subject: [PATCH 3/3] wip: eager load corresponding turbo-frame on edit path visit --- .../edit/component.html.erb | 2 +- .../adjustment_reasons/index/component.rb | 23 ++++++++++++++----- .../adjustment_reasons/new/component.html.erb | 2 +- .../ui/pages/index/component.html.erb | 2 +- .../utils/turbo_frame/component.html.erb | 1 + .../utils/turbo_frame/component.rb | 9 ++++++++ .../adjustment_reasons_controller.rb | 3 +++ .../solidus_admin/base_controller.rb | 1 + .../solidus_admin/adjustment_reasons_spec.rb | 2 +- 9 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 admin/app/components/solidus_admin/utils/turbo_frame/component.html.erb create mode 100644 admin/app/components/solidus_admin/utils/turbo_frame/component.rb diff --git a/admin/app/components/solidus_admin/adjustment_reasons/edit/component.html.erb b/admin/app/components/solidus_admin/adjustment_reasons/edit/component.html.erb index e69772be33d..301567e3bb8 100644 --- a/admin/app/components/solidus_admin/adjustment_reasons/edit/component.html.erb +++ b/admin/app/components/solidus_admin/adjustment_reasons/edit/component.html.erb @@ -1,4 +1,4 @@ -<%= turbo_frame_tag :edit_adjustment_reason_modal do %> +<%= turbo_frame_tag @adjustment_reason, :edit, target: '_top' do %> <%= render component("ui/modal").new(title: t(".title")) do |modal| %> <%= form_for @adjustment_reason, url: solidus_admin.adjustment_reason_path(@adjustment_reason), html: { id: form_id } do |f| %>
diff --git a/admin/app/components/solidus_admin/adjustment_reasons/index/component.rb b/admin/app/components/solidus_admin/adjustment_reasons/index/component.rb index b7503b75749..05e41863bf2 100644 --- a/admin/app/components/solidus_admin/adjustment_reasons/index/component.rb +++ b/admin/app/components/solidus_admin/adjustment_reasons/index/component.rb @@ -24,14 +24,18 @@ def page_actions end def turbo_frames - %w[ - new_adjustment_reason_modal - edit_adjustment_reason_modal + return @turbo_frames if defined? @turbo_frames + + @turbo_frames = [ + component('utils/turbo_frame').new(id: dom_id(Spree::AdjustmentReason, :new)), ] + + @page.records.each { @turbo_frames << component('utils/turbo_frame').new(id: dom_id(_1, :edit)) } + @turbo_frames end def row_url(adjustment_reason) - spree.edit_admin_adjustment_reason_path(adjustment_reason, _turbo_frame: :edit_adjustment_reason_modal) + spree.edit_admin_adjustment_reason_path(adjustment_reason, _turbo_frame: dom_id(adjustment_reason, :edit)) end def batch_actions @@ -52,7 +56,7 @@ def columns data: ->(adjustment_reason) do link_to adjustment_reason.name, row_url(adjustment_reason), class: 'body-link', - data: { turbo_frame: :edit_adjustment_reason_modal, turbo_prefetch: false } + data: { turbo_frame: dom_id(adjustment_reason, :edit), turbo_prefetch: false } end }, { @@ -60,7 +64,7 @@ def columns data: ->(adjustment_reason) do link_to adjustment_reason.code, row_url(adjustment_reason), class: 'body-link', - data: { turbo_frame: :edit_adjustment_reason_modal, turbo_prefetch: false } + data: { turbo_frame: dom_id(adjustment_reason, :edit), turbo_prefetch: false } end }, { @@ -71,4 +75,11 @@ def columns }, ] end + + def eager_loaded_frame(frame_id, src) + frame = turbo_frames.index_by(&:id)[frame_id] + return unless frame + + frame.src = src + end end diff --git a/admin/app/components/solidus_admin/adjustment_reasons/new/component.html.erb b/admin/app/components/solidus_admin/adjustment_reasons/new/component.html.erb index f30e26730cd..28947deeb99 100644 --- a/admin/app/components/solidus_admin/adjustment_reasons/new/component.html.erb +++ b/admin/app/components/solidus_admin/adjustment_reasons/new/component.html.erb @@ -1,4 +1,4 @@ -<%= turbo_frame_tag :new_adjustment_reason_modal do %> +<%= turbo_frame_tag :new_adjustment_reason do %> <%= render component("ui/modal").new(title: t(".title")) do |modal| %> <%= form_for @adjustment_reason, url: solidus_admin.adjustment_reasons_path, html: { id: form_id } do |f| %>
diff --git a/admin/app/components/solidus_admin/ui/pages/index/component.html.erb b/admin/app/components/solidus_admin/ui/pages/index/component.html.erb index 69916f08dd4..efd20b24e01 100644 --- a/admin/app/components/solidus_admin/ui/pages/index/component.html.erb +++ b/admin/app/components/solidus_admin/ui/pages/index/component.html.erb @@ -39,6 +39,6 @@ <% end %> <% turbo_frames.each do |frame| %> - <%= turbo_frame_tag frame %> + <%= render frame %> <% end %> <% end %> diff --git a/admin/app/components/solidus_admin/utils/turbo_frame/component.html.erb b/admin/app/components/solidus_admin/utils/turbo_frame/component.html.erb new file mode 100644 index 00000000000..8dea1e0df62 --- /dev/null +++ b/admin/app/components/solidus_admin/utils/turbo_frame/component.html.erb @@ -0,0 +1 @@ +<%= turbo_frame_tag(@id, src: @src) %> diff --git a/admin/app/components/solidus_admin/utils/turbo_frame/component.rb b/admin/app/components/solidus_admin/utils/turbo_frame/component.rb new file mode 100644 index 00000000000..22d84bc86dd --- /dev/null +++ b/admin/app/components/solidus_admin/utils/turbo_frame/component.rb @@ -0,0 +1,9 @@ +class SolidusAdmin::Utils::TurboFrame::Component < SolidusAdmin::BaseComponent + attr_reader :id + attr_writer :src + + def initialize(id:, src: nil) + @id = id + @src = src + end +end diff --git a/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb b/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb index f8d2df1ecba..84c3856f857 100644 --- a/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb +++ b/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb @@ -58,6 +58,9 @@ def edit render component('adjustment_reasons/edit').new(adjustment_reason: @adjustment_reason), layout: false else set_index_page + component = component('adjustment_reasons/index').new(page: @page) + component.eager_loaded_frame(dom_id(@adjustment_reason, :edit), solidus_admin.edit_adjustment_reason_path(@adjustment_reason)) + render component end end end diff --git a/admin/app/controllers/solidus_admin/base_controller.rb b/admin/app/controllers/solidus_admin/base_controller.rb index 44665e05295..a0a3f3605e3 100644 --- a/admin/app/controllers/solidus_admin/base_controller.rb +++ b/admin/app/controllers/solidus_admin/base_controller.rb @@ -7,6 +7,7 @@ class BaseController < ApplicationController include ActiveStorage::SetCurrent include Spree::Core::ControllerHelpers::Store include GearedPagination::Controller + include ActionView::RecordIdentifier include SolidusAdmin::ControllerHelpers::Authentication include SolidusAdmin::ControllerHelpers::Authorization diff --git a/admin/spec/requests/solidus_admin/adjustment_reasons_spec.rb b/admin/spec/requests/solidus_admin/adjustment_reasons_spec.rb index f415f357c4e..951c6e7aa3e 100644 --- a/admin/spec/requests/solidus_admin/adjustment_reasons_spec.rb +++ b/admin/spec/requests/solidus_admin/adjustment_reasons_spec.rb @@ -70,7 +70,7 @@ end it "renders the edit template with a 200 OK status for Turbo-Frame request" do - get solidus_admin.edit_adjustment_reason_path(adjustment_reason), headers: { "Turbo-Frame": :edit_adjustment_reason_modal } + get solidus_admin.edit_adjustment_reason_path(adjustment_reason), headers: { "Turbo-Frame": "adjustment_reason_#{adjustment_reason.id}_edit" } expect(response).to have_http_status(:ok) end end