Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Re-organize bulk operations to make them more universal

Damian Legawiec165a1619cf10
diff --git a/admin/app/controllers/concerns/spree/admin/bulk_operations_concern.rb b/admin/app/controllers/concerns/spree/admin/bulk_operations_concern.rb
index ef734c6095..3fb18035b1 100644
--- a/admin/app/controllers/concerns/spree/admin/bulk_operations_concern.rb
+++ b/admin/app/controllers/concerns/spree/admin/bulk_operations_concern.rb
@@ -3,24 +3,6 @@ module Spree
module BulkOperationsConcern
extend ActiveSupport::Concern
- # GET /admin/:resources/bulk_modal?kind=:action_key
- # Generic bulk modal action that reads configuration from BulkAction
- def bulk_modal
- @bulk_action = find_bulk_action(params[:kind])
-
- if @bulk_action.nil?
- head :not_found
- return
- end
-
- @title = @bulk_action.resolve_title
- @body = @bulk_action.resolve_body
- @form_partial = @bulk_action.form_partial
- @form_partial_locals = @bulk_action.form_partial_locals || {}
-
- render partial: 'spree/admin/shared/bulk_modal_content'
- end
-
def bulk_add_tags
Spree::Tags::BulkAdd.call(tag_names: params[:tags], records: bulk_collection)
after_bulk_tags_change
@@ -58,16 +40,6 @@ module Spree
def bulk_collection
@bulk_collection ||= model_class.accessible_by(current_ability, :update).where(id: params[:ids])
end
-
- def find_bulk_action(key)
- return nil if key.blank?
-
- table_key = controller_name.to_sym
- table = Spree.admin.tables.get(table_key)
- return nil if table.nil?
-
- table.find_bulk_action(key.to_sym)
- end
end
end
end
diff --git a/admin/app/controllers/spree/admin/bulk_operations_controller.rb b/admin/app/controllers/spree/admin/bulk_operations_controller.rb
new file mode 100644
index 0000000000..015747cce4
--- /dev/null
+++ b/admin/app/controllers/spree/admin/bulk_operations_controller.rb
@@ -0,0 +1,36 @@
+module Spree
+ module Admin
+ class BulkOperationsController < Spree::Admin::BaseController
+ # GET /admin/bulk_operations/new?kind=:action_key&table_key=:table_key
+ # Generic bulk modal action that reads configuration from BulkAction
+ def new
+ @table_key = params[:table_key]&.to_sym
+ @bulk_action = find_bulk_action(params[:kind], @table_key)
+
+ if @bulk_action.nil?
+ head :not_found
+ return
+ end
+
+ @title = @bulk_action.resolve_title
+ @body = @bulk_action.resolve_body
+ @form_partial = @bulk_action.form_partial
+ @form_partial_locals = @bulk_action.form_partial_locals || {}
+ @button_text = @bulk_action.resolve_button_text
+ @button_class = @bulk_action.button_class
+
+ render partial: 'spree/admin/shared/bulk_modal_content'
+ end
+
+ private
+
+ def find_bulk_action(key, table_key)
+ return nil if key.blank? || table_key.blank?
+ return nil unless Spree.admin.tables.registered?(table_key)
+
+ table = Spree.admin.tables.get(table_key)
+ table.find_bulk_action(key.to_sym)
+ end
+ end
+ end
+end
diff --git a/admin/app/controllers/spree/admin/price_list_products_controller.rb b/admin/app/controllers/spree/admin/price_list_products_controller.rb
index f526a4c74e..ad7575f61d 100644
--- a/admin/app/controllers/spree/admin/price_list_products_controller.rb
+++ b/admin/app/controllers/spree/admin/price_list_products_controller.rb
@@ -44,7 +44,6 @@ module Spree
def edit_object_url(object, options = {})
spree.edit_admin_product_path(object, options)
end
-
end
end
end
diff --git a/admin/app/helpers/spree/admin/table_helper.rb b/admin/app/helpers/spree/admin/table_helper.rb
index 3938539685..f7812f5ca0 100644
--- a/admin/app/helpers/spree/admin/table_helper.rb
+++ b/admin/app/helpers/spree/admin/table_helper.rb
@@ -255,13 +255,14 @@ module Spree
# Render a single bulk action button or link
# @param action [Spree::Admin::Table::BulkAction] the bulk action
+ # @param table [Spree::Admin::Table] the table (required for generating modal path)
# @param options [Hash] additional options for the link
# @return [String] rendered HTML
- def render_bulk_action(action, **options)
+ def render_bulk_action(action, table:, **options)
return unless action.visible?(self)
action_path = action.action_path.is_a?(Proc) ? action.action_path.call(self) : action.action_path
- modal_path = action.modal_path.is_a?(Proc) ? action.modal_path.call(self) : action.modal_path
+ modal_path = spree.new_admin_bulk_operation_path(kind: action.key, table_key: table.key)
confirm_message = resolve_bulk_action_confirm(action.confirm)
link_options = {
@@ -318,11 +319,11 @@ module Spree
parts << bulk_operations_counter
primary_actions.each do |action|
- parts << render_bulk_action(action)
+ parts << render_bulk_action(action, table: table)
end
if secondary_actions.any?
- parts << render_bulk_actions_dropdown(secondary_actions)
+ parts << render_bulk_actions_dropdown(secondary_actions, table: table)
end
parts << bulk_operations_close_button
@@ -333,8 +334,9 @@ module Spree
# Render dropdown for secondary bulk actions
# @param actions [Array<Spree::Admin::Table::BulkAction>] the actions
+ # @param table [Spree::Admin::Table] the table (used for auto-generating modal_path)
# @return [String] rendered HTML
- def render_bulk_actions_dropdown(actions)
+ def render_bulk_actions_dropdown(actions, table: nil)
dropdown(direction: 'top', portal: false) do
toggle = dropdown_toggle do
icon('dots-vertical', class: 'mr-0')
@@ -342,7 +344,7 @@ module Spree
menu = dropdown_menu(class: 'mb-2') do
items = actions.map do |action|
- render_bulk_action(action, class: 'dropdown-item')
+ render_bulk_action(action, table: table, class: 'dropdown-item')
end
safe_join(items)
end
diff --git a/admin/app/models/spree/admin/table.rb b/admin/app/models/spree/admin/table.rb
index 086a646510..938b2566d5 100644
--- a/admin/app/models/spree/admin/table.rb
+++ b/admin/app/models/spree/admin/table.rb
@@ -1,12 +1,13 @@
module Spree
module Admin
class Table
- attr_reader :columns, :bulk_actions, :context, :model_class
+ attr_reader :columns, :bulk_actions, :context, :model_class, :key
attr_accessor :search_param, :search_placeholder, :row_actions, :row_actions_edit, :row_actions_delete, :new_resource,
:date_range_param, :date_range_label, :link_to_action
- def initialize(context, model_class: nil, search_param: :name_cont, search_placeholder: nil, row_actions: false, row_actions_edit: true, row_actions_delete: false, new_resource: true, date_range_param: nil, date_range_label: nil, link_to_action: :edit)
+ def initialize(context, key: nil, model_class: nil, search_param: :name_cont, search_placeholder: nil, row_actions: false, row_actions_edit: true, row_actions_delete: false, new_resource: true, date_range_param: nil, date_range_label: nil, link_to_action: :edit)
@context = context
+ @key = key
@model_class = model_class
@columns = {}
@bulk_actions = {}
@@ -196,7 +197,7 @@ module Spree
# Add a bulk action definition
# @param key [Symbol] unique action identifier
- # @param options [Hash] action options (label, icon, modal_path, action_path, position, etc.)
+ # @param options [Hash] action options (label, icon, action_path, position, etc.)
# @return [BulkAction] the created action
# @raise [ArgumentError] if action configuration is invalid
def add_bulk_action(key, **options)
diff --git a/admin/app/models/spree/admin/table/bulk_action.rb b/admin/app/models/spree/admin/table/bulk_action.rb
index c4b3e46134..6b72ac2bfc 100644
--- a/admin/app/models/spree/admin/table/bulk_action.rb
+++ b/admin/app/models/spree/admin/table/bulk_action.rb
@@ -10,9 +10,7 @@ module Spree
attribute :key
attribute :label
- attribute :label_options, default: -> { {} }
attribute :icon, :string
- attribute :modal_path
attribute :action_path
attribute :position, :integer, default: 999
attribute :condition
@@ -21,11 +19,11 @@ module Spree
# Modal content attributes - used by generic bulk_modal action
attribute :title
- attribute :title_options, default: -> { {} }
attribute :body
- attribute :body_options, default: -> { {} }
attribute :form_partial, :string
attribute :form_partial_locals, default: -> { {} }
+ attribute :button_text
+ attribute :button_class, :string, default: 'btn-primary'
validates :key, presence: true
validates :method, presence: true, inclusion: { in: METHODS }
@@ -41,21 +39,28 @@ module Spree
# Resolve label (handles i18n keys)
# @return [String]
def resolve_label
- resolve_i18n_attribute(label || key, label_options, key.to_s) || key.to_s.humanize
+ resolve_i18n_attribute(label || key, key.to_s) || key.to_s.humanize
end
# Resolve title for modal dialog (handles i18n keys)
# Falls back to label if title is not set
# @return [String]
def resolve_title
- resolve_i18n_attribute(title, title_options, "#{key}.title") ||
- resolve_i18n_attribute(label, label_options, key.to_s)
+ resolve_i18n_attribute(title, "#{key}.title") ||
+ resolve_i18n_attribute(label, key.to_s)
end
# Resolve body for modal dialog (handles i18n keys)
# @return [String]
def resolve_body
- resolve_i18n_attribute(body, body_options, "#{key}.body")
+ resolve_i18n_attribute(body, "#{key}.body")
+ end
+
+ # Resolve button text for modal dialog (handles i18n keys)
+ # Falls back to 'Confirm' if not set
+ # @return [String]
+ def resolve_button_text
+ resolve_i18n_attribute(button_text, "#{key}.button") || Spree.t(:confirm)
end
# Deep clone the action
@@ -70,18 +75,18 @@ module Spree
private
- def resolve_i18n_attribute(value, options, default_key)
+ def resolve_i18n_attribute(value, default_key)
return nil if value.blank?
case value
when Symbol
- Spree.t(value, **options)
+ Spree.t(value)
when /\Aadmin\./
- Spree.t(value, **options.merge(default: default_key.to_s.humanize))
+ Spree.t(value, default: default_key.to_s.humanize)
when String
value
else
- Spree.t(default_key, scope: 'admin.bulk_ops', default: default_key.to_s.humanize, **options)
+ Spree.t(default_key, scope: 'admin.bulk_ops', default: default_key.to_s.humanize)
end
end
end
diff --git a/admin/app/views/spree/admin/shared/_bulk_modal_content.html.erb b/admin/app/views/spree/admin/shared/_bulk_modal_content.html.erb
index 07bb738a88..2d1c087fd6 100644
--- a/admin/app/views/spree/admin/shared/_bulk_modal_content.html.erb
+++ b/admin/app/views/spree/admin/shared/_bulk_modal_content.html.erb
@@ -13,6 +13,6 @@
</div>
<div class="dialog-footer">
<%= dialog_discard_button('bulk-dialog') %>
- <%= turbo_save_button_tag %>
+ <%= turbo_save_button_tag(@button_text, class: "btn #{@button_class}") %>
</div>
<% end %>
diff --git a/admin/config/brakeman.ignore b/admin/config/brakeman.ignore
index 1f6197104d..9cb1b73df3 100644
--- a/admin/config/brakeman.ignore
+++ b/admin/config/brakeman.ignore
@@ -1,10 +1,10 @@
{
"ignored_warnings": [
{
- "fingerprint": "49c48c04e8e7307b26d32cfd6caf251970baaf0a8f3910a1c681600f7b995a53",
- "note": "form_partial comes from BulkAction registry configuration, not directly from user input. The params[:kind] is used to look up a pre-registered BulkAction object, and only the form_partial attribute from that registered configuration is rendered."
+ "fingerprint": "a79f90265cecf812d03f170da32bf27c2b68fcb14fe1317ad22b17a01bfc252d",
+ "note": "form_partial comes from BulkAction registry configuration, not directly from user input. The params[:kind] and params[:table_key] are used to look up a pre-registered BulkAction object from the tables registry, and only the form_partial attribute from that registered configuration is rendered."
}
],
- "updated": "2026-01-08",
+ "updated": "2026-01-09",
"brakeman_version": "7.1.2"
}
diff --git a/admin/config/initializers/spree_admin_tables.rb b/admin/config/initializers/spree_admin_tables.rb
index ee145d346f..6b84dcfda7 100644
--- a/admin/config/initializers/spree_admin_tables.rb
+++ b/admin/config/initializers/spree_admin_tables.rb
@@ -132,7 +132,6 @@ Rails.application.config.after_initialize do
Spree.admin.tables.products.add_bulk_action :set_active,
label: 'admin.bulk_ops.products.title.set_active',
icon: 'circle-check',
- modal_path: '/admin/products/bulk_modal?kind=set_active',
action_path: '/admin/products/bulk_status_update?status=active',
body: 'admin.bulk_ops.products.body.set_active',
position: 10,
@@ -141,7 +140,6 @@ Rails.application.config.after_initialize do
Spree.admin.tables.products.add_bulk_action :set_draft,
label: 'admin.bulk_ops.products.title.set_draft',
icon: 'circle-dotted',
- modal_path: '/admin/products/bulk_modal?kind=set_draft',
action_path: '/admin/products/bulk_status_update?status=draft',
body: 'admin.bulk_ops.products.body.set_draft',
position: 20
@@ -149,7 +147,6 @@ Rails.application.config.after_initialize do
Spree.admin.tables.products.add_bulk_action :set_archived,
label: 'admin.bulk_ops.products.title.set_archived',
icon: 'archive',
- modal_path: '/admin/products/bulk_modal?kind=set_archived',
action_path: '/admin/products/bulk_status_update?status=archived',
body: 'admin.bulk_ops.products.body.set_archived',
position: 30
@@ -157,7 +154,6 @@ Rails.application.config.after_initialize do
Spree.admin.tables.products.add_bulk_action :add_to_taxons,
label: 'admin.bulk_ops.products.title.add_to_taxons',
icon: 'category-plus',
- modal_path: '/admin/products/bulk_modal?kind=add_to_taxons',
action_path: '/admin/products/bulk_add_to_taxons',
body: 'admin.bulk_ops.products.body.add_to_taxons',
form_partial: 'spree/admin/bulk_operations/forms/taxon_picker',
@@ -167,7 +163,6 @@ Rails.application.config.after_initialize do
Spree.admin.tables.products.add_bulk_action :remove_from_taxons,
label: 'admin.bulk_ops.products.title.remove_from_taxons',
icon: 'category-minus',
- modal_path: '/admin/products/bulk_modal?kind=remove_from_taxons',
action_path: '/admin/products/bulk_remove_from_taxons',
body: 'admin.bulk_ops.products.body.remove_from_taxons',
form_partial: 'spree/admin/bulk_operations/forms/taxon_picker',
@@ -177,7 +172,6 @@ Rails.application.config.after_initialize do
Spree.admin.tables.products.add_bulk_action :add_tags,
label: 'admin.bulk_ops.products.title.add_tags',
icon: 'tag-plus',
- modal_path: '/admin/products/bulk_modal?kind=add_tags',
action_path: '/admin/products/bulk_add_tags',
body: 'admin.bulk_ops.products.body.add_tags',
form_partial: 'spree/admin/bulk_operations/forms/tag_picker',
@@ -188,7 +182,6 @@ Rails.application.config.after_initialize do
Spree.admin.tables.products.add_bulk_action :remove_tags,
label: 'admin.bulk_ops.products.title.remove_tags',
icon: 'tag-minus',
- modal_path: '/admin/products/bulk_modal?kind=remove_tags',
action_path: '/admin/products/bulk_remove_tags',
body: 'admin.bulk_ops.products.body.remove_tags',
form_partial: 'spree/admin/bulk_operations/forms/tag_picker',
@@ -546,7 +539,6 @@ Rails.application.config.after_initialize do
Spree.admin.tables.users.add_bulk_action :add_tags,
label: 'admin.bulk_ops.users.title.add_tags',
icon: 'tag-plus',
- modal_path: '/admin/users/bulk_modal?kind=add_tags',
action_path: '/admin/users/bulk_add_tags',
body: 'admin.bulk_ops.users.body.add_tags',
form_partial: 'spree/admin/bulk_operations/forms/tag_picker',
@@ -558,7 +550,6 @@ Rails.application.config.after_initialize do
Spree.admin.tables.users.add_bulk_action :remove_tags,
label: 'admin.bulk_ops.users.title.remove_tags',
icon: 'tag-minus',
- modal_path: '/admin/users/bulk_modal?kind=remove_tags',
action_path: '/admin/users/bulk_remove_tags',
body: 'admin.bulk_ops.users.body.remove_tags',
form_partial: 'spree/admin/bulk_operations/forms/tag_picker',
@@ -1532,9 +1523,10 @@ Rails.application.config.after_initialize do
Spree.admin.tables.price_list_products.add_bulk_action :remove_from_price_list,
label: 'admin.bulk_ops.price_list_products.title.remove',
icon: 'trash',
- modal_path: ->(view_context) { view_context.spree.bulk_modal_admin_price_list_products_path(view_context.instance_variable_get(:@price_list), kind: :remove_from_price_list) },
action_path: ->(view_context) { view_context.spree.bulk_destroy_admin_price_list_products_path(view_context.instance_variable_get(:@price_list)) },
body: 'admin.bulk_ops.price_list_products.body.remove',
+ button_text: :remove,
+ button_class: 'btn-danger',
method: :delete,
position: 10
end
diff --git a/admin/config/routes.rb b/admin/config/routes.rb
index d03488c4e2..1e278eaaa4 100644
--- a/admin/config/routes.rb
+++ b/admin/config/routes.rb
@@ -13,7 +13,6 @@ Spree::Core::Engine.add_routes do
collection do
get :select_options, defaults: { format: :json }
post :search
- get :bulk_modal
put :bulk_status_update
put :bulk_add_to_taxons
put :bulk_remove_from_taxons
@@ -38,7 +37,6 @@ Spree::Core::Engine.add_routes do
resources :products, only: [:index], controller: 'price_list_products' do
collection do
get :bulk_new
- get :bulk_modal
post :bulk_create
delete :bulk_destroy
end
@@ -121,7 +119,6 @@ Spree::Core::Engine.add_routes do
resources :gift_cards
collection do
- get :bulk_modal
post :bulk_add_tags
post :bulk_remove_tags
end
@@ -265,6 +262,9 @@ Spree::Core::Engine.add_routes do
# table columns (for session-based column selection)
post 'table_columns', to: 'table_columns#update', as: :table_columns
+ # bulk operations modal
+ resources :bulk_operations, only: [:new]
+
# dashboard
resource :dashboard, controller: 'dashboard'
get '/dashboard/analytics', to: 'dashboard#analytics', as: :dashboard_analytics
diff --git a/admin/lib/spree/admin/engine.rb b/admin/lib/spree/admin/engine.rb
index e43488f769..7ba8ba94c9 100644
--- a/admin/lib/spree/admin/engine.rb
+++ b/admin/lib/spree/admin/engine.rb
@@ -203,6 +203,7 @@ module Spree
name = name.to_sym
@registries[name] ||= Spree::Admin::Table.new(
name,
+ key: name,
model_class: model_class,
search_param: search_param,
search_placeholder: search_placeholder,
diff --git a/admin/spec/controllers/spree/admin/bulk_operations_controller_spec.rb b/admin/spec/controllers/spree/admin/bulk_operations_controller_spec.rb
new file mode 100644
index 0000000000..503a42d418
--- /dev/null
+++ b/admin/spec/controllers/spree/admin/bulk_operations_controller_spec.rb
@@ -0,0 +1,137 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::BulkOperationsController, type: :controller do
+ stub_authorization!
+
+ render_views
+
+ describe '#new' do
+ context 'with valid table_key and kind' do
+ subject { get :new, params: { table_key: 'products', kind: 'set_active' } }
+
+ it 'returns success' do
+ subject
+ expect(response).to have_http_status(:ok)
+ end
+
+ it 'renders the bulk modal content partial' do
+ expect(subject).to render_template(partial: 'spree/admin/shared/_bulk_modal_content')
+ end
+
+ it 'assigns bulk action attributes' do
+ subject
+ expect(assigns(:table_key)).to eq(:products)
+ expect(assigns(:bulk_action)).to be_present
+ expect(assigns(:bulk_action).key).to eq(:set_active)
+ end
+
+ it 'assigns title from bulk action' do
+ subject
+ expect(assigns(:title)).to be_present
+ end
+
+ it 'assigns body from bulk action' do
+ subject
+ expect(assigns(:body)).to be_present
+ end
+
+ it 'assigns default button_text' do
+ subject
+ expect(assigns(:button_text)).to eq(Spree.t(:confirm))
+ end
+
+ it 'assigns default button_class' do
+ subject
+ expect(assigns(:button_class)).to eq('btn-primary')
+ end
+ end
+
+ context 'with bulk action that has form_partial' do
+ subject { get :new, params: { table_key: 'products', kind: 'add_to_taxons' } }
+
+ it 'assigns form_partial from bulk action' do
+ subject
+ expect(assigns(:form_partial)).to eq('spree/admin/bulk_operations/forms/taxon_picker')
+ end
+ end
+
+ context 'with bulk action that has custom button options' do
+ subject { get :new, params: { table_key: 'price_list_products', kind: 'remove_from_price_list' } }
+
+ it 'assigns custom button_text' do
+ subject
+ expect(assigns(:button_text)).to eq(Spree.t(:remove))
+ end
+
+ it 'assigns custom button_class' do
+ subject
+ expect(assigns(:button_class)).to eq('btn-danger')
+ end
+ end
+
+ context 'with invalid table_key' do
+ subject { get :new, params: { table_key: 'nonexistent', kind: 'set_active' } }
+
+ it 'returns not found' do
+ subject
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ context 'with invalid kind' do
+ subject { get :new, params: { table_key: 'products', kind: 'nonexistent_action' } }
+
+ it 'returns not found' do
+ subject
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ context 'with missing table_key' do
+ subject { get :new, params: { kind: 'set_active' } }
+
+ it 'returns not found' do
+ subject
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ context 'with missing kind' do
+ subject { get :new, params: { table_key: 'products' } }
+
+ it 'returns not found' do
+ subject
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ context 'with blank parameters' do
+ subject { get :new, params: { table_key: '', kind: '' } }
+
+ it 'returns not found' do
+ subject
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ context 'renders turbo frame' do
+ subject { get :new, params: { table_key: 'products', kind: 'set_active' } }
+
+ it 'includes turbo frame tag for bulk_dialog' do
+ subject
+ expect(response.body).to include('turbo-frame')
+ expect(response.body).to include('bulk_dialog')
+ end
+
+ it 'includes dialog header' do
+ subject
+ expect(response.body).to include('dialog-header')
+ end
+
+ it 'includes dialog footer with buttons' do
+ subject
+ expect(response.body).to include('dialog-footer')
+ end
+ end
+ end
+end
diff --git a/admin/spec/controllers/spree/admin/products_controller_spec.rb b/admin/spec/controllers/spree/admin/products_controller_spec.rb
index e1863868d3..4ab0480639 100644
--- a/admin/spec/controllers/spree/admin/products_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/products_controller_spec.rb
@@ -1503,24 +1503,6 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
end
end
- describe 'GET #bulk_modal' do
- context 'as admin' do
- it 'renders modal for set_active' do
- get :bulk_modal, params: { kind: 'set_active' }
-
- expect(response).to have_http_status(:ok)
- expect(assigns[:title]).to eq(Spree.t('admin.bulk_ops.products.title.set_active'))
- expect(assigns[:body]).to eq(Spree.t('admin.bulk_ops.products.body.set_active'))
- end
-
- it 'returns not found for unknown kind' do
- get :bulk_modal, params: { kind: 'unknown' }
-
- expect(response).to have_http_status(:not_found)
- end
- end
- end
-
describe 'in_stock/out_of_stock' do
let!(:in_stock_product) { create(:product) }
let!(:in_stock_variant) { create(:variant, product: in_stock_product) }
diff --git a/admin/spec/controllers/spree/admin/users_controller_spec.rb b/admin/spec/controllers/spree/admin/users_controller_spec.rb
index 4bec217b4e..5fec2fcc5b 100644
--- a/admin/spec/controllers/spree/admin/users_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/users_controller_spec.rb
@@ -158,22 +158,6 @@ RSpec.describe Spree::Admin::UsersController, type: :controller do
end
end
- describe 'GET #bulk_modal' do
- it 'renders the bulk modal' do
- get :bulk_modal, params: { kind: 'add_tags' }
-
- expect(response).to have_http_status(:ok)
- expect(assigns[:title]).to eq(Spree.t('admin.bulk_ops.users.title.add_tags'))
- expect(assigns[:body]).to eq(Spree.t('admin.bulk_ops.users.body.add_tags'))
- end
-
- it 'returns not found for unknown kind' do
- get :bulk_modal, params: { kind: 'unknown' }
-
- expect(response).to have_http_status(:not_found)
- end
- end
-
describe 'POST #bulk_add_tags' do
let(:users) { create_list(:user, 3) }
let(:tags) { ['tag1', 'tag2'] }
diff --git a/admin/spec/helpers/spree/admin/table_helper_spec.rb b/admin/spec/helpers/spree/admin/table_helper_spec.rb
index 30f9efa4ae..f6d7bbb212 100644
--- a/admin/spec/helpers/spree/admin/table_helper_spec.rb
+++ b/admin/spec/helpers/spree/admin/table_helper_spec.rb
@@ -562,26 +562,26 @@ RSpec.describe Spree::Admin::TableHelper, type: :helper do
key: :delete,
label: 'Delete',
icon: 'trash',
- modal_path: '/admin/bulk/delete',
action_path: '/admin/products/bulk_delete'
)
end
before do
allow(helper).to receive(:icon).and_return('<svg></svg>'.html_safe)
+ allow(helper).to receive(:spree).and_return(double(new_admin_bulk_operation_path: '/admin/bulk_operations/new?kind=delete&table_key=products'))
end
it 'renders link with action data attributes' do
- result = helper.render_bulk_action(action)
+ result = helper.render_bulk_action(action, table: table)
expect(result).to include('Delete')
- expect(result).to include('href="/admin/bulk/delete"')
+ expect(result).to include('href="/admin/bulk_operations/new?kind=delete&amp;table_key=products"')
expect(result).to include('data-action')
expect(result).to include('bulk-operation#setBulkAction')
end
it 'includes icon when present' do
- result = helper.render_bulk_action(action)
+ helper.render_bulk_action(action, table: table)
expect(helper).to have_received(:icon).with('trash')
end
@@ -593,7 +593,7 @@ RSpec.describe Spree::Admin::TableHelper, type: :helper do
condition: false
)
- result = helper.render_bulk_action(action)
+ result = helper.render_bulk_action(action, table: table)
expect(result).to be_nil
end
@@ -602,11 +602,10 @@ RSpec.describe Spree::Admin::TableHelper, type: :helper do
action = Spree::Admin::Table::BulkAction.new(
key: :delete,
label: 'Delete',
- modal_path: '/admin/bulk/delete',
confirm: 'Are you sure?'
)
- result = helper.render_bulk_action(action)
+ result = helper.render_bulk_action(action, table: table)
expect(result).to include('data-confirm="Are you sure?"')
end
@@ -617,6 +616,7 @@ RSpec.describe Spree::Admin::TableHelper, type: :helper do
allow(helper).to receive(:bulk_operations_counter).and_return('<span>0 selected</span>'.html_safe)
allow(helper).to receive(:bulk_operations_close_button).and_return('<button>Close</button>'.html_safe)
allow(helper).to receive(:icon).and_return('<svg></svg>'.html_safe)
+ allow(helper).to receive(:spree).and_return(double(new_admin_bulk_operation_path: '/admin/bulk_operations/new'))
end
it 'returns nil when no bulk actions' do
@@ -626,7 +626,7 @@ RSpec.describe Spree::Admin::TableHelper, type: :helper do
end
it 'renders panel with bulk actions' do
- table.add_bulk_action(:delete, label: 'Delete', modal_path: '/delete')
+ table.add_bulk_action(:delete, label: 'Delete', action_path: '/delete')
result = helper.render_bulk_actions_panel(table)
@@ -635,8 +635,8 @@ RSpec.describe Spree::Admin::TableHelper, type: :helper do
end
it 'renders first 2 actions as primary' do
- table.add_bulk_action(:delete, label: 'Delete', modal_path: '/delete', position: 10)
- table.add_bulk_action(:export, label: 'Export', modal_path: '/export', position: 20)
+ table.add_bulk_action(:delete, label: 'Delete', action_path: '/delete', position: 10)
+ table.add_bulk_action(:export, label: 'Export', action_path: '/export', position: 20)
result = helper.render_bulk_actions_panel(table)
diff --git a/admin/spec/models/spree/admin/table/bulk_action_spec.rb b/admin/spec/models/spree/admin/table/bulk_action_spec.rb
index 915520d60c..734d9cbdc2 100644
--- a/admin/spec/models/spree/admin/table/bulk_action_spec.rb
+++ b/admin/spec/models/spree/admin/table/bulk_action_spec.rb
@@ -8,7 +8,7 @@ RSpec.describe Spree::Admin::Table::BulkAction do
expect(action.key).to eq(:delete)
expect(action.position).to eq(999)
expect(action.method).to eq(:put)
- expect(action.label_options).to eq({})
+ expect(action.button_class).to eq('btn-primary')
end
it 'accepts custom options' do
@@ -16,7 +16,6 @@ RSpec.describe Spree::Admin::Table::BulkAction do
key: :export,
label: 'Export Products',
icon: 'download',
- modal_path: '/admin/exports/new',
action_path: '/admin/exports',
position: 10,
method: :post
@@ -24,7 +23,6 @@ RSpec.describe Spree::Admin::Table::BulkAction do
expect(action.label).to eq('Export Products')
expect(action.icon).to eq('download')
- expect(action.modal_path).to eq('/admin/exports/new')
expect(action.action_path).to eq('/admin/exports')
expect(action.position).to eq(10)
expect(action.method).to eq(:post)
@@ -123,11 +121,34 @@ RSpec.describe Spree::Admin::Table::BulkAction do
action = described_class.new(key: :delete_selected)
expect(action.resolve_label).to include('Delete')
end
+ end
- it 'supports label_options' do
- action = described_class.new(key: :delete, label: :delete_count, label_options: { count: 5 })
- # Translation with options
- expect(action.resolve_label).to be_a(String)
+ describe '#resolve_button_text' do
+ it 'returns string button_text directly' do
+ action = described_class.new(key: :delete, button_text: 'Remove Items')
+ expect(action.resolve_button_text).to eq('Remove Items')
+ end
+
+ it 'translates symbol button_text' do
+ action = described_class.new(key: :delete, button_text: :remove)
+ expect(action.resolve_button_text).to be_a(String)
+ end
+
+ it 'falls back to Confirm when not set' do
+ action = described_class.new(key: :delete)
+ expect(action.resolve_button_text).to eq(Spree.t(:confirm))
+ end
+ end
+
+ describe '#button_class' do
+ it 'defaults to btn-primary' do
+ action = described_class.new(key: :delete)
+ expect(action.button_class).to eq('btn-primary')
+ end
+
+ it 'can be customized' do
+ action = described_class.new(key: :delete, button_class: 'btn-danger')
+ expect(action.button_class).to eq('btn-danger')
end
end
@@ -137,7 +158,6 @@ RSpec.describe Spree::Admin::Table::BulkAction do
key: :delete,
label: 'Delete',
icon: 'trash',
- modal_path: '/modal',
action_path: '/action',
position: 10,
confirm: 'Are you sure?',
@@ -148,7 +168,6 @@ RSpec.describe Spree::Admin::Table::BulkAction do
expect(attrs['key']).to eq(:delete)
expect(attrs['label']).to eq('Delete')
expect(attrs['icon']).to eq('trash')
- expect(attrs['modal_path']).to eq('/modal')
expect(attrs['action_path']).to eq('/action')
expect(attrs['position']).to eq(10)
expect(attrs['confirm']).to eq('Are you sure?')
diff --git a/docs/developer/admin/tables.mdx b/docs/developer/admin/tables.mdx
index eb6d036bec..7db604f9b6 100644
--- a/docs/developer/admin/tables.mdx
+++ b/docs/developer/admin/tables.mdx
@@ -414,36 +414,29 @@ Add bulk actions that appear when users select multiple rows:
```ruby
Rails.application.config.after_initialize do
Spree.admin.tables.products.add_bulk_action :set_active,
- label: 'admin.bulk_ops.products.title.set_status',
- label_options: { status: :active },
+ label: 'admin.bulk_ops.products.title.set_active',
icon: 'circle-check',
- modal_path: '/admin/products/bulk_modal?kind=set_status&status=active',
action_path: '/admin/products/bulk_status_update?status=active',
+ body: 'admin.bulk_ops.products.body.set_active',
position: 10,
if: -> { can?(:activate, Spree::Product) }
end
```
+The modal is automatically rendered via `/admin/bulk_operations/new?kind=:action_key&table_key=:table_key`.
+
### Bulk Action Options
<ParamField path="label" type="String" required>
Translation key or text for the action button.
</ParamField>
-<ParamField path="label_options" type="Hash">
- Interpolation options for the label translation.
-</ParamField>
-
<ParamField path="icon" type="String">
Icon name from [Tabler Icons](https://tabler.io/icons).
</ParamField>
-<ParamField path="modal_path" type="String" required>
- URL for the confirmation modal.
-</ParamField>
-
-<ParamField path="action_path" type="String" required>
- URL for the bulk action endpoint.
+<ParamField path="action_path" type="String | Lambda" required>
+ URL for the bulk action endpoint. Use a Lambda for dynamic paths: `action_path: ->(view_context) { view_context.custom_path }`.
</ParamField>
<ParamField path="method" type="Symbol" default=":put">
@@ -462,6 +455,14 @@ end
Confirmation message before executing the action.
</ParamField>
+<ParamField path="button_text" type="String">
+ Custom text for the modal submit button. Supports translation keys. Defaults to "Confirm".
+</ParamField>
+
+<ParamField path="button_class" type="String" default="btn-primary">
+ CSS class for the modal submit button (e.g., `btn-danger` for destructive actions).
+</ParamField>
+
### Managing Bulk Actions
```ruby