Skip to content

Commit

Permalink
Admin adjustment reasons with turbo-frame link and turbo template
Browse files Browse the repository at this point in the history
  • Loading branch information
chaimann authored and tvdeyen committed Dec 20, 2024
1 parent 7a56f2b commit 619632e
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@
<% end %>
<% end %>
<% end %>
<%= render component("adjustment_reasons/index").new(page: @page) %>
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ def page_actions
render component("ui/button").new(
tag: :a,
text: t('.add'),
href: solidus_admin.new_adjustment_reason_path, data: { turbo_frame: :new_adjustment_reason_modal },
href: solidus_admin.new_adjustment_reason_path, data: {
turbo_frame: :new_adjustment_reason_modal,
turbo_prefetch: false
},
icon: "add-line",
class: "align-self-end w-full",
)
Expand All @@ -30,8 +33,8 @@ def turbo_frames
]
end

def row_url(adjustment_reason)
spree.edit_admin_adjustment_reason_path(adjustment_reason, _turbo_frame: :edit_adjustment_reason_modal)
def edit_path(adjustment_reason)
spree.edit_admin_adjustment_reason_path(adjustment_reason)
end

def batch_actions
Expand All @@ -47,8 +50,22 @@ def batch_actions

def columns
[
:name,
:code,
{
header: :name,
data: ->(adjustment_reason) do
link_to adjustment_reason.name, edit_path(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, edit_path(adjustment_reason),
class: 'body-link',
data: { turbo_frame: :edit_adjustment_reason_modal, turbo_prefetch: false }
end
},
{
header: :active,
data: ->(adjustment_reason) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,3 @@
<% end %>
<% end %>
<% end %>

<%= render component("adjustment_reasons/index").new(page: @page) %>
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# frozen_string_literal: true

class SolidusAdmin::AdjustmentReasons::New::Component < SolidusAdmin::BaseComponent
def initialize(page:, adjustment_reason:)
@page = page
def initialize(adjustment_reason:)
@adjustment_reason = adjustment_reason
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -17,10 +19,8 @@ def index
def new
@adjustment_reason = Spree::AdjustmentReason.new

set_index_page

respond_to do |format|
format.html { render component('adjustment_reasons/new').new(page: @page, adjustment_reason: @adjustment_reason) }
format.html { render component('adjustment_reasons/new').new(adjustment_reason: @adjustment_reason) }
end
end

Expand All @@ -40,22 +40,20 @@ def create
end
end
else
set_index_page

respond_to do |format|
format.html do
page_component = component('adjustment_reasons/new').new(page: @page, adjustment_reason: @adjustment_reason)
page_component = component('adjustment_reasons/new').new(adjustment_reason: @adjustment_reason)
render page_component, status: :unprocessable_entity
end
end
end
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)
end
end
end

Expand All @@ -73,11 +71,9 @@ def update
end
end
else
set_index_page

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
Expand Down
1 change: 1 addition & 0 deletions admin/app/controllers/solidus_admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion admin/spec/requests/solidus_admin/adjustment_reasons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 619632e

Please sign in to comment.