<%= render component("ui/forms/field").select(
f,
@@ -19,4 +19,3 @@
<% end %>
<% end %>
<% end %>
-<%= render component("users/store_credits/show").new(user: @user, store_credit: @store_credit, events: @store_credit_events) %>
diff --git a/admin/app/components/solidus_admin/users/store_credits/edit_validity/component.rb b/admin/app/components/solidus_admin/users/store_credits/edit_validity/component.rb
index 9302d56fbd8..c1bacfc26d3 100644
--- a/admin/app/components/solidus_admin/users/store_credits/edit_validity/component.rb
+++ b/admin/app/components/solidus_admin/users/store_credits/edit_validity/component.rb
@@ -1,10 +1,9 @@
# frozen_string_literal: true
-class SolidusAdmin::Users::StoreCredits::EditValidity::Component < SolidusAdmin::BaseComponent
- def initialize(user:, store_credit:, events:, reasons:)
+class SolidusAdmin::Users::StoreCredits::EditValidity::Component < SolidusAdmin::Resources::Edit::Component
+ def initialize(user:, store_credit:, reasons:)
@user = user
- @store_credit = store_credit
- @store_credit_events = events
+ super(store_credit)
@store_credit_reasons = reasons
end
@@ -12,6 +11,10 @@ def form_id
dom_id(@store_credit, "#{stimulus_id}_edit_validity_form")
end
+ def form_url
+ solidus_admin.invalidate_user_store_credit_path(@user, @store_credit, **search_filter_params)
+ end
+
def store_credit_reasons_select_options
# Placeholder + Store Credit Reasons
"
" + options_from_collection_for_select(@store_credit_reasons, :id, :name)
diff --git a/admin/app/components/solidus_admin/users/store_credits/index/component.html.erb b/admin/app/components/solidus_admin/users/store_credits/index/component.html.erb
index d5eefee139e..06e06e1a1bd 100644
--- a/admin/app/components/solidus_admin/users/store_credits/index/component.html.erb
+++ b/admin/app/components/solidus_admin/users/store_credits/index/component.html.erb
@@ -5,9 +5,13 @@
<%= page_header_actions do %>
<%= render component("ui/button").new(
- "data-action": "click->#{stimulus_id}#actionButtonClicked",
- "data-#{stimulus_id}-url-param": solidus_admin.new_user_store_credit_path(user_id: @user.id, _turbo_frame: :new_store_credit_modal),
+ tag: :a,
+ href: new_store_credit_path,
+ data: {
+ turbo_frame: :resource_modal
+ },
text: t(".add_store_credit"),
+ icon: "add-line"
)%>
<% end %>
<% end %>
@@ -27,23 +31,25 @@
<%= render component('ui/panel').new(title: title) do %>
<%= render component('ui/table').new(
- id: stimulus_id,
- data: {
- class: model_class,
- rows: credits,
- fade: -> (_order) { false },
- columns: columns,
- url: -> { row_url(_1) },
- },
- )%>
+ id: stimulus_id,
+ data: {
+ class: model_class,
+ rows: credits,
+ fade: -> (_order) { false },
+ columns: columns
+ }
+ ) %>
<% end %>
<% end %>
<% else %>
<%= render component('ui/panel').new(title: t(".store_credit")) do %>
<%= t(".no_credits_found") %>
<%= render component("ui/button").new(
- "data-action": "click->#{stimulus_id}#actionButtonClicked",
- "data-#{stimulus_id}-url-param": solidus_admin.new_user_store_credit_path(user_id: @user.id, _turbo_frame: :new_store_credit_modal),
+ tag: :a,
+ data: {
+ turbo_frame: :resource_modal
+ },
+ href: new_store_credit_path,
text: t(".create_one"),
)%>
<% end %>
@@ -55,7 +61,5 @@
<% end %>
<% end %>
- <% turbo_frames.each do |frame| %>
- <%= turbo_frame_tag frame %>
- <% end %>
+ <%= turbo_frame_tag :resource_modal, target: "_top" %>
<% end %>
diff --git a/admin/app/components/solidus_admin/users/store_credits/index/component.js b/admin/app/components/solidus_admin/users/store_credits/index/component.js
deleted file mode 100644
index 18d886a69ab..00000000000
--- a/admin/app/components/solidus_admin/users/store_credits/index/component.js
+++ /dev/null
@@ -1,15 +0,0 @@
-import { Controller } from "@hotwired/stimulus"
-
-export default class extends Controller {
- actionButtonClicked(event) {
- const url = new URL(event.params.url, "http://dummy.com")
- const params = new URLSearchParams(url.search)
- const frameId = params.get("_turbo_frame")
- const frame = frameId ? { frame: frameId } : {}
- // remove the custom _turbo_frame param from url search:
- params.delete("_turbo_frame")
- url.search = params.toString()
-
- window.Turbo.visit(url.pathname + url.search, frame)
- }
-}
diff --git a/admin/app/components/solidus_admin/users/store_credits/index/component.rb b/admin/app/components/solidus_admin/users/store_credits/index/component.rb
index 6e961826a15..f67f7b7c82b 100644
--- a/admin/app/components/solidus_admin/users/store_credits/index/component.rb
+++ b/admin/app/components/solidus_admin/users/store_credits/index/component.rb
@@ -42,41 +42,39 @@ def tabs
]
end
- def turbo_frames
- %w[
- new_store_credit_modal
- ]
- end
-
def rows
@store_credits
end
- def row_url(store_credit)
+ def show_path(store_credit)
solidus_admin.user_store_credit_path(@user, store_credit)
end
+ def new_store_credit_path
+ solidus_admin.new_user_store_credit_path(user_id: @user.id)
+ end
+
def columns
[
{
header: :credited,
col: { class: "w-[12%]" },
data: ->(store_credit) do
- content_tag :div, store_credit.display_amount.to_html, class: "text-sm"
+ link_to store_credit.display_amount.to_html, show_path(store_credit), class: "body-link text-sm"
end
},
{
header: :authorized,
col: { class: "w-[13%]" },
data: ->(store_credit) do
- content_tag :div, store_credit.display_amount_authorized.to_html, class: "text-sm"
+ link_to store_credit.display_amount_authorized.to_html, show_path(store_credit), class: "body-link text-sm"
end
},
{
header: :used,
col: { class: "w-[9%]" },
data: ->(store_credit) do
- content_tag :div, store_credit.display_amount_used.to_html, class: "text-sm"
+ link_to store_credit.display_amount_used.to_html, show_path(store_credit), class: "body-link text-sm"
end
},
{
@@ -90,23 +88,27 @@ def columns
header: :created_by,
col: { class: "w-[22%]" },
data: ->(store_credit) do
- content_tag :div, store_credit.created_by_email, class: "font-semibold text-sm"
+ content_tag :div, store_credit.created_by_email, class: "text-sm"
end
},
{
header: :issued_on,
col: { class: "w-[16%]" },
data: ->(store_credit) do
- I18n.l(store_credit.created_at.to_date)
+ content_tag :span, I18n.l(store_credit.created_at.to_date), class: "text-sm"
end
},
{
header: :invalidated,
col: { class: "w-[15%]" },
data: ->(store_credit) do
- store_credit.invalidated? ? component('ui/badge').new(name: :yes, color: :red, size: :m) : component('ui/badge').new(name: :no, color: :green, size: :m)
+ if store_credit.invalidated?
+ component('ui/badge').new(name: :yes, color: :red, size: :m)
+ else
+ component('ui/badge').new(name: :no, color: :green, size: :m)
+ end
end
- },
+ }
]
end
end
diff --git a/admin/app/components/solidus_admin/users/store_credits/new/component.html.erb b/admin/app/components/solidus_admin/users/store_credits/new/component.html.erb
index d90ebf1c124..df37ce9b238 100644
--- a/admin/app/components/solidus_admin/users/store_credits/new/component.html.erb
+++ b/admin/app/components/solidus_admin/users/store_credits/new/component.html.erb
@@ -1,6 +1,6 @@
-<%= turbo_frame_tag :new_store_credit_modal do %>
+<%= turbo_frame_tag :resource_modal, target: "_top" do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
- <%= form_for @store_credit, url: solidus_admin.user_store_credits_path(@user), method: :post, html: { id: form_id } do |f| %>
+ <%= form_for @store_credit, url: form_url, method: :post, html: { id: form_id } do |f| %>
<%= render component("ui/forms/field").text_field(f, :amount, class: "required") %>
<%= render component("ui/forms/field").select(
@@ -28,4 +28,3 @@
<% end %>
<% end %>
<% end %>
-<%= render component("users/store_credits/index").new(user: @user, store_credits: @store_credits) %>
diff --git a/admin/app/components/solidus_admin/users/store_credits/new/component.rb b/admin/app/components/solidus_admin/users/store_credits/new/component.rb
index afecb8b683e..5f3d0945d59 100644
--- a/admin/app/components/solidus_admin/users/store_credits/new/component.rb
+++ b/admin/app/components/solidus_admin/users/store_credits/new/component.rb
@@ -1,15 +1,14 @@
# frozen_string_literal: true
-class SolidusAdmin::Users::StoreCredits::New::Component < SolidusAdmin::BaseComponent
+class SolidusAdmin::Users::StoreCredits::New::Component < SolidusAdmin::Resources::New::Component
def initialize(user:, store_credit:, categories:)
@user = user
- @store_credit = store_credit
+ super(store_credit)
@store_credit_categories = categories
- @store_credits = Spree::StoreCredit.where(user_id: @user.id).order(id: :desc)
end
- def form_id
- dom_id(@store_credit, "#{stimulus_id}_new_form")
+ def form_url
+ solidus_admin.user_store_credits_path(@user, **search_filter_params)
end
def currency_select_options
diff --git a/admin/app/components/solidus_admin/users/store_credits/show/component.html.erb b/admin/app/components/solidus_admin/users/store_credits/show/component.html.erb
index 066414def55..239faac3233 100644
--- a/admin/app/components/solidus_admin/users/store_credits/show/component.html.erb
+++ b/admin/app/components/solidus_admin/users/store_credits/show/component.html.erb
@@ -28,25 +28,34 @@
<% if @store_credit.invalidateable? %>
<%= render component("ui/button").new(
+ tag: :a,
scheme: :danger,
- "data-action": "click->#{stimulus_id}#actionButtonClicked",
- "data-#{stimulus_id}-url-param": solidus_admin.edit_validity_user_store_credit_path(@user, @store_credit, _turbo_frame: :edit_validity_modal),
+ data: {
+ turbo_frame: :resource_modal
+ },
+ href: edit_validity_url,
text: t(".invalidate"),
- )%>
+ ) %>
<% end %>
<%= render component("ui/button").new(
- "data-action": "click->#{stimulus_id}#actionButtonClicked",
- "data-#{stimulus_id}-url-param": solidus_admin.edit_memo_user_store_credit_path(@user, @store_credit, _turbo_frame: :edit_memo_modal),
+ tag: :a,
+ data: {
+ turbo_frame: :resource_modal
+ },
+ href: edit_memo_url,
text: t(".edit_memo"),
- )%>
+ ) %>
<% if @store_credit.editable? %>
<%= render component("ui/button").new(
- "data-action": "click->#{stimulus_id}#actionButtonClicked",
- "data-#{stimulus_id}-url-param": solidus_admin.edit_amount_user_store_credit_path(@user, @store_credit, _turbo_frame: :edit_amount_modal),
+ tag: :a,
+ data: {
+ turbo_frame: :resource_modal
+ },
+ href: edit_amount_url,
text: t(".edit_amount"),
- )%>
+ ) %>
<% end %>
<% end %>
@@ -60,7 +69,6 @@
class: event_model_class,
rows: @events,
columns: columns,
- url: -> { row_url(_1) },
},
)%>
<% end %>
@@ -71,7 +79,5 @@
<% end %>
<% end %>
- <% turbo_frames.each do |frame| %>
- <%= turbo_frame_tag frame %>
- <% end %>
+ <%= turbo_frame_tag :resource_modal, target: "_top" %>
<% end %>
diff --git a/admin/app/components/solidus_admin/users/store_credits/show/component.js b/admin/app/components/solidus_admin/users/store_credits/show/component.js
deleted file mode 100644
index 18d886a69ab..00000000000
--- a/admin/app/components/solidus_admin/users/store_credits/show/component.js
+++ /dev/null
@@ -1,15 +0,0 @@
-import { Controller } from "@hotwired/stimulus"
-
-export default class extends Controller {
- actionButtonClicked(event) {
- const url = new URL(event.params.url, "http://dummy.com")
- const params = new URLSearchParams(url.search)
- const frameId = params.get("_turbo_frame")
- const frame = frameId ? { frame: frameId } : {}
- // remove the custom _turbo_frame param from url search:
- params.delete("_turbo_frame")
- url.search = params.toString()
-
- window.Turbo.visit(url.pathname + url.search, frame)
- }
-}
diff --git a/admin/app/components/solidus_admin/users/store_credits/show/component.rb b/admin/app/components/solidus_admin/users/store_credits/show/component.rb
index 25ad16da705..033ad96c855 100644
--- a/admin/app/components/solidus_admin/users/store_credits/show/component.rb
+++ b/admin/app/components/solidus_admin/users/store_credits/show/component.rb
@@ -48,20 +48,20 @@ def tabs
]
end
- def turbo_frames
- %w[
- edit_amount_modal
- edit_memo_modal
- edit_validity_modal
- ]
- end
-
def form_id
@form_id ||= "#{stimulus_id}--form-#{@store_credit.id}"
end
- def row_url(_)
- "#"
+ def edit_validity_url
+ solidus_admin.edit_validity_user_store_credit_path(@user, @store_credit, **search_filter_params)
+ end
+
+ def edit_memo_url
+ solidus_admin.edit_memo_user_store_credit_path(@user, @store_credit, **search_filter_params)
+ end
+
+ def edit_amount_url
+ solidus_admin.edit_amount_user_store_credit_path(@user, @store_credit, **search_filter_params)
end
def columns
@@ -70,49 +70,49 @@ def columns
header: :date,
col: { class: "w-[15%]" },
data: ->(event) do
- content_tag :div, event.created_at.strftime("%b %d '%y %l:%M%P"), class: "text-sm"
+ content_tag :span, I18n.l(event.created_at, format: :short), class: "text-sm"
end
},
{
header: :action,
col: { class: "w-[10%]" },
data: ->(event) do
- content_tag :div, store_credit_event_admin_action_name(event), class: "text-sm"
+ content_tag :span, store_credit_event_admin_action_name(event), class: "text-sm"
end
},
{
header: :credited,
col: { class: "w-[10%]" },
data: ->(event) do
- content_tag :div, event.display_amount, class: "text-sm"
+ content_tag :span, event.display_amount, class: "text-sm"
end
},
{
header: :created_by,
col: { class: "w-[20%]" },
data: ->(event) do
- content_tag :div, store_credit_event_originator_link(event), class: "underline cursor-pointer text-sm"
+ content_tag :span, store_credit_event_originator_link(event), class: "body-link text-sm"
end
},
{
header: :total_amount,
col: { class: "w-[10%]" },
data: ->(event) do
- content_tag :div, event.display_user_total_amount, class: "text-sm"
+ content_tag :span, event.display_user_total_amount, class: "text-sm"
end
},
{
header: :total_unused,
col: { class: "w-[10%]" },
data: ->(event) do
- content_tag :div, event.display_remaining_amount, class: "text-sm"
+ content_tag :span, event.display_remaining_amount, class: "text-sm"
end
},
{
header: :reason_for_updating,
col: { class: "w-[25%]" },
data: ->(event) do
- content_tag :div, event.store_credit_reason&.name, class: "text-sm"
+ content_tag :span, event.store_credit_reason&.name, class: "text-sm"
end
},
]
diff --git a/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb b/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb
index 64793387604..365fd0aaa7f 100644
--- a/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb
+++ b/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb
@@ -1,115 +1,13 @@
# frozen_string_literal: true
module SolidusAdmin
- class AdjustmentReasonsController < SolidusAdmin::BaseController
- include SolidusAdmin::ControllerHelpers::Search
-
- before_action :set_adjustment_reason, only: %i[edit update]
-
- def index
- set_index_page
-
- respond_to do |format|
- format.html { render component('adjustment_reasons/index').new(page: @page) }
- end
- end
-
- 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) }
- end
- end
-
- def create
- @adjustment_reason = Spree::AdjustmentReason.new(adjustment_reason_params)
-
- if @adjustment_reason.save
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.adjustment_reasons_path, status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- 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)
- 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) }
- end
- end
-
- def update
- if @adjustment_reason.update(adjustment_reason_params)
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.adjustment_reasons_path, status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- 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)
- render page_component, status: :unprocessable_entity
- end
- end
- end
- end
-
- def destroy
- @adjustment_reason = Spree::AdjustmentReason.find_by!(id: params[:id])
-
- Spree::AdjustmentReason.transaction { @adjustment_reason.destroy }
-
- flash[:notice] = t('.success')
- redirect_back_or_to adjustment_reasons_path, status: :see_other
- end
-
+ class AdjustmentReasonsController < SolidusAdmin::ResourcesController
private
- def set_adjustment_reason
- @adjustment_reason = Spree::AdjustmentReason.find(params[:id])
- end
+ def resource_class = Spree::AdjustmentReason
- def adjustment_reason_params
+ def permitted_resource_params
params.require(:adjustment_reason).permit(:name, :code, :active)
end
-
- def set_index_page
- adjustment_reasons = apply_search_to(
- Spree::AdjustmentReason.order(id: :desc),
- param: :q,
- )
-
- set_page_and_extract_portion_from(adjustment_reasons)
- 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..bd7442f4eee 100644
--- a/admin/app/controllers/solidus_admin/base_controller.rb
+++ b/admin/app/controllers/solidus_admin/base_controller.rb
@@ -15,8 +15,19 @@ class BaseController < ApplicationController
include SolidusAdmin::ComponentsHelper
include SolidusAdmin::AuthenticationAdapters::Backend if defined?(Spree::Backend)
- layout 'solidus_admin/application'
+ layout :set_layout
+
helper 'solidus_admin/components'
helper 'solidus_admin/layout'
+
+ private
+
+ def set_layout
+ if turbo_frame_request?
+ 'turbo_rails/frame'
+ else
+ 'solidus_admin/application'
+ end
+ end
end
end
diff --git a/admin/app/controllers/solidus_admin/properties_controller.rb b/admin/app/controllers/solidus_admin/properties_controller.rb
index 53db315d8d6..f09d590ce81 100644
--- a/admin/app/controllers/solidus_admin/properties_controller.rb
+++ b/admin/app/controllers/solidus_admin/properties_controller.rb
@@ -1,117 +1,15 @@
# frozen_string_literal: true
module SolidusAdmin
- class PropertiesController < SolidusAdmin::BaseController
- include SolidusAdmin::ControllerHelpers::Search
-
- before_action :set_property, only: %i[edit update]
-
- def index
- set_index_page
-
- respond_to do |format|
- format.html { render component('properties/index').new(page: @page) }
- end
- end
-
- def new
- @property = Spree::Property.new
-
- set_index_page
-
- respond_to do |format|
- format.html { render component('properties/new').new(page: @page, property: @property) }
- end
- end
-
- def create
- @property = Spree::Property.new(property_params)
-
- if @property.save
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.properties_path, status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- end
- end
- else
- set_index_page
-
- respond_to do |format|
- format.html do
- page_component = component('properties/new').new(page: @page, property: @property)
- render page_component, status: :unprocessable_entity
- end
- end
- end
- end
-
- def edit
- set_index_page
-
- respond_to do |format|
- format.html { render component('properties/edit').new(page: @page, property: @property) }
- end
- end
-
- def update
- if @property.update(property_params)
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.properties_path, status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- end
- end
- else
- set_index_page
-
- respond_to do |format|
- format.html do
- page_component = component('properties/edit').new(page: @page, property: @property)
- render page_component, status: :unprocessable_entity
- end
- end
- end
- end
-
- def destroy
- @properties = Spree::Property.where(id: params[:id])
-
- Spree::Property.transaction do
- @properties.destroy_all
- end
-
- flash[:notice] = t('.success')
- redirect_to properties_path, status: :see_other
- end
-
+ class PropertiesController < SolidusAdmin::ResourcesController
private
- def set_property
- @property = Spree::Property.find(params[:id])
- end
+ def resource_class = Spree::Property
- def property_params
+ def permitted_resource_params
params.require(:property).permit(:name, :presentation)
end
- def set_index_page
- properties = apply_search_to(
- Spree::Property.unscoped.order(id: :desc),
- param: :q,
- )
-
- set_page_and_extract_portion_from(properties)
- end
+ def resources_collection = Spree::Property.unscoped
end
end
diff --git a/admin/app/controllers/solidus_admin/refund_reasons_controller.rb b/admin/app/controllers/solidus_admin/refund_reasons_controller.rb
index 92e8c5b2195..d26c18eac1b 100644
--- a/admin/app/controllers/solidus_admin/refund_reasons_controller.rb
+++ b/admin/app/controllers/solidus_admin/refund_reasons_controller.rb
@@ -1,115 +1,15 @@
# frozen_string_literal: true
module SolidusAdmin
- class RefundReasonsController < SolidusAdmin::BaseController
- include SolidusAdmin::ControllerHelpers::Search
-
- before_action :set_refund_reason, only: %i[edit update]
-
- def index
- set_index_page
-
- respond_to do |format|
- format.html { render component('refund_reasons/index').new(page: @page) }
- end
- end
-
- def new
- @refund_reason = Spree::RefundReason.new
-
- set_index_page
-
- respond_to do |format|
- format.html { render component('refund_reasons/new').new(page: @page, refund_reason: @refund_reason) }
- end
- end
-
- def create
- @refund_reason = Spree::RefundReason.new(refund_reason_params)
-
- if @refund_reason.save
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.refund_reasons_path, status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- end
- end
- else
- set_index_page
-
- respond_to do |format|
- format.html do
- page_component = component('refund_reasons/new').new(page: @page, refund_reason: @refund_reason)
- render page_component, status: :unprocessable_entity
- end
- end
- end
- end
-
- def edit
- set_index_page
-
- respond_to do |format|
- format.html { render component('refund_reasons/edit').new(page: @page, refund_reason: @refund_reason) }
- end
- end
-
- def update
- if @refund_reason.update(refund_reason_params)
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.refund_reasons_path, status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- end
- end
- else
- set_index_page
-
- respond_to do |format|
- format.html do
- page_component = component('refund_reasons/edit').new(page: @page, refund_reason: @refund_reason)
- render page_component, status: :unprocessable_entity
- end
- end
- end
- end
-
- def destroy
- @refund_reason = Spree::RefundReason.find_by!(id: params[:id])
-
- Spree::RefundReason.transaction { @refund_reason.destroy }
-
- flash[:notice] = t('.success')
- redirect_back_or_to refund_reasons_path, status: :see_other
- end
-
+ class RefundReasonsController < SolidusAdmin::ResourcesController
private
- def set_refund_reason
- @refund_reason = Spree::RefundReason.find(params[:id])
- end
-
- def refund_reason_params
- params.require(:refund_reason).permit(:name, :code, :active)
- end
+ def resource_class = Spree::RefundReason
- def set_index_page
- refund_reasons = apply_search_to(
- Spree::RefundReason.unscoped.order(id: :desc),
- param: :q,
- )
+ def resources_collection = Spree::RefundReason.unscoped
- set_page_and_extract_portion_from(refund_reasons)
+ def permitted_resource_params
+ params.require(:refund_reason).permit(:name, :code, :active)
end
end
end
diff --git a/admin/app/controllers/solidus_admin/resources_controller.rb b/admin/app/controllers/solidus_admin/resources_controller.rb
new file mode 100644
index 00000000000..b58399ab844
--- /dev/null
+++ b/admin/app/controllers/solidus_admin/resources_controller.rb
@@ -0,0 +1,152 @@
+# frozen_string_literal: true
+
+module SolidusAdmin
+ class ResourcesController < SolidusAdmin::BaseController
+ include SolidusAdmin::ControllerHelpers::Search
+
+ helper_method :search_filter_params
+
+ before_action :set_paginated_resources, only: %i[index]
+ before_action :set_resource, only: %i[edit update]
+
+ # GET /index
+ #
+ # Uses {set_paginated_resources} to set @resources
+ # and a instance variable with the plural name of the resource.
+ #
+ # Uses the geared_pagination gem to set @page for pagination.
+ #
+ # @see set_paginated_resources
+ # @see resources_collection
+ def index
+ respond_to do |format|
+ format.html { render index_component.new(page: @page) }
+ end
+ end
+
+ def new
+ @resource = resource_class.new
+ render new_component.new(@resource)
+ end
+
+ def create
+ @resource = resource_class.new(permitted_resource_params)
+
+ if @resource.save
+ flash[:notice] = t('.success')
+ redirect_to after_create_path, status: :see_other
+ else
+ page_component = new_component.new(@resource)
+ render_resource_form_with_errors(page_component)
+ end
+ end
+
+ def edit
+ render edit_component.new(@resource)
+ end
+
+ def update
+ if @resource.update(permitted_resource_params)
+ flash[:notice] = t('.success')
+ redirect_to after_update_path, status: :see_other
+ else
+ page_component = edit_component.new(@resource)
+ render_resource_form_with_errors(page_component)
+ end
+ end
+
+ def destroy
+ @resource = resource_class.find_by!(id: params[:id])
+
+ resource_class.transaction { @resource.destroy }
+
+ flash[:notice] = t('.success')
+ redirect_back_or_to after_destroy_path, status: :see_other
+ end
+
+ private
+
+ def search_filter_params
+ request.params.slice(:q, :page)
+ end
+
+ def set_paginated_resources
+ @resources ||= apply_search_to(
+ resources_collection,
+ param: :q,
+ ).tap do |resources|
+ instance_variable_set("@#{plural_resource_name}", resources)
+ # sets @page instance variable in geared_pagination gem
+ set_page_and_extract_portion_from(resources, ordered_by: resources_sorting_options)
+ end
+ end
+
+ def resources_sorting_options
+ { id: :desc }
+ end
+
+ def resources_collection
+ resource_class.all
+ end
+
+ def set_resource
+ @resource ||= resource_class.find(params[:id]).tap do |resource|
+ instance_variable_set("@#{resource_name}", resource)
+ end
+ end
+
+ def resource_class
+ raise NotImplementedError,
+ "You must implement the resource_class method in #{self.class}"
+ end
+
+ def resource_name
+ resource_class.model_name.singular_route_key
+ end
+
+ def plural_resource_name
+ resource_class.model_name.route_key
+ end
+
+ def index_component
+ component("#{plural_resource_name}/index")
+ end
+
+ def new_component
+ component("#{plural_resource_name}/new")
+ end
+
+ def edit_component
+ component("#{plural_resource_name}/edit")
+ end
+
+ def render_resource_form_with_errors(page_component)
+ respond_to do |format|
+ format.html do
+ render page_component, status: :unprocessable_entity
+ end
+ format.turbo_stream do
+ render turbo_stream: turbo_stream.replace(:resource_modal, page_component),
+ status: :unprocessable_entity
+ end
+ end
+ end
+
+ def permitted_resource_params
+ raise NotImplementedError,
+ "You must implement the permitted_resource_params method in #{self.class}"
+ end
+
+ def after_create_path
+ solidus_admin.send("#{plural_resource_name}_path", **search_filter_params)
+ end
+
+ def after_update_path
+ solidus_admin.send("#{plural_resource_name}_path", **search_filter_params)
+ end
+
+ def after_destroy_path
+ solidus_admin.send("#{plural_resource_name}_path", **search_filter_params)
+ end
+ end
+end
diff --git a/admin/app/controllers/solidus_admin/return_reasons_controller.rb b/admin/app/controllers/solidus_admin/return_reasons_controller.rb
index 892603806ca..ea09e3a4bfc 100644
--- a/admin/app/controllers/solidus_admin/return_reasons_controller.rb
+++ b/admin/app/controllers/solidus_admin/return_reasons_controller.rb
@@ -1,114 +1,14 @@
# frozen_string_literal: true
module SolidusAdmin
- class ReturnReasonsController < SolidusAdmin::BaseController
- include SolidusAdmin::ControllerHelpers::Search
-
- before_action :set_return_reason, only: %i[edit update]
-
- def index
- set_index_page
-
- respond_to do |format|
- format.html { render component('return_reasons/index').new(page: @page) }
- end
- end
-
- def new
- @return_reason = Spree::ReturnReason.new
-
- set_index_page
-
- respond_to do |format|
- format.html { render component('return_reasons/new').new(page: @page, return_reason: @return_reason) }
- end
- end
-
- def create
- @return_reason = Spree::ReturnReason.new(return_reason_params)
-
- if @return_reason.save
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.return_reasons_path, status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- end
- end
- else
- set_index_page
-
- respond_to do |format|
- format.html do
- page_component = component('return_reasons/new').new(page: @page, return_reason: @return_reason)
- render page_component, status: :unprocessable_entity
- end
- end
- end
- end
-
- def edit
- set_index_page
-
- respond_to do |format|
- format.html { render component('return_reasons/edit').new(page: @page, return_reason: @return_reason) }
- end
- end
-
- def update
- if @return_reason.update(return_reason_params)
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.return_reasons_path, status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- end
- end
- else
- set_index_page
-
- respond_to do |format|
- format.html do
- page_component = component('return_reasons/edit').new(page: @page, return_reason: @return_reason)
- render page_component, status: :unprocessable_entity
- end
- end
- end
- end
-
- def destroy
- @return_reason = Spree::ReturnReason.find_by!(id: params[:id])
-
- Spree::ReturnReason.transaction { @return_reason.destroy }
-
- flash[:notice] = t('.success')
- redirect_back_or_to return_reasons_path, status: :see_other
- end
-
+ class ReturnReasonsController < SolidusAdmin::ResourcesController
private
- def set_return_reason
- @return_reason = Spree::ReturnReason.find(params[:id])
- end
-
- def set_index_page
- return_reasons = apply_search_to(
- Spree::ReturnReason.unscoped.order(id: :desc),
- param: :q,
- )
+ def resource_class = Spree::ReturnReason
- set_page_and_extract_portion_from(return_reasons)
- end
+ def resources_collection = Spree::ReturnReason.unscoped
- def return_reason_params
+ def permitted_resource_params
params.require(:return_reason).permit(:name, :active)
end
end
diff --git a/admin/app/controllers/solidus_admin/roles_controller.rb b/admin/app/controllers/solidus_admin/roles_controller.rb
index fea6e2f5b0f..fc7612609b8 100644
--- a/admin/app/controllers/solidus_admin/roles_controller.rb
+++ b/admin/app/controllers/solidus_admin/roles_controller.rb
@@ -1,117 +1,15 @@
# frozen_string_literal: true
module SolidusAdmin
- class RolesController < SolidusAdmin::BaseController
- include SolidusAdmin::ControllerHelpers::Search
-
- before_action :set_role, only: %i[edit update]
-
+ class RolesController < SolidusAdmin::ResourcesController
search_scope(:all)
search_scope(:admin) { _1.where(name: "admin") }
- def index
- set_index_page
-
- respond_to do |format|
- format.html { render component('roles/index').new(page: @page) }
- end
- end
-
- def new
- @role = Spree::Role.new
-
- set_index_page
-
- respond_to do |format|
- format.html { render component('roles/new').new(page: @page, role: @role) }
- end
- end
-
- def create
- @role = Spree::Role.new(role_params)
-
- if @role.save
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.roles_path, status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- end
- end
- else
- set_index_page
-
- respond_to do |format|
- format.html do
- page_component = component('roles/new').new(page: @page, role: @role)
- render page_component, status: :unprocessable_entity
- end
- end
- end
- end
-
- def edit
- set_index_page
-
- respond_to do |format|
- format.html { render component('roles/edit').new(page: @page, role: @role) }
- end
- end
-
- def update
- if @role.update(role_params)
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.roles_path, status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- end
- end
- else
- set_index_page
-
- respond_to do |format|
- format.html do
- page_component = component('roles/edit').new(page: @page, role: @role)
- render page_component, status: :unprocessable_entity
- end
- end
- end
- end
-
- def destroy
- @roles = Spree::Role.where(id: params[:id])
-
- Spree::Role.transaction { @roles.destroy_all }
-
- flash[:notice] = t('.success')
- redirect_back_or_to solidus_admin.roles_path, status: :see_other
- end
-
private
- def set_role
- @role = Spree::Role.find(params[:id])
- end
-
- def set_index_page
- roles = apply_search_to(
- Spree::Role.unscoped.order(id: :desc),
- param: :q,
- )
-
- set_page_and_extract_portion_from(roles)
- end
+ def resource_class = Spree::Role
- def role_params
+ def permitted_resource_params
params.require(:role).permit(:role_id, :name, :description, permission_set_ids: [])
end
end
diff --git a/admin/app/controllers/solidus_admin/shipping_categories_controller.rb b/admin/app/controllers/solidus_admin/shipping_categories_controller.rb
index a7a58686da7..68a61787e35 100644
--- a/admin/app/controllers/solidus_admin/shipping_categories_controller.rb
+++ b/admin/app/controllers/solidus_admin/shipping_categories_controller.rb
@@ -1,117 +1,13 @@
# frozen_string_literal: true
module SolidusAdmin
- class ShippingCategoriesController < SolidusAdmin::BaseController
- include SolidusAdmin::ControllerHelpers::Search
-
- before_action :set_shipping_category, only: %i[edit update]
-
- def new
- @shipping_category = Spree::ShippingCategory.new
-
- set_index_page
-
- respond_to do |format|
- format.html { render component('shipping_categories/new').new(page: @page, shipping_category: @shipping_category) }
- end
- end
-
- def create
- @shipping_category = Spree::ShippingCategory.new(shipping_category_params)
-
- if @shipping_category.save
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.shipping_categories_path, status: :see_other
- end
-
- format.turbo_stream do
- # we need to explicitly write the refresh tag for now.
- # See https://github.com/hotwired/turbo-rails/issues/579
- render turbo_stream: '
'
- end
- end
- else
- set_index_page
-
- respond_to do |format|
- format.html do
- page_component = component('shipping_categories/new').new(page: @page, shipping_category: @shipping_category)
- render page_component, status: :unprocessable_entity
- end
- end
- end
- end
-
- def index
- set_index_page
-
- respond_to do |format|
- format.html { render component('shipping_categories/index').new(page: @page) }
- end
- end
-
- def edit
- set_index_page
-
- respond_to do |format|
- format.html { render component('shipping_categories/edit').new(page: @page, shipping_category: @shipping_category) }
- end
- end
-
- def update
- if @shipping_category.update(shipping_category_params)
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.shipping_categories_path, status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- end
- end
- else
- set_index_page
-
- respond_to do |format|
- format.html do
- page_component = component('shipping_categories/edit').new(page: @page, shipping_category: @shipping_category)
- render page_component, status: :unprocessable_entity
- end
- end
- end
- end
-
- def destroy
- @shipping_category = Spree::ShippingCategory.find_by!(id: params[:id])
-
- Spree::ShippingCategory.transaction { @shipping_category.destroy }
-
- flash[:notice] = t('.success')
- redirect_back_or_to shipping_categories_path, status: :see_other
- end
-
+ class ShippingCategoriesController < SolidusAdmin::ResourcesController
private
- def set_shipping_category
- @shipping_category = Spree::ShippingCategory.find(params[:id])
- end
+ def resource_class = Spree::ShippingCategory
- def shipping_category_params
+ def permitted_resource_params
params.require(:shipping_category).permit(:name)
end
-
- def set_index_page
- shipping_categories = apply_search_to(
- Spree::ShippingCategory.order(id: :desc),
- param: :q,
- )
-
- set_page_and_extract_portion_from(shipping_categories)
- end
end
end
diff --git a/admin/app/controllers/solidus_admin/stock_items_controller.rb b/admin/app/controllers/solidus_admin/stock_items_controller.rb
index 1333dbe672c..98d2cf098af 100644
--- a/admin/app/controllers/solidus_admin/stock_items_controller.rb
+++ b/admin/app/controllers/solidus_admin/stock_items_controller.rb
@@ -1,10 +1,8 @@
# frozen_string_literal: true
module SolidusAdmin
- class StockItemsController < SolidusAdmin::BaseController
+ class StockItemsController < SolidusAdmin::ResourcesController
include SolidusAdmin::ControllerHelpers::Search
- before_action :load_stock_items, only: [:index, :edit, :update]
- before_action :load_stock_item, only: [:edit, :update]
search_scope(:all, default: true) { _1 }
search_scope(:back_orderable) { _1.where(backorderable: true) }
@@ -18,49 +16,34 @@ def index
end
end
- def edit
- respond_to do |format|
- format.html { render component('stock_items/edit').new(stock_item: @stock_item, page: @page) }
- end
- end
-
def update
quantity_adjustment = params[:quantity_adjustment].to_i
- @stock_item.assign_attributes(stock_item_params)
+ @stock_item.assign_attributes(permitted_resource_params)
@stock_item.stock_movements.build(quantity: quantity_adjustment, originator: current_solidus_admin_user)
if @stock_item.save
- respond_to do |format|
- format.html { redirect_to solidus_admin.stock_items_path, status: :see_other }
- format.turbo_stream { render turbo_stream: '
' }
- end
+ redirect_to after_update_path, status: :see_other
else
- respond_to do |format|
- format.html { render component('stock_items/edit').new(stock_item: @stock_item, page: @page), status: :unprocessable_entity }
- end
+ page_component = edit_component.new(@stock_item)
+ render_resource_form_with_errors(page_component)
end
end
private
- def load_stock_items
- @stock_items = apply_search_to(
- Spree::StockItem.reorder(nil),
- param: :q,
- )
+ def resource_class = Spree::StockItem
- set_page_and_extract_portion_from(@stock_items, ordered_by: {
+ def resources_collection = Spree::StockItem.reorder(nil)
+
+ def resources_sorting_options
+ {
variant_id: :desc,
stock_location_id: :desc,
id: :desc,
- })
- end
-
- def load_stock_item
- @stock_item = Spree::StockItem.find(params[:id])
+ }
end
- def stock_item_params
+ def permitted_resource_params
params.require(:stock_item).permit(:backorderable)
end
end
diff --git a/admin/app/controllers/solidus_admin/store_credit_reasons_controller.rb b/admin/app/controllers/solidus_admin/store_credit_reasons_controller.rb
index 98bbd0615bf..c52f1047e0b 100644
--- a/admin/app/controllers/solidus_admin/store_credit_reasons_controller.rb
+++ b/admin/app/controllers/solidus_admin/store_credit_reasons_controller.rb
@@ -1,115 +1,15 @@
# frozen_string_literal: true
module SolidusAdmin
- class StoreCreditReasonsController < SolidusAdmin::BaseController
- include SolidusAdmin::ControllerHelpers::Search
-
- before_action :set_store_credit_reason, only: %i[edit update]
-
- def index
- set_index_page
-
- respond_to do |format|
- format.html { render component('store_credit_reasons/index').new(page: @page) }
- end
- end
-
- def new
- @store_credit_reason = Spree::StoreCreditReason.new
-
- set_index_page
-
- respond_to do |format|
- format.html { render component('store_credit_reasons/new').new(page: @page, store_credit_reason: @store_credit_reason) }
- end
- end
-
- def create
- @store_credit_reason = Spree::StoreCreditReason.new(store_credit_reason_params)
-
- if @store_credit_reason.save
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.store_credit_reasons_path, status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- end
- end
- else
- set_index_page
-
- respond_to do |format|
- format.html do
- page_component = component('store_credit_reasons/new').new(page: @page, store_credit_reason: @store_credit_reason)
- render page_component, status: :unprocessable_entity
- end
- end
- end
- end
-
- def edit
- set_index_page
-
- respond_to do |format|
- format.html { render component('store_credit_reasons/edit').new(page: @page, store_credit_reason: @store_credit_reason) }
- end
- end
-
- def update
- if @store_credit_reason.update(store_credit_reason_params)
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.store_credit_reasons_path, status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- end
- end
- else
- set_index_page
-
- respond_to do |format|
- format.html do
- page_component = component('store_credit_reasons/edit').new(page: @page, store_credit_reason: @store_credit_reason)
- render page_component, status: :unprocessable_entity
- end
- end
- end
- end
-
- def destroy
- @store_credit_reason = Spree::StoreCreditReason.find_by!(id: params[:id])
-
- Spree::StoreCreditReason.transaction { @store_credit_reason.destroy }
-
- flash[:notice] = t('.success')
- redirect_back_or_to store_credit_reasons_path, status: :see_other
- end
-
+ class StoreCreditReasonsController < SolidusAdmin::ResourcesController
private
- def set_store_credit_reason
- @store_credit_reason = Spree::StoreCreditReason.find(params[:id])
- end
-
- def store_credit_reason_params
- params.require(:store_credit_reason).permit(:name, :active)
- end
+ def resource_class = Spree::StoreCreditReason
- def set_index_page
- store_credit_reasons = apply_search_to(
- Spree::StoreCreditReason.unscoped.order(id: :desc),
- param: :q,
- )
+ def resources_collection = Spree::StoreCreditReason.unscoped
- set_page_and_extract_portion_from(store_credit_reasons)
+ def permitted_resource_params
+ params.require(:store_credit_reason).permit(:name, :active)
end
end
end
diff --git a/admin/app/controllers/solidus_admin/store_credits_controller.rb b/admin/app/controllers/solidus_admin/store_credits_controller.rb
index 3cf165e3df8..bf5bb454feb 100644
--- a/admin/app/controllers/solidus_admin/store_credits_controller.rb
+++ b/admin/app/controllers/solidus_admin/store_credits_controller.rb
@@ -1,11 +1,11 @@
# frozen_string_literal: true
module SolidusAdmin
- class StoreCreditsController < SolidusAdmin::BaseController
+ class StoreCreditsController < SolidusAdmin::ResourcesController
before_action :set_user
before_action :set_store_credit, only: [:show, :edit_amount, :update_amount, :edit_memo, :update_memo, :edit_validity, :invalidate]
before_action :set_store_credit_reasons, only: [:edit_amount, :update_amount, :edit_validity, :invalidate]
- before_action :set_store_credit_events, only: [:show, :edit_amount, :edit_memo, :edit_validity]
+ before_action :set_store_credit_events, only: [:show]
before_action :set_store_credit_categories, only: [:new]
def index
@@ -38,7 +38,7 @@ def new
def create
@store_credit = @user.store_credits.build(
- permitted_store_credit_params.merge({
+ permitted_resource_params.merge({
created_by: spree_current_user,
action_originator: spree_current_user
})
@@ -48,17 +48,8 @@ def create
return unless ensure_store_credit_category { render_new_with_errors }
if @store_credit.save
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.user_store_credits_path(@user), status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- end
- end
+ flash[:notice] = t('.success')
+ redirect_to after_create_path, status: :see_other
else
render_new_with_errors
end
@@ -70,9 +61,8 @@ def edit_amount
render component("users/store_credits/edit_amount").new(
user: @user,
store_credit: @store_credit,
- events: @store_credit_events,
reasons: @store_credit_reasons
- )
+ )
}
end
end
@@ -81,18 +71,9 @@ def update_amount
return unless ensure_amount { render_edit_with_errors }
return unless ensure_store_credit_reason { render_edit_with_errors }
- if @store_credit.update_amount(permitted_store_credit_params[:amount], @store_credit_reason, spree_current_user)
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.user_store_credit_path(@user, @store_credit), status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- end
- end
+ if @store_credit.update_amount(permitted_resource_params[:amount], @store_credit_reason, spree_current_user)
+ flash[:notice] = t('.success')
+ redirect_to after_update_path, status: :see_other
else
render_edit_with_errors
end
@@ -104,29 +85,20 @@ def edit_memo
render component("users/store_credits/edit_memo").new(
user: @user,
store_credit: @store_credit,
- events: @store_credit_events,
)
}
end
end
def update_memo
- if @store_credit.update(memo: permitted_store_credit_params[:memo])
+ if @store_credit.update(memo: permitted_resource_params[:memo])
flash[:notice] = t('.success')
else
# Memo update failures are nearly impossible to trigger due to lack of validation.
flash[:error] = t('.failure')
end
- respond_to do |format|
- format.html do
- redirect_to solidus_admin.user_store_credit_path(@user, @store_credit), status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- end
- end
+ redirect_to after_update_path, status: :see_other
end
def edit_validity
@@ -135,7 +107,6 @@ def edit_validity
render component("users/store_credits/edit_validity").new(
user: @user,
store_credit: @store_credit,
- events: @store_credit_events,
reasons: @store_credit_reasons
)
}
@@ -153,21 +124,15 @@ def invalidate
flash[:error] = t('.failure')
end
- respond_to do |format|
- format.html do
- redirect_to solidus_admin.user_store_credit_path(@user, @store_credit), status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- end
- end
+ redirect_to after_update_path, status: :see_other
end
private
+ def resource_class = Spree::StoreCredit
+
def set_store_credit
- @store_credit = Spree::StoreCredit.find(params[:id])
+ @store_credit ||= Spree::StoreCredit.find(params[:id])
end
def set_user
@@ -186,7 +151,7 @@ def set_store_credit_events
@store_credit_events = @store_credit.store_credit_events.chronological
end
- def permitted_store_credit_params
+ def permitted_resource_params
permitted_params = [:amount, :currency, :category_id, :memo]
permitted_params << :category_id if action_name.to_sym == :create
permitted_params << :store_credit_reason_id if [:update_amount, :invalidate].include?(action_name.to_sym)
@@ -197,16 +162,13 @@ def permitted_store_credit_params
def render_new_with_errors
set_store_credit_categories
- respond_to do |format|
- format.html do
- render component("users/store_credits/new").new(
- user: @user,
- store_credit: @store_credit,
- categories: @store_credit_categories
- ),
- status: :unprocessable_entity
- end
- end
+ page_component = component("users/store_credits/new").new(
+ user: @user,
+ store_credit: @store_credit,
+ categories: @store_credit_categories
+ )
+
+ render_resource_form_with_errors(page_component)
end
def render_edit_with_errors
@@ -218,22 +180,18 @@ def render_edit_with_errors
"edit_amount"
end
- respond_to do |format|
- format.html do
- render component("users/store_credits/#{template}").new(
- user: @user,
- store_credit: @store_credit,
- events: @store_credit_events,
- reasons: @store_credit_reasons
- ),
- status: :unprocessable_entity
- end
- end
+ page_component = component("users/store_credits/#{template}").new(
+ user: @user,
+ store_credit: @store_credit,
+ reasons: @store_credit_reasons
+ )
+
+ render_resource_form_with_errors(page_component)
end
def ensure_amount
- if permitted_store_credit_params[:amount].blank?
- @store_credit.errors.add(:amount, :greater_than, count: 0, value: permitted_store_credit_params[:amount])
+ if permitted_resource_params[:amount].blank?
+ @store_credit.errors.add(:amount, :greater_than, count: 0, value: permitted_resource_params[:amount])
yield if block_given? # Block is for error template rendering on a per-action basis so this can be re-used.
return false
end
@@ -241,7 +199,7 @@ def ensure_amount
end
def ensure_store_credit_reason
- @store_credit_reason = Spree::StoreCreditReason.find_by(id: permitted_store_credit_params[:store_credit_reason_id])
+ @store_credit_reason = Spree::StoreCreditReason.find_by(id: permitted_resource_params[:store_credit_reason_id])
if @store_credit_reason.blank?
@store_credit.errors.add(:store_credit_reason_id, "Store Credit reason must be provided")
@@ -252,7 +210,7 @@ def ensure_store_credit_reason
end
def ensure_store_credit_category
- @store_credit_category = Spree::StoreCreditCategory.find_by(id: permitted_store_credit_params[:category_id])
+ @store_credit_category = Spree::StoreCreditCategory.find_by(id: permitted_resource_params[:category_id])
if @store_credit_category.blank?
@store_credit.errors.add(:category_id, "Store Credit category must be provided")
@@ -261,5 +219,13 @@ def ensure_store_credit_category
end
true
end
+
+ def after_create_path
+ solidus_admin.user_store_credits_path(@user)
+ end
+
+ def after_update_path
+ solidus_admin.user_store_credit_path(@user, @store_credit)
+ end
end
end
diff --git a/admin/app/controllers/solidus_admin/tax_categories_controller.rb b/admin/app/controllers/solidus_admin/tax_categories_controller.rb
index 96a7d96a750..b9f601ef841 100644
--- a/admin/app/controllers/solidus_admin/tax_categories_controller.rb
+++ b/admin/app/controllers/solidus_admin/tax_categories_controller.rb
@@ -1,119 +1,13 @@
# frozen_string_literal: true
module SolidusAdmin
- class TaxCategoriesController < SolidusAdmin::BaseController
- include SolidusAdmin::ControllerHelpers::Search
-
- before_action :set_tax_category, only: %i[edit update]
-
- def new
- @tax_category = Spree::TaxCategory.new
-
- set_index_page
-
- respond_to do |format|
- format.html { render component('tax_categories/new').new(page: @page, tax_category: @tax_category) }
- end
- end
-
- def edit
- @tax_category = Spree::TaxCategory.find(params[:id])
-
- set_index_page
-
- respond_to do |format|
- format.html { render component('tax_categories/edit').new(page: @page, tax_category: @tax_category) }
- end
- end
-
- def create
- @tax_category = Spree::TaxCategory.new(tax_category_params)
-
- if @tax_category.save
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.tax_categories_path, status: :see_other
- end
-
- format.turbo_stream do
- # we need to explicitly write the refresh tag for now.
- # See https://github.com/hotwired/turbo-rails/issues/579
- render turbo_stream: '
'
- end
- end
- else
- set_index_page
-
- respond_to do |format|
- format.html do
- page_component = component('tax_categories/new').new(page: @page, tax_category: @tax_category)
- render page_component, status: :unprocessable_entity
- end
- end
- end
- end
-
- def update
- if @tax_category.update(tax_category_params)
- respond_to do |format|
- flash[:notice] = t('.success')
-
- format.html do
- redirect_to solidus_admin.tax_categories_path, status: :see_other
- end
-
- format.turbo_stream do
- render turbo_stream: '
'
- end
- end
- else
- set_index_page
-
- respond_to do |format|
- format.html do
- page_component = component('tax_categories/edit').new(page: @page, tax_category: @tax_category)
- render page_component, status: :unprocessable_entity
- end
- end
- end
- end
-
- def index
- set_index_page
-
- respond_to do |format|
- format.html { render component('tax_categories/index').new(page: @page) }
- end
- end
-
- def destroy
- @tax_categories = Spree::TaxCategory.where(id: params[:id])
-
- Spree::TaxCategory.transaction { @tax_categories.destroy_all }
-
- flash[:notice] = t('.success')
- redirect_back_or_to tax_categories_path, status: :see_other
- end
-
+ class TaxCategoriesController < SolidusAdmin::ResourcesController
private
- def set_tax_category
- @tax_category = Spree::TaxCategory.find(params[:id])
- end
+ def resource_class = Spree::TaxCategory
- def tax_category_params
+ def permitted_resource_params
params.require(:tax_category).permit(:name, :description, :is_default, :tax_code)
end
-
- def set_index_page
- tax_categories = apply_search_to(
- Spree::TaxCategory.order(created_at: :desc, id: :desc),
- param: :q,
- )
-
- set_page_and_extract_portion_from(tax_categories)
- end
end
end
diff --git a/admin/app/views/layouts/solidus_admin/application.html.erb b/admin/app/views/layouts/solidus_admin/application.html.erb
index 96196ecff7a..7e802291f26 100644
--- a/admin/app/views/layouts/solidus_admin/application.html.erb
+++ b/admin/app/views/layouts/solidus_admin/application.html.erb
@@ -5,6 +5,9 @@
<%= solidus_admin_title %>
+
+
+
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
diff --git a/admin/spec/controllers/solidus_admin/base_controller_spec.rb b/admin/spec/controllers/solidus_admin/base_controller_spec.rb
index b7bb5f8fc52..8c22c4a7fdf 100644
--- a/admin/spec/controllers/solidus_admin/base_controller_spec.rb
+++ b/admin/spec/controllers/solidus_admin/base_controller_spec.rb
@@ -44,4 +44,28 @@ def index
expect(response.code).to eq "200"
end
end
+
+ describe "layout rendering" do
+ subject { controller.send(:set_layout) }
+
+ context "with turbo frame request" do
+ before do
+ allow_any_instance_of(described_class).to receive(:turbo_frame_request?).and_return(true)
+ end
+
+ it "renders minimal turbo frame layout" do
+ is_expected.to be "turbo_rails/frame"
+ end
+ end
+
+ context "without turbo frame request" do
+ before do
+ allow_any_instance_of(described_class).to receive(:turbo_frame_request?).and_return(false)
+ end
+
+ it "renders the default layout" do
+ is_expected.to eq "solidus_admin/application"
+ end
+ end
+ end
end
diff --git a/admin/spec/features/adjustment_reasons_spec.rb b/admin/spec/features/adjustment_reasons_spec.rb
index 615dfa234fa..76a8d68db07 100644
--- a/admin/spec/features/adjustment_reasons_spec.rb
+++ b/admin/spec/features/adjustment_reasons_spec.rb
@@ -26,14 +26,14 @@
before do
visit "/admin/adjustment_reasons#{query}"
click_on "Add new"
- expect(page).to have_selector("dialog", wait: 5)
+ expect(page).to have_selector("dialog")
expect(page).to have_content("New Adjustment Reason")
expect(page).to be_axe_clean
end
it "closing the modal keeps query params" do
within("dialog") { click_on "Cancel" }
- expect(page).not_to have_selector("dialog", wait: 5)
+ expect(page).not_to have_selector("dialog")
expect(page.current_url).to include(query)
end
@@ -68,15 +68,15 @@
before do
Spree::AdjustmentReason.create(name: "Good Reason", code: 5999)
visit "/admin/adjustment_reasons#{query}"
- find_row("Good Reason").click
- expect(page).to have_selector("dialog", wait: 5)
+ click_on "Good Reason"
+ expect(page).to have_selector("dialog")
expect(page).to have_content("Edit Adjustment Reason")
expect(page).to be_axe_clean
end
it "closing the modal keeps query params" do
within("dialog") { click_on "Cancel" }
- expect(page).not_to have_selector("dialog", wait: 5)
+ expect(page).not_to have_selector("dialog")
expect(page.current_url).to include(query)
end
diff --git a/admin/spec/features/properties_spec.rb b/admin/spec/features/properties_spec.rb
index bf0c813b368..bf095252f1c 100644
--- a/admin/spec/features/properties_spec.rb
+++ b/admin/spec/features/properties_spec.rb
@@ -54,7 +54,7 @@
it "updates the property" do
visit "/admin/properties"
- find_row("Color").click
+ click_on "Color"
fill_in "Name", with: "Size"
fill_in "Presentation", with: "Cool Size"
@@ -68,7 +68,7 @@
it "shows validation errors" do
visit "/admin/properties"
- find_row("Color").click
+ click_on "Color"
fill_in "Name", with: ""
click_on "Update Property"
@@ -83,7 +83,7 @@
it "updates the property" do
visit "/admin/properties"
- find_row("Color").click
+ click_on "Color"
fill_in "Name", with: "Size"
fill_in "Presentation", with: "Cool Size"
@@ -97,7 +97,7 @@
it "shows validation errors" do
visit "/admin/properties"
- find_row("Color").click
+ click_on "Color"
expect(page).to have_field("Name", with: "Color")
fill_in "Name", with: ""
click_on "Update Property"
diff --git a/admin/spec/features/refund_reasons_spec.rb b/admin/spec/features/refund_reasons_spec.rb
index 19f83464914..6fec05bdd1b 100644
--- a/admin/spec/features/refund_reasons_spec.rb
+++ b/admin/spec/features/refund_reasons_spec.rb
@@ -26,14 +26,14 @@
before do
visit "/admin/refund_reasons/#{query}"
click_on "Add new"
- expect(page).to have_css("dialog", wait: 5)
+ expect(page).to have_css("dialog")
expect(page).to have_content("New Refund Reason")
expect(page).to be_axe_clean
end
it "closing the modal keeps query params" do
within("dialog") { click_on "Cancel" }
- expect(page).not_to have_selector("dialog", wait: 5)
+ expect(page).not_to have_selector("dialog")
expect(page.current_url).to include(query)
end
@@ -65,15 +65,15 @@
before do
Spree::RefundReason.create(name: "Return process")
visit "/admin/refund_reasons#{query}"
- find_row("Return process").click
- expect(page).to have_css("dialog", wait: 5)
+ click_on "Return process"
+ expect(page).to have_css("dialog")
expect(page).to have_content("Edit Refund Reason")
expect(page).to be_axe_clean
end
it "closing the modal keeps query params" do
within("dialog") { click_on "Cancel" }
- expect(page).not_to have_selector("dialog", wait: 5)
+ expect(page).not_to have_selector("dialog")
expect(page.current_url).to include(query)
end
diff --git a/admin/spec/features/return_reasons_spec.rb b/admin/spec/features/return_reasons_spec.rb
index 41c978da8de..548dbf61f80 100644
--- a/admin/spec/features/return_reasons_spec.rb
+++ b/admin/spec/features/return_reasons_spec.rb
@@ -26,14 +26,14 @@
before do
visit "/admin/return_reasons#{query}"
click_on "Add new"
- expect(page).to have_selector("dialog", wait: 5)
+ expect(page).to have_selector("dialog")
expect(page).to have_content("New Return Reason")
expect(page).to be_axe_clean
end
it "closing the modal keeps query params" do
within("dialog") { click_on "Cancel" }
- expect(page).not_to have_selector("dialog", wait: 5)
+ expect(page).not_to have_selector("dialog")
expect(page.current_url).to include(query)
end
@@ -67,15 +67,15 @@
before do
Spree::ReturnReason.create(name: "Good Reason")
visit "/admin/return_reasons#{query}"
- find_row("Good Reason").click
- expect(page).to have_selector("dialog", wait: 5)
+ click_on "Good Reason"
+ expect(page).to have_selector("dialog")
expect(page).to have_content("Edit Return Reason")
expect(page).to be_axe_clean
end
it "closing the modal keeps query params" do
within("dialog") { click_on "Cancel" }
- expect(page).not_to have_selector("dialog", wait: 5)
+ expect(page).not_to have_selector("dialog")
expect(page.current_url).to include(query)
end
diff --git a/admin/spec/features/roles_spec.rb b/admin/spec/features/roles_spec.rb
index 625d048e2a9..84eb98f0dc2 100644
--- a/admin/spec/features/roles_spec.rb
+++ b/admin/spec/features/roles_spec.rb
@@ -120,7 +120,7 @@
before do
Spree::Role.create(name: "Reviewer", permission_sets: [settings_edit_permission])
visit "/admin/roles#{query}"
- find_row("Reviewer").click
+ click_on "Reviewer"
expect(page).to have_selector("dialog", wait: 5)
expect(page).to have_content("Edit Role")
expect(page).to be_axe_clean
diff --git a/admin/spec/features/shipping_categories_spec.rb b/admin/spec/features/shipping_categories_spec.rb
index 09ad72338fd..005d9d071c0 100644
--- a/admin/spec/features/shipping_categories_spec.rb
+++ b/admin/spec/features/shipping_categories_spec.rb
@@ -26,14 +26,14 @@
before do
visit "/admin/shipping_categories#{query}"
click_on "Add new"
- expect(page).to have_css("dialog", wait: 5)
+ expect(page).to have_css("dialog")
expect(page).to have_content("New Shipping Category")
expect(page).to be_axe_clean
end
it "closing the modal keeps query params" do
within("dialog") { click_on "Cancel" }
- expect(page).not_to have_selector("dialog", wait: 5)
+ expect(page).not_to have_selector("dialog")
expect(page.current_url).to include(query)
end
@@ -65,15 +65,15 @@
before do
Spree::ShippingCategory.create(name: "Letter Mail")
visit "/admin/shipping_categories#{query}"
- find_row("Letter Mail").click
- expect(page).to have_css("dialog", wait: 5)
+ click_on "Letter Mail"
+ expect(page).to have_css("dialog")
expect(page).to have_content("Edit Shipping Category")
expect(page).to be_axe_clean
end
it "closing the modal keeps query params" do
within("dialog") { click_on "Cancel" }
- expect(page).not_to have_selector("dialog", wait: 5)
+ expect(page).not_to have_selector("dialog")
expect(page.current_url).to include(query)
end
diff --git a/admin/spec/features/store_credit_reasons_spec.rb b/admin/spec/features/store_credit_reasons_spec.rb
index fcbe4bbea6c..b91b0a4e7a6 100644
--- a/admin/spec/features/store_credit_reasons_spec.rb
+++ b/admin/spec/features/store_credit_reasons_spec.rb
@@ -26,14 +26,14 @@
before do
visit "/admin/store_credit_reasons#{query}"
click_on "Add new"
- expect(page).to have_selector("dialog", wait: 5)
+ expect(page).to have_selector("dialog")
expect(page).to have_content("New Store Credit Reason")
expect(page).to be_axe_clean
end
it "closing the modal keeps query params" do
within("dialog") { click_on "Cancel" }
- expect(page).not_to have_selector("dialog", wait: 5)
+ expect(page).not_to have_selector("dialog")
expect(page.current_url).to include(query)
end
@@ -65,15 +65,15 @@
before do
Spree::StoreCreditReason.create(name: "New Customer Reward")
visit "/admin/store_credit_reasons#{query}"
- find_row("New Customer Reward").click
- expect(page).to have_selector("dialog", wait: 5)
+ click_on "New Customer Reward"
+ expect(page).to have_selector("dialog")
expect(page).to have_content("Edit Store Credit Reason")
expect(page).to be_axe_clean
end
it "closing the modal keeps query params" do
within("dialog") { click_on "Cancel" }
- expect(page).not_to have_selector("dialog", wait: 5)
+ expect(page).not_to have_selector("dialog")
expect(page.current_url).to include(query)
end
diff --git a/admin/spec/features/store_credits_spec.rb b/admin/spec/features/store_credits_spec.rb
index 27788b13e31..adbf671a4f0 100644
--- a/admin/spec/features/store_credits_spec.rb
+++ b/admin/spec/features/store_credits_spec.rb
@@ -34,7 +34,7 @@
it "allows creation of a new store credit" do
click_on "Create One"
- expect(page).to have_selector("dialog", wait: 5)
+ expect(page).to have_selector("dialog")
expect(page).to have_content("New Store Credit")
within("dialog") do
@@ -47,7 +47,7 @@
click_on "Create One"
- expect(page).to have_selector("dialog", wait: 5)
+ expect(page).to have_selector("dialog")
expect(page).to have_content("New Store Credit")
within("dialog") do
@@ -95,7 +95,7 @@
context "when clicking through to a single store credit" do
before do
stub_authorization!(admin)
- find_row("$199.00").click
+ click_on("$199.00")
end
it "shows individual store credit details" do
@@ -109,7 +109,7 @@
context "with invalid amount" do
it "shows the appropriate error message" do
click_on "Edit Amount"
- expect(page).to have_selector("dialog", wait: 5)
+ expect(page).to have_selector("dialog")
expect(page).to have_content("Edit Store Credit Amount")
within("dialog") do
@@ -124,7 +124,7 @@
context "without a valid reason" do
it "shows the appropriate error message" do
click_on "Edit Amount"
- expect(page).to have_selector("dialog", wait: 5)
+ expect(page).to have_selector("dialog")
expect(page).to have_content("Edit Store Credit Amount")
within("dialog") do
@@ -139,7 +139,7 @@
context "with valid params" do
it "allows editing of the store credit amount" do
click_on "Edit Amount"
- expect(page).to have_selector("dialog", wait: 5)
+ expect(page).to have_selector("dialog")
expect(page).to have_content("Edit Store Credit Amount")
within("dialog") do
@@ -159,7 +159,7 @@
context "without a valid reason" do
it "shows the appropriate error message" do
click_on "Invalidate"
- expect(page).to have_selector("dialog", wait: 5)
+ expect(page).to have_selector("dialog")
expect(page).to have_content("Invalidate Store Credit")
within("dialog") do
@@ -173,7 +173,7 @@
context "with a valid reason" do
it "invalidates the store credit" do
click_on "Invalidate"
- expect(page).to have_selector("dialog", wait: 5)
+ expect(page).to have_selector("dialog")
expect(page).to have_content("Invalidate Store Credit")
within("dialog") do
@@ -192,7 +192,7 @@
context "when editing the store credit memo" do
it "allows editing of the store credit memo" do
click_on "Edit Memo"
- expect(page).to have_selector("dialog", wait: 5)
+ expect(page).to have_selector("dialog")
expect(page).to have_content("Edit Store Credit Memo")
within("dialog") do
diff --git a/admin/spec/features/tax_categories_spec.rb b/admin/spec/features/tax_categories_spec.rb
index 463f673bfdc..417ee5e1262 100644
--- a/admin/spec/features/tax_categories_spec.rb
+++ b/admin/spec/features/tax_categories_spec.rb
@@ -28,14 +28,14 @@
before do
visit "/admin/tax_categories#{query}"
click_on "Add new"
- expect(page).to have_selector("dialog", wait: 5)
+ expect(page).to have_selector("dialog")
expect(page).to have_content("New Tax Category")
expect(page).to be_axe_clean
end
it "closing the modal keeps query params" do
within("dialog") { click_on "Cancel" }
- expect(page).not_to have_selector("dialog", wait: 5)
+ expect(page).not_to have_selector("dialog")
expect(page.current_url).to include(query)
end
diff --git a/admin/spec/requests/solidus_admin/store_credits_spec.rb b/admin/spec/requests/solidus_admin/store_credits_spec.rb
index 7e4f7a951f5..a6a6e6f4088 100644
--- a/admin/spec/requests/solidus_admin/store_credits_spec.rb
+++ b/admin/spec/requests/solidus_admin/store_credits_spec.rb
@@ -60,7 +60,6 @@
it "renders the new store credit template with a 200 OK status" do
get solidus_admin.new_user_store_credit_path(user)
expect(response).to have_http_status(:ok)
- expect(response.body).to include(store_credit.amount.to_s)
end
end
@@ -72,17 +71,22 @@
}.to change(Spree::StoreCredit, :count).by(1)
end
- it "redirects to the store credits index page with a success message" do
- post solidus_admin.user_store_credits_path(user), params: valid_create_params
- expect(response).to redirect_to(solidus_admin.user_store_credits_path(user))
- follow_redirect!
- expect(response.body).to include("Store credit was successfully created.")
+ context "for html requests" do
+ it "redirects to the store credits index page with a success message" do
+ post solidus_admin.user_store_credits_path(user), params: valid_create_params
+ expect(response).to redirect_to(solidus_admin.user_store_credits_path(user))
+ follow_redirect!
+ expect(response.body).to include("Store credit was successfully created.")
+ end
end
- it "returns a turbo_stream response when requested" do
- post solidus_admin.user_store_credits_path(user, format: :turbo_stream), params: valid_create_params
- expect(response.media_type).to eq("text/vnd.turbo-stream.html")
- expect(response.body).to include('
')
+ context "for turbo_stream requests" do
+ it "returns a turbo_stream response when requested" do
+ post solidus_admin.user_store_credits_path(user, format: :turbo_stream), params: valid_create_params
+ expect(response).to redirect_to(solidus_admin.user_store_credits_path(user))
+ follow_redirect!
+ expect(response.body).to include("Store credit was successfully created.")
+ end
end
end
@@ -187,7 +191,7 @@
it "renders the edit_validity template with a 200 OK status" do
get solidus_admin.edit_validity_user_store_credit_path(user, store_credit)
expect(response).to have_http_status(:ok)
- expect(response.body).to include(store_credit.amount.to_s)
+ expect(response.body).to include("Choose Reason For Invalidating")
end
end
diff --git a/admin/spec/spec_helper.rb b/admin/spec/spec_helper.rb
index 70ecb0c11f7..3560cc13601 100644
--- a/admin/spec/spec_helper.rb
+++ b/admin/spec/spec_helper.rb
@@ -12,6 +12,7 @@
end
require 'solidus_admin'
+require 'rails-controller-testing'
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }