Skip to content

Commit

Permalink
wip: adjustment reason edit with turbo-frame link and turbo template …
Browse files Browse the repository at this point in the history
…with no index
  • Loading branch information
chaimann committed Dec 19, 2024
1 parent 89730ee commit 323dafe
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 10 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 @@ -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
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 Down Expand Up @@ -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

Expand All @@ -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
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 323dafe

Please sign in to comment.