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.

Implement custom sorting and bulk operations support

Damian Legawiec083e2ec7b24b
diff --git a/admin/app/controllers/concerns/spree/admin/record_list_concern.rb b/admin/app/controllers/concerns/spree/admin/record_list_concern.rb
new file mode 100644
index 0000000000..95864ade6a
--- /dev/null
+++ b/admin/app/controllers/concerns/spree/admin/record_list_concern.rb
@@ -0,0 +1,58 @@
+module Spree
+ module Admin
+ module RecordListConcern
+ extend ActiveSupport::Concern
+
+ included do
+ class_attribute :_record_list_key, instance_writer: false
+ end
+
+ class_methods do
+ # Override the record list key for this controller
+ # By default, the key is derived from controller_name (e.g., ProductsController -> :products)
+ # @param key [Symbol] the record list key (e.g., :products)
+ def use_record_list(key)
+ self._record_list_key = key
+ end
+ end
+
+ # Get the record list key for this controller
+ # Uses explicitly set key or derives from controller_name
+ # @return [Symbol]
+ def record_list_key
+ _record_list_key || controller_name.to_sym
+ end
+
+ # Get the record list for this controller
+ # @return [Spree::Admin::RecordList, nil]
+ def record_list
+ @record_list ||= Spree.admin.record_lists.get(record_list_key)
+ end
+
+ # Apply custom sort scopes if the record list has them configured
+ # Should be called after ransack result is obtained
+ # @param collection [ActiveRecord::Relation] the collection to sort
+ # @return [ActiveRecord::Relation] sorted collection
+ def apply_record_list_sort(collection)
+ return collection unless record_list
+
+ sort_param = params.dig(:q, :s)
+ column = record_list.find_custom_sort_column(sort_param)
+
+ return collection unless column
+
+ # Remove sort from ransack params since we'll apply it manually
+ record_list.apply_custom_sort(collection.reorder(nil), sort_param)
+ end
+
+ # Check if current sort uses a custom scope
+ # @return [Boolean]
+ def custom_sort_active?
+ return false unless record_list
+
+ sort_param = params.dig(:q, :s)
+ record_list.find_custom_sort_column(sort_param).present?
+ end
+ end
+ end
+end
diff --git a/admin/app/controllers/spree/admin/products_controller.rb b/admin/app/controllers/spree/admin/products_controller.rb
index eb5ba7e650..751709c784 100644
--- a/admin/app/controllers/spree/admin/products_controller.rb
+++ b/admin/app/controllers/spree/admin/products_controller.rb
@@ -265,16 +265,24 @@ module Spree
# That's because we sort products by name
@collection = @collection.group(:name) if params.dig(:q, :out_of_stock_items) == '1'
+ # Check if custom sort scope is needed (e.g., for price sorting)
+ custom_sort = custom_sort_active?
+ ransack_params = params[:q].except(:deleted_at_null)
+ ransack_params = ransack_params.except(:s) if custom_sort
+
# @search needs to be defined as this is passed to search_form_for
# Temporarily remove params[:q][:deleted_at_null] from params[:q] to ransack products.
# This is to include all products and not just deleted products.
- @search = @collection.ransack(params[:q].except(:deleted_at_null))
+ @search = @collection.ransack(ransack_params)
@collection = @search.result(distinct: true).
for_ordering_with_translations(model_class, :name).
includes(product_includes).
page(params[:page]).
per(params[:per_page] || Spree::Admin::RuntimeConfig.admin_products_per_page)
+ # Apply custom sort scope if configured (e.g., for price sorting)
+ @collection = apply_record_list_sort(@collection) if custom_sort
+
@collection
end
diff --git a/admin/app/controllers/spree/admin/record_list_columns_controller.rb b/admin/app/controllers/spree/admin/record_list_columns_controller.rb
index 37fa106b44..5120bb9db6 100644
--- a/admin/app/controllers/spree/admin/record_list_columns_controller.rb
+++ b/admin/app/controllers/spree/admin/record_list_columns_controller.rb
@@ -18,10 +18,8 @@ module Spree
end
end
- respond_to do |format|
- format.html { redirect_back fallback_location: spree.admin_path }
- format.turbo_stream { redirect_back fallback_location: spree.admin_path }
- end
+ redirect_url = params[:redirect_url].presence || request.referer || spree.admin_path
+ redirect_to redirect_url, status: :see_other
end
end
end
diff --git a/admin/app/controllers/spree/admin/resource_controller.rb b/admin/app/controllers/spree/admin/resource_controller.rb
index 51dcd6c660..12a2df4fc5 100644
--- a/admin/app/controllers/spree/admin/resource_controller.rb
+++ b/admin/app/controllers/spree/admin/resource_controller.rb
@@ -1,5 +1,6 @@
class Spree::Admin::ResourceController < Spree::Admin::BaseController
include Spree::Admin::Callbacks
+ include Spree::Admin::RecordListConcern
helper_method :new_object_url, :edit_object_url, :object_url, :collection_url, :model_class
before_action :load_resource
diff --git a/admin/app/helpers/spree/admin/record_list_helper.rb b/admin/app/helpers/spree/admin/record_list_helper.rb
index 57cfc21e8b..825f1cd0e7 100644
--- a/admin/app/helpers/spree/admin/record_list_helper.rb
+++ b/admin/app/helpers/spree/admin/record_list_helper.rb
@@ -259,6 +259,98 @@ module Spree
Spree::Admin::RecordList::Filter.operators_for_select.to_json
end
+ # Count the number of applied filters from query_state parameter
+ # @param query_state [String] JSON string of query state
+ # @return [Integer] total number of filters applied
+ def count_applied_filters(query_state)
+ return 0 if query_state.blank? || query_state == '{}'
+
+ begin
+ state = JSON.parse(query_state)
+ count_filters_in_state(state)
+ rescue JSON::ParserError
+ 0
+ end
+ end
+
+ # Render a single bulk action button or link
+ # @param action [Spree::Admin::RecordList::BulkAction] the bulk action
+ # @param options [Hash] additional options for the link
+ # @return [String] rendered HTML
+ def render_bulk_action(action, **options)
+ return unless action.visible?(self)
+
+ link_options = {
+ class: options[:class] || 'btn',
+ data: {
+ action: 'click->bulk-operation#setBulkAction click->bulk-dialog#open',
+ turbo_frame: :bulk_dialog,
+ url: action.action_path
+ }
+ }
+
+ link_options[:data][:confirm] = action.confirm if action.confirm.present?
+
+ content = if action.icon.present?
+ icon(action.icon) + ' ' + action.resolve_label
+ else
+ action.resolve_label
+ end
+
+ link_to content, action.modal_path, link_options
+ end
+
+ # Render bulk actions panel for a record list
+ # @param record_list [Spree::Admin::RecordList] the record list
+ # @param options [Hash] additional options
+ # @return [String] rendered HTML
+ def render_bulk_actions_panel(record_list)
+ actions = record_list.visible_bulk_actions(self)
+ return if actions.empty?
+
+ # Split actions into primary (first 2) and secondary (rest in dropdown)
+ primary_actions = actions.first(2)
+ secondary_actions = actions.drop(2)
+
+ content_tag(:div, id: 'bulk-panel', class: 'hidden', data: { bulk_operation_target: 'panel' }) do
+ content_tag(:div, class: 'bulk-panel-container') do
+ parts = []
+ parts << bulk_operations_counter
+
+ primary_actions.each do |action|
+ parts << render_bulk_action(action)
+ end
+
+ if secondary_actions.any?
+ parts << render_bulk_actions_dropdown(secondary_actions)
+ end
+
+ parts << bulk_operations_close_button
+ safe_join(parts)
+ end
+ end
+ end
+
+ # Render dropdown for secondary bulk actions
+ # @param actions [Array<Spree::Admin::RecordList::BulkAction>] the actions
+ # @return [String] rendered HTML
+ def render_bulk_actions_dropdown(actions)
+ dropdown(direction: 'top', portal: false) do
+ toggle = dropdown_toggle do
+ icon('dots-vertical', class: 'mr-0')
+ end
+
+ menu = dropdown_menu(class: 'mb-2') do
+ items = actions.map do |action|
+ render_bulk_action(action, class: 'dropdown-item')
+ end
+ safe_join(items)
+ end
+
+ safe_join([toggle, menu])
+ end
+ end
+
private
def find_sort_label(sortable, current_sort)
@@ -289,6 +381,29 @@ module Spree
class: "dropdown-item #{'active' if is_active}",
data: { turbo_action: 'advance' }
end
+
+ # Recursively count filters in a state object (including nested groups)
+ # @param state [Hash] the state object
+ # @return [Integer] count of filters
+ def count_filters_in_state(state)
+ return 0 unless state.is_a?(Hash)
+
+ count = 0
+
+ # Count filters at this level (only those with a field selected)
+ if state['filters'].is_a?(Array)
+ count += state['filters'].count { |f| f['field'].present? }
+ end
+
+ # Count filters in nested groups
+ if state['groups'].is_a?(Array)
+ state['groups'].each do |group|
+ count += count_filters_in_state(group)
+ end
+ end
+
+ count
+ end
end
end
end
diff --git a/admin/app/models/spree/admin/record_list.rb b/admin/app/models/spree/admin/record_list.rb
index a317250ffb..f3202b6b05 100644
--- a/admin/app/models/spree/admin/record_list.rb
+++ b/admin/app/models/spree/admin/record_list.rb
@@ -1,15 +1,23 @@
module Spree
module Admin
class RecordList
- attr_reader :columns, :context, :model_class
- attr_accessor :search_param, :search_placeholder
+ attr_reader :columns, :bulk_actions, :context, :model_class
+ attr_accessor :search_param, :search_placeholder, :row_actions
- def initialize(context, model_class: nil, search_param: :name_cont, search_placeholder: nil)
+ def initialize(context, model_class: nil, search_param: :name_cont, search_placeholder: nil, row_actions: true)
@context = context
@model_class = model_class
@columns = {}
+ @bulk_actions = {}
@search_param = search_param
@search_placeholder = search_placeholder
+ @row_actions = row_actions
+ end
+
+ # Check if row actions are enabled
+ # @return [Boolean]
+ def row_actions?
+ @row_actions
end
# Add a column definition
@@ -123,19 +131,103 @@ module Spree
@columns.values.select(&:sortable?).sort_by(&:position)
end
+ # Find column with custom sort scope for the given sort param
+ # @param sort_param [String] the sort param (e.g., "master_price desc")
+ # @return [Column, nil] column with custom sort scope or nil
+ def find_custom_sort_column(sort_param)
+ return nil if sort_param.blank?
+
+ attribute = sort_param.to_s.split.first
+ @columns.values.find { |c| c.ransack_attribute == attribute && c.custom_sort? }
+ end
+
+ # Apply custom sort scope to collection
+ # @param collection [ActiveRecord::Relation] the collection to sort
+ # @param sort_param [String] the sort param (e.g., "master_price desc")
+ # @return [ActiveRecord::Relation] sorted collection
+ def apply_custom_sort(collection, sort_param)
+ column = find_custom_sort_column(sort_param)
+ return collection unless column
+
+ direction = sort_param.to_s.include?('desc') ? :desc : :asc
+ scope_name = direction == :desc ? column.sort_scope_desc : column.sort_scope_asc
+
+ return collection unless scope_name.present?
+
+ collection.send(scope_name)
+ end
+
# Get filterable columns (columns that can be used in query builder)
# @return [Array<Column>]
def filterable_columns
@columns.values.select(&:filterable?).sort_by(&:position)
end
+ # Add a bulk action definition
+ # @param key [Symbol] unique action identifier
+ # @param options [Hash] action options (label, icon, modal_path, action_path, position, etc.)
+ # @return [BulkAction] the created action
+ def add_bulk_action(key, **options)
+ key = key.to_sym
+ action = BulkAction.new(key, **options)
+ @bulk_actions[key] = action
+ sort_bulk_actions!
+ action
+ end
+
+ # Remove a bulk action
+ # @param key [Symbol] action key to remove
+ # @return [BulkAction, nil] the removed action or nil
+ def remove_bulk_action(key)
+ @bulk_actions.delete(key.to_sym)
+ end
+
+ # Update an existing bulk action
+ # @param key [Symbol] action key to update
+ # @param options [Hash] attributes to update
+ # @return [BulkAction, nil] the updated action or nil
+ def update_bulk_action(key, **options)
+ action = @bulk_actions[key.to_sym]
+ return nil unless action
+
+ options.each do |attr, value|
+ action.send("#{attr}=", value) if action.respond_to?("#{attr}=")
+ end
+
+ sort_bulk_actions!
+ action
+ end
+
+ # Find a bulk action by key
+ # @param key [Symbol] action key
+ # @return [BulkAction, nil]
+ def find_bulk_action(key)
+ @bulk_actions[key.to_sym]
+ end
+
+ # Get visible bulk actions for the given context
+ # @param view_context [Object, nil] view context for visibility checks
+ # @return [Array<BulkAction>]
+ def visible_bulk_actions(view_context = nil)
+ @bulk_actions.values.select { |a| a.visible?(view_context) }.sort_by(&:position)
+ end
+
+ # Check if bulk operations are enabled (has any bulk actions)
+ # @return [Boolean]
+ def bulk_operations_enabled?
+ @bulk_actions.any?
+ end
+
# Deep clone the registry
# @return [RecordList]
def deep_clone
- cloned = self.class.new(context, model_class: model_class, search_param: search_param, search_placeholder: search_placeholder)
+ cloned = self.class.new(context, model_class: model_class, search_param: search_param, search_placeholder: search_placeholder, row_actions: row_actions)
@columns.each do |key, column|
cloned.columns[key] = column.deep_clone
end
+ @bulk_actions.each do |key, action|
+ cloned.bulk_actions[key] = action.deep_clone
+ end
cloned
end
@@ -144,8 +236,13 @@ module Spree
@columns.clear
end
+ # Clear all bulk actions
+ def clear_bulk_actions
+ @bulk_actions.clear
+ end
+
def inspect
- "#<Spree::Admin::RecordList context=#{context} columns=#{@columns.size}>"
+ "#<Spree::Admin::RecordList context=#{context} columns=#{@columns.size} bulk_actions=#{@bulk_actions.size}>"
end
private
@@ -153,6 +250,10 @@ module Spree
def sort_columns!
@columns = @columns.sort_by { |_key, col| [col.position, col.key.to_s] }.to_h
end
+
+ def sort_bulk_actions!
+ @bulk_actions = @bulk_actions.sort_by { |_key, action| [action.position, action.key.to_s] }.to_h
+ end
end
end
end
diff --git a/admin/app/models/spree/admin/record_list/bulk_action.rb b/admin/app/models/spree/admin/record_list/bulk_action.rb
new file mode 100644
index 0000000000..1f6d096c78
--- /dev/null
+++ b/admin/app/models/spree/admin/record_list/bulk_action.rb
@@ -0,0 +1,79 @@
+module Spree
+ module Admin
+ class RecordList
+ class BulkAction
+ attr_accessor :key, :label, :label_options, :icon, :modal_path, :action_path, :position, :condition, :confirm, :method
+
+ def initialize(key, **options)
+ @key = key.to_sym
+ @label = options[:label]
+ @label_options = options[:label_options] || {}
+ @icon = options[:icon]
+ @modal_path = options[:modal_path]
+ @action_path = options[:action_path]
+ @position = options[:position] || 999
+ @condition = options.key?(:if) ? options[:if] : options[:condition]
+ @confirm = options[:confirm]
+ @method = options[:method] || :put
+ end
+
+ # Check if action is visible for the given context
+ # @param context [Object, nil] view context with access to helper methods
+ # @return [Boolean]
+ def visible?(context = nil)
+ return true if condition.nil?
+
+ if condition.respond_to?(:call)
+ if context&.respond_to?(:instance_exec)
+ context.instance_exec(&condition)
+ else
+ condition.call(context)
+ end
+ else
+ condition
+ end
+ end
+
+ # Resolve label (handles i18n keys)
+ # @return [String]
+ def resolve_label
+ if label.is_a?(String) && label.start_with?('admin.')
+ # Full translation key
+ Spree.t(label, **label_options.merge(default: key.to_s.humanize))
+ elsif label.is_a?(String) && label.present?
+ label
+ else
+ key_to_translate = label || key
+ Spree.t(key_to_translate, scope: 'admin.bulk_ops', default: key_to_translate.to_s.humanize, **label_options)
+ end
+ end
+
+ # Convert to hash
+ # @return [Hash]
+ def to_h
+ {
+ key: key,
+ label: label,
+ label_options: label_options,
+ icon: icon,
+ modal_path: modal_path,
+ action_path: action_path,
+ position: position,
+ confirm: confirm,
+ method: method
+ }
+ end
+
+ # Deep clone the action
+ # @return [BulkAction]
+ def deep_clone
+ self.class.new(key, to_h.merge(condition: condition))
+ end
+
+ def inspect
+ "#<Spree::Admin::RecordList::BulkAction key=#{key}>"
+ end
+ end
+ end
+ end
+end
diff --git a/admin/app/models/spree/admin/record_list/column.rb b/admin/app/models/spree/admin/record_list/column.rb
index 1bba790fee..d6b40ff2d7 100644
--- a/admin/app/models/spree/admin/record_list/column.rb
+++ b/admin/app/models/spree/admin/record_list/column.rb
@@ -7,7 +7,8 @@ module Spree
attr_accessor :key, :label, :type, :filter_type, :sortable, :filterable, :displayable, :default, :position,
:partial, :method, :width, :align, :format, :condition,
- :ransack_attribute, :operators, :value_options, :search_url
+ :ransack_attribute, :operators, :value_options, :search_url,
+ :sort_scope_asc, :sort_scope_desc
def initialize(key, **options)
@key = key.to_sym
@@ -29,6 +30,14 @@ module Spree
@value_options = options[:value_options]
@search_url = options[:search_url]
@operators = options[:operators] || default_operators_for_type
+ @sort_scope_asc = options[:sort_scope_asc]
+ @sort_scope_desc = options[:sort_scope_desc]
+ end
+
+ # Check if column uses custom sort scopes instead of ransack
+ # @return [Boolean]
+ def custom_sort?
+ sort_scope_asc.present? || sort_scope_desc.present?
end
def sortable?
@@ -133,7 +142,9 @@ module Spree
ransack_attribute: ransack_attribute,
operators: operators,
value_options: value_options,
- search_url: search_url
+ search_url: search_url,
+ sort_scope_asc: sort_scope_asc,
+ sort_scope_desc: sort_scope_desc
}
end
diff --git a/admin/app/views/spree/admin/products/_bulk_operations.html.erb b/admin/app/views/spree/admin/products/_bulk_operations.html.erb
deleted file mode 100644
index 51ffa2cee3..0000000000
--- a/admin/app/views/spree/admin/products/_bulk_operations.html.erb
+++ /dev/null
@@ -1,78 +0,0 @@
-<div id="bulk-panel" class="hidden" data-bulk-operation-target="panel">
- <div class="bulk-panel-container">
- <%= bulk_operations_counter %>
-
- <% if can?(:activate, Spree::Product) %>
- <%= bulk_action_link(
- Spree.t("admin.bulk_ops.products.title.set_status", status: :active),
- spree.bulk_modal_admin_products_path(kind: :set_status, status: :active),
- icon: "circle-check",
- url: spree.bulk_status_update_admin_products_path(status: :active)
- ) %>
- <% end %>
-
- <%= bulk_action_link(
- Spree.t("admin.bulk_ops.products.title.set_status", status: :draft),
- spree.bulk_modal_admin_products_path(kind: :set_status, status: :draft),
- icon: "circle-dotted",
- url: spree.bulk_status_update_admin_products_path(status: :draft)
- ) %>
-
- <%= dropdown direction: 'top', portal: false do %>
- <%= dropdown_toggle do %>
- <%= icon('dots-vertical', class: "mr-0") %>
- <% end %>
- <%= dropdown_menu class: 'mb-2' do %>
- <%= bulk_action_link(
- Spree.t("admin.bulk_ops.products.title.set_status", status: :archived),
- spree.bulk_modal_admin_products_path(kind: :set_status, status: :archived),
- icon: "archive",
- url: spree.bulk_status_update_admin_products_path(status: :archived),
- class: "dropdown-item"
- ) %>
-
- <% if can?(:manage, Spree::Classification) %>
- <div class="dropdown-divider"></div>
-
- <%= bulk_action_link(
- Spree.t("admin.bulk_ops.products.title.add_to_taxons"),
- spree.bulk_modal_admin_products_path(kind: :add_to_taxons),
- icon: "category-plus",
- url: spree.bulk_add_to_taxons_admin_products_path,
- class: "dropdown-item"
- ) %>
-
- <%= bulk_action_link(
- Spree.t("admin.bulk_ops.products.title.remove_from_taxons"),
- spree.bulk_modal_admin_products_path(kind: :remove_from_taxons),
- icon: "category-minus",
- url: spree.bulk_remove_from_taxons_admin_products_path,
- class: "dropdown-item"
- ) %>
- <% end %>
-
- <% if can?(:manage_tags, Spree::Product) %>
- <div class="dropdown-divider"></div>
-
- <%= bulk_action_link(
- Spree.t("admin.bulk_ops.products.title.add_tags"),
- spree.bulk_modal_admin_products_path(kind: :add_tags),
- icon: "tag-plus",
- url: spree.bulk_add_tags_admin_products_path,
- class: "dropdown-item"
- ) %>
-
- <%= bulk_action_link(
- Spree.t("admin.bulk_ops.products.title.remove_tags"),
- spree.bulk_modal_admin_products_path(kind: :remove_tags),
- icon: "tag-minus",
- url: spree.bulk_remove_tags_admin_products_path,
- class: "dropdown-item"
- ) %>
- <% end %>
- <% end %>
- <% end %>
-
- <%= bulk_operations_close_button %>
- </div>
-</div>
diff --git a/admin/app/views/spree/admin/record_lists/_column_selector.html.erb b/admin/app/views/spree/admin/record_lists/_column_selector.html.erb
index a20ff78551..48604ac358 100644
--- a/admin/app/views/spree/admin/record_lists/_column_selector.html.erb
+++ b/admin/app/views/spree/admin/record_lists/_column_selector.html.erb
@@ -1,9 +1,10 @@
-<%# locals: (record_list:, list_key:, selected_columns:) %>
+<%# locals: (record_list:, list_key:, selected_columns:, frame_name:) %>
<div data-controller="column-selector">
<%= form_with url: spree.admin_record_list_columns_path,
method: :post,
- data: { turbo_frame: '_top' } do |f| %>
+ data: { turbo_frame: frame_name } do |f| %>
<%= hidden_field_tag :list_key, list_key %>
+ <%= hidden_field_tag :redirect_url, request.fullpath %>
<%= dropdown(portal: false) do %>
<%= dropdown_toggle(class: 'btn-light btn-sm flex items-center gap-1') do %>
@@ -12,12 +13,10 @@
<%= icon 'chevron-down', class: 'w-3 h-3' %>
<% end %>
- <%= dropdown_menu(class: 'min-w-[250px] p-4') do %>
- <div class="mb-3">
- <h6 class="font-semibold text-gray-700 text-sm">
- <%= Spree.t('admin.record_lists.visible_columns') %>
- </h6>
- </div>
+ <%= dropdown_menu(class: 'min-w-[250px]') do %>
+ <h6 class="font-semibold text-gray-700 text-sm p-3">
+ <%= Spree.t('admin.record_lists.visible_columns') %>
+ </h6>
<div class="space-y-2 max-h-[300px] overflow-y-auto mb-4">
<% record_list.available_columns.each do |column| %>
@@ -38,7 +37,7 @@
<% end %>
</div>
- <div class="flex gap-2 pt-3 border-t border-gray-200">
+ <div class="flex gap-2 p-2 pt-3 border-t border-gray-200">
<button type="button"
class="btn btn-sm btn-light flex-1"
data-action="column-selector#reset">
diff --git a/admin/app/views/spree/admin/record_lists/_query_builder.html.erb b/admin/app/views/spree/admin/record_lists/_query_builder.html.erb
index 44e6da5942..9652a8c983 100644
--- a/admin/app/views/spree/admin/record_lists/_query_builder.html.erb
+++ b/admin/app/views/spree/admin/record_lists/_query_builder.html.erb
@@ -1,14 +1,18 @@
-<%# locals: (record_list:, list_key:) %>
+<%# locals: (record_list:, list_key:, frame_name:) %>
<% fields_json = query_builder_fields_json(record_list) %>
<% operators_json = query_builder_operators_json %>
<% current_state = params[:query_state] || '{}' %>
+<% filter_count = count_applied_filters(current_state) %>
-<div data-controller="dialog">
+<div data-controller="dialog" class="h-full">
<button type="button"
- class="btn btn-light btn-sm flex items-center gap-1"
+ class="btn btn-light btn-sm flex items-center gap-1 h-full"
data-action="dialog#open">
<%= icon 'filter', class: 'w-4 h-4' %>
<span><%= Spree.t('admin.record_lists.filters') %></span>
+ <% if filter_count > 0 %>
+ <span class="badge badge-primary rounded-full text-xs px-1.5 py-0.5 min-w-[1.25rem]"><%= filter_count %></span>
+ <% end %>
</button>
<dialog class="drawer"
diff --git a/admin/app/views/spree/admin/record_lists/_record_list.html.erb b/admin/app/views/spree/admin/record_lists/_record_list.html.erb
index cc1eae3542..c30ff645cf 100644
--- a/admin/app/views/spree/admin/record_lists/_record_list.html.erb
+++ b/admin/app/views/spree/admin/record_lists/_record_list.html.erb
@@ -7,29 +7,30 @@
<%= turbo_frame_tag frame_name, autoscroll: true, data: { autoscroll_block: :start, turbo_action: 'advance' } do %>
<div class="card-lg" data-controller="record-list" data-record-list-url-value="<%= request.path %>">
- <div class="p-4 border-b border-gray-200">
- <%= search_form_for @search, url: url_for, html: { data: { turbo_frame: frame_name, turbo_action: 'advance' } } do |f| %>
- <div class="flex flex-col lg:flex-row gap-3 items-start lg:items-center justify-between">
- <div class="flex gap-2 items-center flex-wrap grow">
- <div class="form-input flex items-center py-0 focus-within:outline-2 focus-within:outline-offset-2 focus-within:outline-blue-500 grow max-lg:max-w-[300px] mr-auto h-[2.125rem] transition-all duration-100 ease-in-out">
+ <div class="p-2 border-b border-gray-200">
+ <div class="flex flex-col lg:flex-row gap-3 items-start lg:items-center justify-between">
+ <div class="flex gap-2 items-center flex-wrap">
+ <%= search_form_for @search, url: url_for, html: { data: { turbo_frame: frame_name, turbo_action: 'advance' } } do |f| %>
+ <div class="form-input flex items-center py-0 focus-within:outline-2 focus-within:outline-offset-2 focus-within:outline-blue-500 lg:w-[300px] mr-auto h-[2.125rem] transition-all duration-100 ease-in-out">
<%= icon "search", class: "mr-4 text-gray-600" %>
<%= search_field_tag "q[#{search_param}]",
params.dig(:q, search_param),
class: "outline-none border-0 h-full focus:border-0 focus:ring-0 p-0 text-base w-full",
placeholder: search_placeholder %>
</div>
- <%= render 'spree/admin/record_lists/query_builder', record_list: record_list, list_key: list_key %>
- </div>
+ <% end %>
+ </div>
- <div class="flex gap-2 items-center">
- <%= record_list_sort_dropdown(record_list, current_sort) %>
- <%= render 'spree/admin/record_lists/column_selector',
- record_list: record_list,
- list_key: list_key,
- selected_columns: selected_columns %>
- </div>
+ <div class="flex gap-2 items-center h-full">
+ <%= render 'spree/admin/record_lists/query_builder', record_list: record_list, list_key: list_key, frame_name: frame_name %>
+ <%= record_list_sort_dropdown(record_list, current_sort) %>
+ <%= render 'spree/admin/record_lists/column_selector',
+ record_list: record_list,
+ list_key: list_key,
+ selected_columns: selected_columns,
+ frame_name: frame_name %>
</div>
- <% end %>
+ </div>
</div>
<% if collection.any? %>
diff --git a/admin/app/views/spree/admin/record_lists/_row_actions.html.erb b/admin/app/views/spree/admin/record_lists/_row_actions.html.erb
index 8744113f73..1048177a39 100644
--- a/admin/app/views/spree/admin/record_lists/_row_actions.html.erb
+++ b/admin/app/views/spree/admin/record_lists/_row_actions.html.erb
@@ -1,5 +1,5 @@
<%# locals: (record:) %>
-<div class="flex items-center justify-end gap-1 opacity-0 group-hover:opacity-100 transition-opacity">
+<div class="flex items-center justify-end gap-1">
<% if respond_to?(:edit_object_url) && can?(:update, record) %>
<%= link_to edit_object_url(record),
class: 'btn btn-sm btn-light',
@@ -8,17 +8,4 @@
<%= icon 'pencil', class: 'w-4 h-4' %>
<% end %>
<% end %>
-
- <% if respond_to?(:object_url) && can?(:destroy, record) %>
- <%= link_to object_url(record),
- method: :delete,
- class: 'btn btn-sm btn-light text-red-600 hover:text-red-700',
- title: Spree.t(:delete),
- data: {
- turbo_method: :delete,
- turbo_confirm: Spree.t(:are_you_sure)
- } do %>
- <%= icon 'trash', class: 'w-4 h-4' %>
- <% end %>
- <% end %>
</div>
diff --git a/admin/app/views/spree/admin/record_lists/_table.html.erb b/admin/app/views/spree/admin/record_lists/_table.html.erb
index b600ae176d..e709c7873e 100644
--- a/admin/app/views/spree/admin/record_lists/_table.html.erb
+++ b/admin/app/views/spree/admin/record_lists/_table.html.erb
@@ -1,30 +1,33 @@
<%# locals: (collection:, columns:, record_list:, bulk_operations: false, sortable: false) %>
-<% if bulk_operations %>
+<% has_bulk_actions = bulk_operations && record_list.bulk_operations_enabled? %>
+<% has_row_actions = record_list.row_actions? %>
+<% if has_bulk_actions %>
<div data-controller="bulk-operation bulk-dialog" data-action="keydown.esc->bulk-operation#cancel" tabindex="0">
<%= form_tag url_for(action: :index), method: :put, data: { 'bulk-operation-target': 'form' } do %>
<%= render 'spree/admin/shared/bulk_modal' %>
+ <%= render_bulk_actions_panel(record_list) %>
<div class="table-responsive">
<table class="table" id="listing_<%= controller_name %>">
<thead>
<tr>
+ <% if sortable %>
+ <th scope="col" class="w-5"></th>
+ <% end %>
+
<th scope="col" class="pr-0 w-5">
<%= bulk_operations_select_all_checkbox %>
</th>
<% columns.each do |column| %>
<th scope="col" class="<%= column_header_class(column) %>">
- <% if column.sortable? && @search %>
- <%= sort_link @search, column.ransack_attribute, column.resolve_label,
- default_order: 'desc',
- title: "#{controller_name}_listing_#{column.key}_title" %>
- <% else %>
- <%= column.resolve_label %>
- <% end %>
+ <%= column.resolve_label %>
</th>
<% end %>
- <th scope="col" class="w-10"></th>
+ <% if has_row_actions %>
+ <th scope="col" class="w-10"></th>
+ <% end %>
</tr>
</thead>
<tbody
@@ -41,8 +44,9 @@
locals: {
columns: columns,
record_list: record_list,
- bulk_operations: bulk_operations,
- sortable: sortable
+ bulk_operations: has_bulk_actions,
+ sortable: sortable,
+ row_actions: has_row_actions
} %>
</tbody>
</table>
@@ -54,19 +58,19 @@
<table class="table" id="listing_<%= controller_name %>">
<thead>
<tr>
+ <% if sortable %>
+ <th scope="col" class="w-5"></th>
+ <% end %>
+
<% columns.each do |column| %>
<th scope="col" class="<%= column_header_class(column) %>">
- <% if column.sortable? && @search %>
- <%= sort_link @search, column.ransack_attribute, column.resolve_label,
- default_order: 'desc',
- title: "#{controller_name}_listing_#{column.key}_title" %>
- <% else %>
- <%= column.resolve_label %>
- <% end %>
+ <%= column.resolve_label %>
</th>
<% end %>
- <th scope="col" class="w-10"></th>
+ <% if has_row_actions %>
+ <th scope="col" class="w-10"></th>
+ <% end %>
</tr>
</thead>
<tbody
@@ -84,7 +88,8 @@
columns: columns,
record_list: record_list,
bulk_operations: false,
- sortable: sortable
+ sortable: sortable,
+ row_actions: has_row_actions
} %>
</tbody>
</table>
diff --git a/admin/app/views/spree/admin/record_lists/_table_row.html.erb b/admin/app/views/spree/admin/record_lists/_table_row.html.erb
index b142816ac9..fe0d72943c 100644
--- a/admin/app/views/spree/admin/record_lists/_table_row.html.erb
+++ b/admin/app/views/spree/admin/record_lists/_table_row.html.erb
@@ -1,5 +1,11 @@
-<%# locals: (record:, columns:, record_list:, bulk_operations: false, sortable: false) %>
-<tr id="<%= dom_id record %>" class="group">
+<%# locals: (record:, columns:, record_list:, bulk_operations: false, sortable: false, row_actions: true) %>
+<tr id="<%= dom_id record %>" class="group"<% if sortable %> data-sortable-update-url="<%= edit_object_url(record) %>"<% end %>>
+ <% if sortable %>
+ <td class="w-5 move-handle text-center">
+ <%= icon('grip-vertical', class: 'rounded-md hover:bg-gray-100 p-2 cursor-grab') %>
+ </td>
+ <% end %>
+
<% if bulk_operations %>
<td class="pr-0 w-5">
<%= bulk_operations_checkbox(record) %>
@@ -7,7 +13,7 @@
<% end %>
<% columns.each_with_index do |column, index| %>
- <td class="<%= column_cell_class(column) %>">
+ <td class="<%= column_cell_class(column) %>" id="<%= dom_id record, column.key %>">
<% if index == 0 %>
<%= link_to edit_object_url(record),
class: 'flex items-center w-full no-underline hover:text-primary',
@@ -20,7 +26,9 @@
</td>
<% end %>
- <td class="w-10 text-right">
- <%= render 'spree/admin/record_lists/row_actions', record: record %>
- </td>
+ <% if row_actions %>
+ <td class="w-10 text-right">
+ <%= render 'spree/admin/record_lists/row_actions', record: record %>
+ </td>
+ <% end %>
</tr>
diff --git a/admin/config/initializers/record_lists.rb b/admin/config/initializers/record_lists.rb
index 5da46ee761..a57aa897db 100644
--- a/admin/config/initializers/record_lists.rb
+++ b/admin/config/initializers/record_lists.rb
@@ -68,6 +68,8 @@ Rails.application.config.after_initialize do
default: true,
position: 40,
ransack_attribute: 'master_price',
+ sort_scope_asc: :ascend_by_price,
+ sort_scope_desc: :descend_by_price,
method: ->(product) { product.price }
Spree.admin.record_lists.products.add :created_at,
@@ -126,6 +128,64 @@ Rails.application.config.after_initialize do
search_url: '/admin/tags/select_options.json?taggable_type=Spree::Product',
method: ->(product) { product.tag_list.join(', ') }
+ # Products bulk actions
+ Spree.admin.record_lists.products.add_bulk_action :set_active,
+ label: 'admin.bulk_ops.products.title.set_status',
+ label_options: { status: :active },
+ icon: 'circle-check',
+ modal_path: '/admin/products/bulk_modal?kind=set_status&status=active',
+ action_path: '/admin/products/bulk_status_update?status=active',
+ position: 10,
+ condition: -> { can?(:activate, Spree::Product) }
+
+ Spree.admin.record_lists.products.add_bulk_action :set_draft,
+ label: 'admin.bulk_ops.products.title.set_status',
+ label_options: { status: :draft },
+ icon: 'circle-dotted',
+ modal_path: '/admin/products/bulk_modal?kind=set_status&status=draft',
+ action_path: '/admin/products/bulk_status_update?status=draft',
+ position: 20
+
+ Spree.admin.record_lists.products.add_bulk_action :set_archived,
+ label: 'admin.bulk_ops.products.title.set_status',
+ label_options: { status: :archived },
+ icon: 'archive',
+ modal_path: '/admin/products/bulk_modal?kind=set_status&status=archived',
+ action_path: '/admin/products/bulk_status_update?status=archived',
+ position: 30
+
+ Spree.admin.record_lists.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',
+ position: 40,
+ condition: -> { can?(:manage, Spree::Classification) }
+
+ Spree.admin.record_lists.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',
+ position: 50,
+ condition: -> { can?(:manage, Spree::Classification) }
+
+ Spree.admin.record_lists.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',
+ position: 60,
+ condition: -> { can?(:manage_tags, Spree::Product) }
+
+ Spree.admin.record_lists.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',
+ position: 70,
+ condition: -> { can?(:manage_tags, Spree::Product) }
+
# Register Orders record list
Spree.admin.record_lists.register(:orders, model_class: Spree::Order, search_param: :number_or_email_cont)
@@ -255,6 +315,25 @@ Rails.application.config.after_initialize do
default: true,
position: 40
+ # Users bulk actions
+ Spree.admin.record_lists.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',
+ method: :post,
+ position: 10,
+ condition: -> { can?(:manage_tags, Spree.user_class) }
+
+ Spree.admin.record_lists.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',
+ method: :post,
+ position: 20,
+ condition: -> { can?(:manage_tags, Spree.user_class) }
+
# Register Promotions record list
Spree.admin.record_lists.register(:promotions, model_class: Spree::Promotion, search_param: :name_cont)
diff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rb
index 4e6c863819..c387697204 100644
--- a/core/app/models/concerns/spree/product_scopes.rb
+++ b/core/app/models/concerns/spree/product_scopes.rb
@@ -56,6 +56,28 @@ module Spree
order(price_table_name => { amount: :desc })
end
+ # Price sorting scopes that use subqueries to get prices across all variants
+ # These ensure products with only variant prices (no master price) are included in results
+ add_search_scope :ascend_by_price do
+ price_subquery = Price
+ .non_zero
+ .joins(:variant)
+ .where("#{Variant.table_name}.product_id = #{Product.table_name}.id")
+ .select('MIN(amount)')
+
+ order(Arel.sql("COALESCE((#{price_subquery.to_sql}), 999999999) ASC"))
+ end
+
+ add_search_scope :descend_by_price do
+ price_subquery = Price
+ .non_zero
+ .joins(:variant)
+ .where("#{Variant.table_name}.product_id = #{Product.table_name}.id")
+ .select('MAX(amount)')
+
+ order(Arel.sql("COALESCE((#{price_subquery.to_sql}), 0) DESC"))
+ end
+
add_search_scope :price_between do |low, high|
where(Price.table_name => { amount: low..high })
end