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.

Added Admin Table specs, fixed translations

Damian Legawiec2c9bddaec873
diff --git a/admin/app/models/spree/admin/table/bulk_action.rb b/admin/app/models/spree/admin/table/bulk_action.rb
index 6b6acaf31c..98c477f725 100644
--- a/admin/app/models/spree/admin/table/bulk_action.rb
+++ b/admin/app/models/spree/admin/table/bulk_action.rb
@@ -67,7 +67,8 @@ module Spree
# Deep clone the action
# @return [BulkAction]
def deep_clone
- self.class.new(key, to_h.merge(condition: condition))
+ options = to_h.except(:key).merge(condition: condition)
+ self.class.new(key, **options)
end
def inspect
diff --git a/admin/app/models/spree/admin/table/column.rb b/admin/app/models/spree/admin/table/column.rb
index 650ba9f762..cfa92a686d 100644
--- a/admin/app/models/spree/admin/table/column.rb
+++ b/admin/app/models/spree/admin/table/column.rb
@@ -152,7 +152,8 @@ module Spree
# Deep clone the column
# @return [Column]
def deep_clone
- self.class.new(key, to_h.merge(condition: condition, method: method))
+ options = to_h.except(:key).merge(condition: condition, method: method)
+ self.class.new(key, **options)
end
def inspect
diff --git a/admin/config/locales/en.yml b/admin/config/locales/en.yml
index 29f492a901..7023e22374 100644
--- a/admin/config/locales/en.yml
+++ b/admin/config/locales/en.yml
@@ -260,6 +260,40 @@ en:
storefront: Storefront
successful: Successful
successfully_reset_digital_links_limit: Download limits have been reset
+ tables:
+ add_filter: Add filter
+ add_or_group: Add OR group
+ apply: Apply
+ apply_filters: Apply Filters
+ ascending: Ascending
+ clear_all: Clear All
+ columns: Columns
+ descending: Descending
+ enter_value: Enter value...
+ filter_builder: Filter Builder
+ filters: Filters
+ match: Match
+ of_the_following: of the following
+ operators:
+ contains: contains
+ does_not_contain: does not contain
+ ends_with: ends with
+ equals: equals
+ greater_than: greater than
+ greater_than_or_equal: greater than or equal
+ is_any_of: is any of
+ is_empty: is empty
+ is_none_of: is none of
+ is_not_empty: is not empty
+ less_than: less than
+ less_than_or_equal: less than or equal
+ not_equals: does not equal
+ starts_with: starts with
+ order: Order
+ reset_to_default: Reset to Default
+ select_field: Select field...
+ sort_by: Sort by
+ visible_columns: Visible Columns
taxon_rules:
all_conditions: all conditions
any_condition: any condition
@@ -338,37 +372,3 @@ en:
subscriptions: Subscriptions
time_of_last_event: Time of Last Event
webhooks_events: Webhooks Events
- tables:
- add_filter: Add filter
- add_or_group: Add OR group
- apply: Apply
- apply_filters: Apply Filters
- clear_all: Clear All
- columns: Columns
- enter_value: Enter value...
- filter_builder: Filter Builder
- filters: Filters
- match: Match
- of_the_following: of the following
- operators:
- contains: contains
- does_not_contain: does not contain
- ends_with: ends with
- equals: equals
- greater_than: greater than
- greater_than_or_equal: greater than or equal
- is_any_of: is any of
- is_empty: is empty
- is_none_of: is none of
- is_not_empty: is not empty
- less_than: less than
- less_than_or_equal: less than or equal
- not_equals: does not equal
- starts_with: starts with
- reset_to_default: Reset to Default
- select_field: Select field...
- sort_by: Sort by
- order: Order
- ascending: Ascending
- descending: Descending
- visible_columns: Visible Columns
diff --git a/admin/spec/models/spree/admin/table/bulk_action_spec.rb b/admin/spec/models/spree/admin/table/bulk_action_spec.rb
new file mode 100644
index 0000000000..c7458976a2
--- /dev/null
+++ b/admin/spec/models/spree/admin/table/bulk_action_spec.rb
@@ -0,0 +1,153 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::Table::BulkAction do
+ describe '#initialize' do
+ it 'sets default values' do
+ action = described_class.new(:delete)
+
+ expect(action.key).to eq(:delete)
+ expect(action.position).to eq(999)
+ expect(action.method).to eq(:put)
+ end
+
+ it 'accepts custom options' do
+ action = described_class.new(:export,
+ label: 'Export Products',
+ icon: 'download',
+ modal_path: '/admin/exports/new',
+ action_path: '/admin/exports',
+ position: 10,
+ method: :post
+ )
+
+ 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)
+ end
+
+ it 'converts key to symbol' do
+ action = described_class.new('delete')
+ expect(action.key).to eq(:delete)
+ end
+ end
+
+ describe '#visible?' do
+ it 'returns true when no condition' do
+ action = described_class.new(:delete)
+ expect(action.visible?).to be true
+ end
+
+ it 'returns false when condition is false' do
+ action = described_class.new(:delete, if: false)
+ expect(action.visible?).to be false
+ end
+
+ it 'evaluates lambda condition with context' do
+ context = Object.new
+ action = described_class.new(:delete, if: -> { true })
+ expect(action.visible?(context)).to be true
+ end
+
+ it 'evaluates condition in view context' do
+ context = Object.new
+ def context.can_delete?
+ true
+ end
+
+ action = described_class.new(:delete, if: -> { can_delete? })
+ expect(action.visible?(context)).to be true
+ end
+
+ it 'returns false when condition lambda returns false' do
+ context = Object.new
+ action = described_class.new(:delete, if: -> { false })
+ expect(action.visible?(context)).to be false
+ end
+ end
+
+ describe '#resolve_label' do
+ it 'returns string label directly when not a translation key' do
+ action = described_class.new(:delete, label: 'Delete Selected')
+ expect(action.resolve_label).to eq('Delete Selected')
+ end
+
+ it 'translates admin translation key' do
+ action = described_class.new(:delete, label: 'admin.bulk_ops.delete')
+ # Will use Spree.t which falls back to humanized key if translation not found
+ expect(action.resolve_label).to be_a(String)
+ end
+
+ it 'uses key as fallback' do
+ action = described_class.new(:delete_selected)
+ expect(action.resolve_label).to include('Delete')
+ end
+
+ it 'supports label_options' do
+ action = described_class.new(:delete, label: :delete_count, label_options: { count: 5 })
+ # Translation with options
+ expect(action.resolve_label).to be_a(String)
+ end
+ end
+
+ describe '#to_h' do
+ it 'returns hash representation' do
+ action = described_class.new(:delete,
+ label: 'Delete',
+ icon: 'trash',
+ modal_path: '/modal',
+ action_path: '/action',
+ position: 10,
+ confirm: 'Are you sure?',
+ method: :delete
+ )
+
+ hash = action.to_h
+
+ expect(hash[:key]).to eq(:delete)
+ expect(hash[:label]).to eq('Delete')
+ expect(hash[:icon]).to eq('trash')
+ expect(hash[:modal_path]).to eq('/modal')
+ expect(hash[:action_path]).to eq('/action')
+ expect(hash[:position]).to eq(10)
+ expect(hash[:confirm]).to eq('Are you sure?')
+ expect(hash[:method]).to eq(:delete)
+ end
+ end
+
+ describe '#deep_clone' do
+ it 'creates a deep copy' do
+ original = described_class.new(:delete, label: 'Original', position: 10)
+ cloned = original.deep_clone
+
+ cloned.label = 'Changed'
+
+ expect(original.label).to eq('Original')
+ expect(cloned.label).to eq('Changed')
+ expect(cloned.key).to eq(:delete)
+ expect(cloned.position).to eq(10)
+ end
+
+ it 'preserves condition' do
+ condition = -> { true }
+ original = described_class.new(:delete, if: condition, label: 'Delete')
+ cloned = original.deep_clone
+
+ expect(cloned.condition).to eq(condition)
+ end
+ end
+
+ describe '#confirm' do
+ it 'returns nil when not set' do
+ action = described_class.new(:delete)
+ expect(action.confirm).to be_nil
+ end
+
+ it 'returns confirmation message when set' do
+ action = described_class.new(:delete, confirm: 'Are you sure you want to delete?')
+ expect(action.confirm).to eq('Are you sure you want to delete?')
+ end
+ end
+end
diff --git a/admin/spec/models/spree/admin/table/column_spec.rb b/admin/spec/models/spree/admin/table/column_spec.rb
new file mode 100644
index 0000000000..11bf89c9ca
--- /dev/null
+++ b/admin/spec/models/spree/admin/table/column_spec.rb
@@ -0,0 +1,249 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::Table::Column do
+ describe '#initialize' do
+ it 'sets default values' do
+ column = described_class.new(:name)
+
+ expect(column.key).to eq(:name)
+ expect(column.type).to eq(:string)
+ expect(column.filter_type).to eq(:string)
+ expect(column.sortable).to be true
+ expect(column.filterable).to be true
+ expect(column.displayable).to be true
+ expect(column.default).to be false
+ expect(column.align).to eq(:left)
+ expect(column.ransack_attribute).to eq('name')
+ end
+
+ it 'accepts custom options' do
+ column = described_class.new(:price,
+ label: 'Product Price',
+ type: :currency,
+ sortable: false,
+ filterable: true,
+ default: true,
+ position: 10,
+ align: :right,
+ ransack_attribute: 'master_price'
+ )
+
+ expect(column.label).to eq('Product Price')
+ expect(column.type).to eq(:currency)
+ expect(column.filter_type).to eq(:currency)
+ expect(column.sortable).to be false
+ expect(column.default).to be true
+ expect(column.position).to eq(10)
+ expect(column.align).to eq(:right)
+ expect(column.ransack_attribute).to eq('master_price')
+ end
+
+ it 'uses type as filter_type by default' do
+ column = described_class.new(:status, type: :status)
+
+ expect(column.filter_type).to eq(:status)
+ end
+
+ it 'allows different filter_type than type' do
+ column = described_class.new(:status, type: :custom, filter_type: :select)
+
+ expect(column.type).to eq(:custom)
+ expect(column.filter_type).to eq(:select)
+ end
+ end
+
+ describe '#sortable?' do
+ it 'returns true when sortable' do
+ column = described_class.new(:name, sortable: true)
+ expect(column.sortable?).to be true
+ end
+
+ it 'returns false when not sortable' do
+ column = described_class.new(:name, sortable: false)
+ expect(column.sortable?).to be false
+ end
+ end
+
+ describe '#filterable?' do
+ it 'returns true when filterable' do
+ column = described_class.new(:name, filterable: true)
+ expect(column.filterable?).to be true
+ end
+
+ it 'returns false when not filterable' do
+ column = described_class.new(:name, filterable: false)
+ expect(column.filterable?).to be false
+ end
+ end
+
+ describe '#default?' do
+ it 'returns true when default' do
+ column = described_class.new(:name, default: true)
+ expect(column.default?).to be true
+ end
+
+ it 'returns false when not default' do
+ column = described_class.new(:name, default: false)
+ expect(column.default?).to be false
+ end
+ end
+
+ describe '#custom_sort?' do
+ it 'returns true when sort_scope_asc is present' do
+ column = described_class.new(:price, sort_scope_asc: :ascend_by_price)
+ expect(column.custom_sort?).to be true
+ end
+
+ it 'returns true when sort_scope_desc is present' do
+ column = described_class.new(:price, sort_scope_desc: :descend_by_price)
+ expect(column.custom_sort?).to be true
+ end
+
+ it 'returns false when no sort scopes' do
+ column = described_class.new(:name)
+ expect(column.custom_sort?).to be false
+ end
+ end
+
+ describe '#visible?' do
+ it 'returns true when no condition' do
+ column = described_class.new(:name)
+ expect(column.visible?).to be true
+ end
+
+ it 'returns false when condition is false' do
+ column = described_class.new(:name, if: false)
+ expect(column.visible?).to be false
+ end
+
+ it 'evaluates lambda condition with context' do
+ context = Object.new
+ column = described_class.new(:name, if: -> { true })
+ expect(column.visible?(context)).to be true
+ end
+
+ it 'evaluates condition in view context' do
+ context = Object.new
+ def context.admin?
+ true
+ end
+
+ column = described_class.new(:admin_only, if: -> { admin? })
+ expect(column.visible?(context)).to be true
+ end
+ end
+
+ describe '#resolve_label' do
+ it 'returns string label directly' do
+ column = described_class.new(:name, label: 'Custom Label')
+ expect(column.resolve_label).to eq('Custom Label')
+ end
+
+ it 'translates symbol label' do
+ column = described_class.new(:name, label: :product_name)
+ # Uses Spree.t which will return the translated value or humanized key
+ expect(column.resolve_label).to be_a(String)
+ end
+
+ it 'uses key as fallback' do
+ column = described_class.new(:custom_field)
+ expect(column.resolve_label).to include('Custom')
+ end
+ end
+
+ describe '#resolve_value' do
+ let(:product) { build(:product, name: 'Test Product') }
+
+ it 'calls method on record' do
+ column = described_class.new(:name)
+ expect(column.resolve_value(product)).to eq('Test Product')
+ end
+
+ it 'uses custom method' do
+ column = described_class.new(:display_name, method: :name)
+ expect(column.resolve_value(product)).to eq('Test Product')
+ end
+
+ it 'evaluates lambda method' do
+ column = described_class.new(:upper_name, method: ->(record) { record.name.upcase })
+ expect(column.resolve_value(product)).to eq('TEST PRODUCT')
+ end
+
+ it 'evaluates lambda in view context' do
+ context = Object.new
+ def context.truncate(str, length:)
+ str[0...length]
+ end
+
+ column = described_class.new(:truncated, method: ->(record) { truncate(record.name, length: 4) })
+ expect(column.resolve_value(product, context)).to eq('Test')
+ end
+ end
+
+ describe '#default_operators_for_type' do
+ it 'returns string operators for string type' do
+ column = described_class.new(:name, type: :string)
+ expect(column.operators).to include(:eq, :cont, :start, :end)
+ end
+
+ it 'returns number operators for number type' do
+ column = described_class.new(:quantity, type: :number)
+ expect(column.operators).to include(:eq, :gt, :lt, :gteq, :lteq)
+ end
+
+ it 'returns date operators for date type' do
+ column = described_class.new(:created_at, type: :date)
+ expect(column.operators).to include(:eq, :gt, :lt)
+ expect(column.operators).not_to include(:cont)
+ end
+
+ it 'returns status operators for status type' do
+ column = described_class.new(:status, type: :status)
+ expect(column.operators).to include(:eq, :not_eq, :in, :not_in)
+ end
+
+ it 'returns boolean operators for boolean type' do
+ column = described_class.new(:active, type: :boolean)
+ expect(column.operators).to eq([:eq])
+ end
+
+ it 'returns autocomplete operators for autocomplete type' do
+ column = described_class.new(:taxon, filter_type: :autocomplete)
+ expect(column.operators).to eq([:in, :not_in])
+ end
+ end
+
+ describe '#to_h' do
+ it 'returns hash representation' do
+ column = described_class.new(:name,
+ label: 'Name',
+ type: :string,
+ sortable: true,
+ default: true,
+ position: 10
+ )
+
+ hash = column.to_h
+ expect(hash[:key]).to eq(:name)
+ expect(hash[:label]).to eq('Name')
+ expect(hash[:type]).to eq(:string)
+ expect(hash[:sortable]).to be true
+ expect(hash[:default]).to be true
+ expect(hash[:position]).to eq(10)
+ end
+ end
+
+ describe '#deep_clone' do
+ it 'creates a deep copy' do
+ original = described_class.new(:name, label: 'Original', default: true, position: 10)
+ cloned = original.deep_clone
+
+ cloned.label = 'Changed'
+
+ expect(original.label).to eq('Original')
+ expect(cloned.label).to eq('Changed')
+ expect(cloned.key).to eq(:name)
+ expect(cloned.default).to be true
+ end
+ end
+end
diff --git a/admin/spec/models/spree/admin/table/filter_group_spec.rb b/admin/spec/models/spree/admin/table/filter_group_spec.rb
new file mode 100644
index 0000000000..ea16b7470e
--- /dev/null
+++ b/admin/spec/models/spree/admin/table/filter_group_spec.rb
@@ -0,0 +1,202 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::Table::FilterGroup do
+ describe '#initialize' do
+ it 'sets default values' do
+ group = described_class.new
+
+ expect(group.combinator).to eq(:and)
+ expect(group.filters).to eq([])
+ expect(group.groups).to eq([])
+ expect(group.id).to be_present
+ end
+
+ it 'accepts custom options' do
+ group = described_class.new(combinator: :or, id: 'custom-id')
+
+ expect(group.combinator).to eq(:or)
+ expect(group.id).to eq('custom-id')
+ end
+
+ it 'converts combinator to symbol' do
+ group = described_class.new(combinator: 'or')
+ expect(group.combinator).to eq(:or)
+ end
+ end
+
+ describe '#add_filter' do
+ it 'adds a filter to the group' do
+ group = described_class.new
+ filter = Spree::Admin::Table::Filter.new(field: 'name')
+
+ group.add_filter(filter)
+
+ expect(group.filters).to include(filter)
+ end
+ end
+
+ describe '#add_group' do
+ it 'adds a nested group' do
+ parent = described_class.new
+ child = described_class.new(combinator: :or)
+
+ parent.add_group(child)
+
+ expect(parent.groups).to include(child)
+ end
+ end
+
+ describe '#remove_filter' do
+ it 'removes filter by id' do
+ group = described_class.new
+ filter1 = Spree::Admin::Table::Filter.new(field: 'name', id: 'f1')
+ filter2 = Spree::Admin::Table::Filter.new(field: 'status', id: 'f2')
+
+ group.add_filter(filter1)
+ group.add_filter(filter2)
+ group.remove_filter('f1')
+
+ expect(group.filters.map(&:id)).to eq(['f2'])
+ end
+ end
+
+ describe '#remove_group' do
+ it 'removes group by id' do
+ parent = described_class.new
+ child1 = described_class.new(id: 'g1')
+ child2 = described_class.new(id: 'g2')
+
+ parent.add_group(child1)
+ parent.add_group(child2)
+ parent.remove_group('g1')
+
+ expect(parent.groups.map(&:id)).to eq(['g2'])
+ end
+ end
+
+ describe '#empty?' do
+ it 'returns true when no filters or groups' do
+ group = described_class.new
+ expect(group.empty?).to be true
+ end
+
+ it 'returns false when has filters' do
+ group = described_class.new
+ group.add_filter(Spree::Admin::Table::Filter.new(field: 'name'))
+ expect(group.empty?).to be false
+ end
+
+ it 'returns false when has nested groups' do
+ group = described_class.new
+ group.add_group(described_class.new)
+ expect(group.empty?).to be false
+ end
+ end
+
+ describe '#to_ransack_params' do
+ context 'with AND combinator' do
+ it 'returns merged filter params' do
+ group = described_class.new(combinator: :and)
+ group.add_filter(Spree::Admin::Table::Filter.new(field: 'name', operator: :cont, value: 'test'))
+ group.add_filter(Spree::Admin::Table::Filter.new(field: 'status', operator: :eq, value: 'active'))
+
+ params = group.to_ransack_params
+
+ expect(params).to eq({
+ 'name_cont' => 'test',
+ 'status_eq' => 'active'
+ })
+ end
+
+ it 'handles nested OR groups' do
+ group = described_class.new(combinator: :and)
+ group.add_filter(Spree::Admin::Table::Filter.new(field: 'name', operator: :cont, value: 'test'))
+
+ or_group = described_class.new(combinator: :or)
+ or_group.add_filter(Spree::Admin::Table::Filter.new(field: 'status', operator: :eq, value: 'active'))
+ or_group.add_filter(Spree::Admin::Table::Filter.new(field: 'status', operator: :eq, value: 'draft'))
+ group.add_group(or_group)
+
+ params = group.to_ransack_params
+
+ expect(params['name_cont']).to eq('test')
+ expect(params[:g]).to be_present
+ end
+ end
+
+ context 'with OR combinator' do
+ it 'returns ransack grouping format' do
+ group = described_class.new(combinator: :or)
+ group.add_filter(Spree::Admin::Table::Filter.new(field: 'name', operator: :cont, value: 'test'))
+ group.add_filter(Spree::Admin::Table::Filter.new(field: 'description', operator: :cont, value: 'test'))
+
+ params = group.to_ransack_params
+
+ expect(params[:g]).to be_present
+ expect(params[:g]['0'][:m]).to eq('or')
+ end
+ end
+ end
+
+ describe '#to_h' do
+ it 'returns hash representation' do
+ group = described_class.new(combinator: :and, id: 'root')
+ filter = Spree::Admin::Table::Filter.new(field: 'name', id: 'f1')
+ group.add_filter(filter)
+
+ hash = group.to_h
+
+ expect(hash[:combinator]).to eq(:and)
+ expect(hash[:id]).to eq('root')
+ expect(hash[:filters].first[:field]).to eq('name')
+ expect(hash[:groups]).to eq([])
+ end
+ end
+
+ describe '.from_params' do
+ it 'creates group from params hash' do
+ params = {
+ combinator: 'and',
+ id: 'root',
+ filters: [
+ { field: 'name', operator: 'cont', value: 'test', id: 'f1' }
+ ],
+ groups: []
+ }
+
+ group = described_class.from_params(params)
+
+ expect(group.combinator).to eq(:and)
+ expect(group.id).to eq('root')
+ expect(group.filters.size).to eq(1)
+ expect(group.filters.first.field).to eq('name')
+ end
+
+ it 'handles nested groups' do
+ params = {
+ combinator: 'and',
+ filters: [],
+ groups: [
+ {
+ combinator: 'or',
+ filters: [
+ { field: 'status', operator: 'eq', value: 'active' }
+ ],
+ groups: []
+ }
+ ]
+ }
+
+ group = described_class.from_params(params)
+
+ expect(group.groups.size).to eq(1)
+ expect(group.groups.first.combinator).to eq(:or)
+ expect(group.groups.first.filters.size).to eq(1)
+ end
+
+ it 'returns empty group for blank params' do
+ group = described_class.from_params(nil)
+ expect(group.empty?).to be true
+ end
+ end
+end
diff --git a/admin/spec/models/spree/admin/table/filter_spec.rb b/admin/spec/models/spree/admin/table/filter_spec.rb
new file mode 100644
index 0000000000..ba637d5043
--- /dev/null
+++ b/admin/spec/models/spree/admin/table/filter_spec.rb
@@ -0,0 +1,184 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::Table::Filter do
+ describe '#initialize' do
+ it 'sets default values' do
+ filter = described_class.new
+
+ expect(filter.field).to be_nil
+ expect(filter.operator).to eq(:eq)
+ expect(filter.value).to be_nil
+ expect(filter.id).to be_present
+ end
+
+ it 'accepts custom options' do
+ filter = described_class.new(
+ field: 'name',
+ operator: :cont,
+ value: 'test',
+ id: 'custom-id'
+ )
+
+ expect(filter.field).to eq('name')
+ expect(filter.operator).to eq(:cont)
+ expect(filter.value).to eq('test')
+ expect(filter.id).to eq('custom-id')
+ end
+
+ it 'converts operator to symbol' do
+ filter = described_class.new(operator: 'cont')
+ expect(filter.operator).to eq(:cont)
+ end
+ end
+
+ describe '#to_ransack_param' do
+ it 'returns empty hash when field is blank' do
+ filter = described_class.new(field: nil, value: 'test')
+ expect(filter.to_ransack_param).to eq({})
+ end
+
+ it 'generates correct param for eq operator' do
+ filter = described_class.new(field: 'name', operator: :eq, value: 'Test')
+ expect(filter.to_ransack_param).to eq({ 'name_eq' => 'Test' })
+ end
+
+ it 'generates correct param for cont operator' do
+ filter = described_class.new(field: 'name', operator: :cont, value: 'Test')
+ expect(filter.to_ransack_param).to eq({ 'name_cont' => 'Test' })
+ end
+
+ it 'generates correct param for in operator' do
+ filter = described_class.new(field: 'status', operator: :in, value: %w[active draft])
+ expect(filter.to_ransack_param).to eq({ 'status_in' => %w[active draft] })
+ end
+
+ it 'generates correct param for not_in operator' do
+ filter = described_class.new(field: 'status', operator: :not_in, value: %w[archived])
+ expect(filter.to_ransack_param).to eq({ 'status_not_in' => ['archived'] })
+ end
+
+ it 'generates correct param for null operator' do
+ filter = described_class.new(field: 'deleted_at', operator: :null)
+ expect(filter.to_ransack_param).to eq({ 'deleted_at_null' => true })
+ end
+
+ it 'generates correct param for not_null operator' do
+ filter = described_class.new(field: 'completed_at', operator: :not_null)
+ expect(filter.to_ransack_param).to eq({ 'completed_at_not_null' => true })
+ end
+
+ it 'generates correct param for comparison operators' do
+ filter = described_class.new(field: 'price', operator: :gt, value: 100)
+ expect(filter.to_ransack_param).to eq({ 'price_gt' => 100 })
+ end
+
+ it 'extracts ids from autocomplete array values' do
+ filter = described_class.new(
+ field: 'taxon_id',
+ operator: :in,
+ value: [{ 'id' => 1, 'name' => 'Category' }, { 'id' => 2, 'name' => 'Brand' }]
+ )
+ expect(filter.to_ransack_param).to eq({ 'taxon_id_in' => [1, 2] })
+ end
+
+ it 'handles single value arrays for eq operator' do
+ filter = described_class.new(
+ field: 'taxon_id',
+ operator: :eq,
+ value: [{ 'id' => 1, 'name' => 'Category' }]
+ )
+ expect(filter.to_ransack_param).to eq({ 'taxon_id_eq' => 1 })
+ end
+ end
+
+ describe '#operator_label' do
+ it 'returns human readable label for operator' do
+ filter = described_class.new(operator: :cont)
+ expect(filter.operator_label).to eq('contains')
+ end
+
+ it 'returns humanized operator for unknown operator' do
+ filter = described_class.new(operator: :unknown)
+ expect(filter.operator_label).to eq('Unknown')
+ end
+ end
+
+ describe '#requires_value?' do
+ it 'returns true for eq operator' do
+ filter = described_class.new(operator: :eq)
+ expect(filter.requires_value?).to be true
+ end
+
+ it 'returns true for cont operator' do
+ filter = described_class.new(operator: :cont)
+ expect(filter.requires_value?).to be true
+ end
+
+ it 'returns false for null operator' do
+ filter = described_class.new(operator: :null)
+ expect(filter.requires_value?).to be false
+ end
+
+ it 'returns false for not_null operator' do
+ filter = described_class.new(operator: :not_null)
+ expect(filter.requires_value?).to be false
+ end
+ end
+
+ describe '#to_h' do
+ it 'returns hash representation' do
+ filter = described_class.new(
+ field: 'name',
+ operator: :cont,
+ value: 'test',
+ id: 'filter-1'
+ )
+
+ hash = filter.to_h
+ expect(hash[:field]).to eq('name')
+ expect(hash[:operator]).to eq(:cont)
+ expect(hash[:value]).to eq('test')
+ expect(hash[:id]).to eq('filter-1')
+ end
+ end
+
+ describe '.from_params' do
+ it 'creates filter from params hash' do
+ params = { field: 'name', operator: 'cont', value: 'test', id: 'f1' }
+ filter = described_class.from_params(params)
+
+ expect(filter.field).to eq('name')
+ expect(filter.operator).to eq(:cont)
+ expect(filter.value).to eq('test')
+ expect(filter.id).to eq('f1')
+ end
+
+ it 'uses default operator when not provided' do
+ params = { field: 'name', value: 'test' }
+ filter = described_class.from_params(params)
+
+ expect(filter.operator).to eq(:eq)
+ end
+
+ it 'returns nil for non-hash params' do
+ expect(described_class.from_params(nil)).to be_nil
+ expect(described_class.from_params('invalid')).to be_nil
+ end
+ end
+
+ describe '.operators_for_select' do
+ it 'returns array of operator options' do
+ options = described_class.operators_for_select
+
+ expect(options).to be_an(Array)
+ expect(options.first).to include(:value, :label, :no_value)
+ end
+
+ it 'includes all defined operators' do
+ options = described_class.operators_for_select
+ values = options.map { |o| o[:value] }
+
+ expect(values).to include('eq', 'not_eq', 'cont', 'in', 'not_in', 'null', 'not_null')
+ end
+ end
+end
diff --git a/admin/spec/models/spree/admin/table/query_builder_spec.rb b/admin/spec/models/spree/admin/table/query_builder_spec.rb
new file mode 100644
index 0000000000..4cf43717f2
--- /dev/null
+++ b/admin/spec/models/spree/admin/table/query_builder_spec.rb
@@ -0,0 +1,192 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::Table::QueryBuilder do
+ let(:table) { Spree::Admin::Table.new(:products, model_class: Spree::Product) }
+ let(:query_builder) { described_class.new(table) }
+
+ before do
+ table.add(:name, label: 'Name', type: :string, filterable: true)
+ table.add(:status, label: 'Status', type: :status, filterable: true, value_options: %w[active draft archived])
+ table.add(:price, label: 'Price', type: :currency, filterable: true)
+ table.add(:created_at, label: 'Created', type: :datetime, filterable: true)
+ end
+
+ describe '#initialize' do
+ it 'sets the table' do
+ expect(query_builder.table).to eq(table)
+ end
+
+ it 'creates an empty root group' do
+ expect(query_builder.root_group).to be_a(Spree::Admin::Table::FilterGroup)
+ expect(query_builder.root_group.empty?).to be true
+ end
+ end
+
+ describe '#add_filter' do
+ it 'adds a filter to the root group' do
+ filter = query_builder.add_filter(field: 'name', operator: :cont, value: 'test')
+
+ expect(filter).to be_a(Spree::Admin::Table::Filter)
+ expect(query_builder.root_group.filters).to include(filter)
+ end
+ end
+
+ describe '#add_group' do
+ it 'adds a nested group' do
+ group = query_builder.add_group(combinator: :or)
+
+ expect(group).to be_a(Spree::Admin::Table::FilterGroup)
+ expect(query_builder.root_group.groups).to include(group)
+ end
+ end
+
+ describe '#clear' do
+ it 'resets the root group' do
+ query_builder.add_filter(field: 'name', operator: :cont, value: 'test')
+ query_builder.clear
+
+ expect(query_builder.empty?).to be true
+ end
+ end
+
+ describe '#empty?' do
+ it 'returns true when no filters' do
+ expect(query_builder.empty?).to be true
+ end
+
+ it 'returns false when has filters' do
+ query_builder.add_filter(field: 'name', operator: :cont, value: 'test')
+ expect(query_builder.empty?).to be false
+ end
+ end
+
+ describe '#to_ransack_params' do
+ it 'returns ransack params from filters' do
+ query_builder.add_filter(field: 'name', operator: :cont, value: 'test')
+ query_builder.add_filter(field: 'status', operator: :eq, value: 'active')
+
+ params = query_builder.to_ransack_params
+
+ expect(params['name_cont']).to eq('test')
+ expect(params['status_eq']).to eq('active')
+ end
+ end
+
+ describe '#to_json_state' do
+ it 'returns JSON string representation' do
+ query_builder.add_filter(field: 'name', operator: :cont, value: 'test')
+
+ json = query_builder.to_json_state
+ parsed = JSON.parse(json)
+
+ expect(parsed['combinator']).to eq('and')
+ expect(parsed['filters'].first['field']).to eq('name')
+ end
+ end
+
+ describe '#load_from_params' do
+ it 'loads state from params hash' do
+ params = {
+ combinator: 'and',
+ filters: [
+ { field: 'name', operator: 'cont', value: 'test' }
+ ],
+ groups: []
+ }
+
+ query_builder.load_from_params(params)
+
+ expect(query_builder.root_group.filters.size).to eq(1)
+ expect(query_builder.root_group.filters.first.field).to eq('name')
+ end
+ end
+
+ describe '#load_from_json' do
+ it 'loads state from JSON string' do
+ json = {
+ combinator: 'and',
+ filters: [
+ { field: 'name', operator: 'cont', value: 'test' }
+ ],
+ groups: []
+ }.to_json
+
+ query_builder.load_from_json(json)
+
+ expect(query_builder.root_group.filters.size).to eq(1)
+ end
+
+ it 'handles invalid JSON gracefully' do
+ query_builder.load_from_json('invalid json')
+ expect(query_builder.empty?).to be true
+ end
+
+ it 'handles blank string' do
+ query_builder.load_from_json('')
+ expect(query_builder.empty?).to be true
+ end
+ end
+
+ describe '#available_fields' do
+ it 'returns array of field configurations' do
+ fields = query_builder.available_fields
+
+ expect(fields).to be_an(Array)
+ expect(fields.size).to eq(4)
+ end
+
+ it 'includes field key' do
+ fields = query_builder.available_fields
+ name_field = fields.find { |f| f[:key] == 'name' }
+
+ expect(name_field[:key]).to eq('name')
+ end
+
+ it 'includes label' do
+ fields = query_builder.available_fields
+ name_field = fields.find { |f| f[:key] == 'name' }
+
+ expect(name_field[:label]).to eq('Name')
+ end
+
+ it 'includes filter type' do
+ fields = query_builder.available_fields
+ status_field = fields.find { |f| f[:key] == 'status' }
+
+ expect(status_field[:type]).to eq('status')
+ end
+
+ it 'includes operators' do
+ fields = query_builder.available_fields
+ name_field = fields.find { |f| f[:key] == 'name' }
+
+ expect(name_field[:operators]).to include('eq', 'cont')
+ end
+
+ it 'includes value_options for select fields' do
+ fields = query_builder.available_fields
+ status_field = fields.find { |f| f[:key] == 'status' }
+
+ expect(status_field[:value_options]).to be_present
+ expect(status_field[:value_options].map { |o| o[:value] }).to include('active', 'draft', 'archived')
+ end
+
+ it 'resolves lambda value_options' do
+ table.update(:status, value_options: -> { %w[one two three] })
+
+ fields = query_builder.available_fields
+ status_field = fields.find { |f| f[:key] == 'status' }
+
+ expect(status_field[:value_options].map { |o| o[:value] }).to include('one', 'two', 'three')
+ end
+ end
+
+ describe '#available_operators' do
+ it 'returns array of operator options' do
+ operators = query_builder.available_operators
+
+ expect(operators).to be_an(Array)
+ expect(operators.first).to include(:value, :label)
+ end
+ end
+end
diff --git a/admin/spec/models/spree/admin/table_spec.rb b/admin/spec/models/spree/admin/table_spec.rb
new file mode 100644
index 0000000000..807ff3ad6a
--- /dev/null
+++ b/admin/spec/models/spree/admin/table_spec.rb
@@ -0,0 +1,312 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::Table do
+ let(:table) { described_class.new(:products, model_class: Spree::Product) }
+
+ describe '#initialize' do
+ it 'sets the context' do
+ expect(table.context).to eq(:products)
+ end
+
+ it 'sets the model class' do
+ expect(table.model_class).to eq(Spree::Product)
+ end
+
+ it 'sets default values' do
+ expect(table.search_param).to eq(:name_cont)
+ expect(table.new_resource).to be true
+ expect(table.row_actions).to be false
+ expect(table.row_actions_edit).to be true
+ expect(table.row_actions_delete).to be false
+ end
+
+ it 'allows custom options' do
+ custom_table = described_class.new(:orders,
+ model_class: Spree::Order,
+ search_param: :number_cont,
+ row_actions: true,
+ new_resource: false,
+ date_range_param: :completed_at
+ )
+
+ expect(custom_table.search_param).to eq(:number_cont)
+ expect(custom_table.row_actions).to be true
+ expect(custom_table.new_resource).to be false
+ expect(custom_table.date_range_param).to eq(:completed_at)
+ end
+ end
+
+ describe '#add' do
+ it 'adds a column' do
+ column = table.add(:name, label: 'Name', type: :string)
+
+ expect(table.find(:name)).to eq(column)
+ end
+
+ it 'converts key to symbol' do
+ table.add('name', label: 'Name')
+
+ expect(table.find(:name)).to be_present
+ end
+
+ it 'sorts columns by position' do
+ table.add(:status, position: 20)
+ table.add(:name, position: 10)
+ table.add(:price, position: 30)
+
+ keys = table.available_columns.map(&:key)
+ expect(keys).to eq([:name, :status, :price])
+ end
+ end
+
+ describe '#remove' do
+ it 'removes a column' do
+ table.add(:name, label: 'Name')
+ table.remove(:name)
+
+ expect(table.find(:name)).to be_nil
+ end
+
+ it 'returns nil for non-existent column' do
+ expect(table.remove(:nonexistent)).to be_nil
+ end
+ end
+
+ describe '#update' do
+ it 'updates an existing column' do
+ table.add(:name, label: 'Name', sortable: false)
+ table.update(:name, label: 'Product Name', sortable: true)
+
+ column = table.find(:name)
+ expect(column.label).to eq('Product Name')
+ expect(column.sortable).to be true
+ end
+
+ it 'returns nil for non-existent column' do
+ result = table.update(:nonexistent, label: 'Test')
+
+ expect(result).to be_nil
+ end
+ end
+
+ describe '#find' do
+ it 'finds a column by key' do
+ column = table.add(:name, label: 'Name')
+
+ expect(table.find(:name)).to eq(column)
+ end
+
+ it 'returns nil for non-existent column' do
+ expect(table.find(:nonexistent)).to be_nil
+ end
+ end
+
+ describe '#exists?' do
+ it 'returns true when column exists' do
+ table.add(:name, label: 'Name')
+
+ expect(table.exists?(:name)).to be true
+ end
+
+ it 'returns false when column does not exist' do
+ expect(table.exists?(:nonexistent)).to be false
+ end
+ end
+
+ describe '#insert_before' do
+ it 'inserts column before target' do
+ table.add(:status, label: 'Status', position: 20)
+ table.insert_before(:status, :name, label: 'Name')
+
+ name = table.find(:name)
+ status = table.find(:status)
+
+ expect(name.position).to be < status.position
+ end
+
+ it 'returns nil if target does not exist' do
+ result = table.insert_before(:nonexistent, :name, label: 'Name')
+
+ expect(result).to be_nil
+ end
+ end
+
+ describe '#insert_after' do
+ it 'inserts column after target' do
+ table.add(:name, label: 'Name', position: 10)
+ table.insert_after(:name, :status, label: 'Status')
+
+ name = table.find(:name)
+ status = table.find(:status)
+
+ expect(status.position).to be > name.position
+ end
+ end
+
+ describe '#visible_columns' do
+ before do
+ table.add(:name, label: 'Name', default: true)
+ table.add(:status, label: 'Status', default: true)
+ table.add(:hidden, label: 'Hidden', default: false)
+ end
+
+ it 'returns default columns when no selection' do
+ columns = table.visible_columns
+ expect(columns.map(&:key)).to contain_exactly(:name, :status)
+ end
+
+ it 'returns selected columns when provided' do
+ columns = table.visible_columns([:name, :hidden])
+ expect(columns.map(&:key)).to contain_exactly(:name, :hidden)
+ end
+
+ it 'filters based on visibility condition' do
+ table.add(:conditional, label: 'Conditional', default: true, if: -> { false })
+
+ columns = table.visible_columns(nil, Object.new)
+ expect(columns.map(&:key)).to contain_exactly(:name, :status)
+ end
+ end
+
+ describe '#default_columns' do
+ it 'returns only default columns' do
+ table.add(:name, default: true)
+ table.add(:status, default: true)
+ table.add(:hidden, default: false)
+
+ columns = table.default_columns
+ expect(columns.map(&:key)).to contain_exactly(:name, :status)
+ end
+
+ it 'returns columns sorted by position' do
+ table.add(:status, default: true, position: 20)
+ table.add(:name, default: true, position: 10)
+
+ columns = table.default_columns
+ expect(columns.map(&:key)).to eq([:name, :status])
+ end
+ end
+
+ describe '#sortable_columns' do
+ it 'returns only sortable columns' do
+ table.add(:name, sortable: true)
+ table.add(:description, sortable: false)
+ table.add(:status, sortable: true)
+
+ columns = table.sortable_columns
+ expect(columns.map(&:key)).to contain_exactly(:name, :status)
+ end
+ end
+
+ describe '#filterable_columns' do
+ it 'returns only filterable columns' do
+ table.add(:name, filterable: true)
+ table.add(:image, filterable: false)
+ table.add(:status, filterable: true)
+
+ columns = table.filterable_columns
+ expect(columns.map(&:key)).to contain_exactly(:name, :status)
+ end
+ end
+
+ describe '#add_bulk_action' do
+ it 'adds a bulk action' do
+ action = table.add_bulk_action(:delete, label: 'Delete', icon: 'trash')
+
+ expect(table.find_bulk_action(:delete)).to eq(action)
+ end
+
+ it 'sorts bulk actions by position' do
+ table.add_bulk_action(:export, position: 20)
+ table.add_bulk_action(:delete, position: 10)
+
+ actions = table.visible_bulk_actions
+ expect(actions.map(&:key)).to eq([:delete, :export])
+ end
+ end
+
+ describe '#remove_bulk_action' do
+ it 'removes a bulk action' do
+ table.add_bulk_action(:delete)
+ table.remove_bulk_action(:delete)
+
+ expect(table.find_bulk_action(:delete)).to be_nil
+ end
+ end
+
+ describe '#bulk_operations_enabled?' do
+ it 'returns false when no bulk actions' do
+ expect(table.bulk_operations_enabled?).to be false
+ end
+
+ it 'returns true when bulk actions exist' do
+ table.add_bulk_action(:delete)
+ expect(table.bulk_operations_enabled?).to be true
+ end
+ end
+
+ describe '#date_range?' do
+ it 'returns false when date_range_param is not set' do
+ expect(table.date_range?).to be false
+ end
+
+ it 'returns true when date_range_param is set' do
+ table.date_range_param = :created_at
+ expect(table.date_range?).to be true
+ end
+ end
+
+ describe '#deep_clone' do
+ it 'creates a deep copy of the table' do
+ table.add(:name, label: 'Name', default: true)
+ table.add_bulk_action(:delete, label: 'Delete', position: 10)
+
+ cloned = table.deep_clone
+
+ expect(cloned.context).to eq(table.context)
+ expect(cloned.find(:name).label).to eq('Name')
+ expect(cloned.find_bulk_action(:delete).label).to eq('Delete')
+
+ # Verify it's a deep copy
+ cloned.update(:name, label: 'Changed')
+ expect(table.find(:name).label).to eq('Name')
+ end
+ end
+
+ describe '#clear' do
+ it 'removes all columns' do
+ table.add(:name)
+ table.add(:status)
+ table.clear
+
+ expect(table.available_columns).to be_empty
+ end
+ end
+
+ describe '#clear_bulk_actions' do
+ it 'removes all bulk actions' do
+ table.add_bulk_action(:delete)
+ table.add_bulk_action(:export)
+ table.clear_bulk_actions
+
+ expect(table.visible_bulk_actions).to be_empty
+ end
+ end
+
+ describe 'custom sort' do
+ it 'finds column with custom sort scope' do
+ table.add(:price, sortable: true, ransack_attribute: 'price', sort_scope_asc: :ascend_by_price)
+
+ column = table.find_custom_sort_column('price asc')
+ expect(column).to be_present
+ expect(column.key).to eq(:price)
+ end
+
+ it 'returns nil for standard sort' do
+ table.add(:name, sortable: true)
+
+ column = table.find_custom_sort_column('name asc')
+ expect(column).to be_nil
+ end
+ end
+end
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index 0159fd6a3d..83b8079410 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -1624,6 +1624,106 @@ describe Spree::Product, type: :model do
end
end
end
+
+ describe '.ascend_by_price' do
+ let!(:product_cheap) { create(:product, name: 'Cheap Product', stores: [store]) }
+ let!(:product_mid) { create(:product, name: 'Mid Product', stores: [store]) }
+ let!(:product_expensive) { create(:product, name: 'Expensive Product', stores: [store]) }
+ let!(:product_no_price) { create(:product, name: 'No Price Product', stores: [store]) }
+
+ before do
+ product_cheap.master.prices.delete_all
+ product_mid.master.prices.delete_all
+ product_expensive.master.prices.delete_all
+ product_no_price.master.prices.delete_all
+
+ create(:price, variant: product_cheap.master, amount: 10.00)
+ create(:price, variant: product_mid.master, amount: 50.00)
+ create(:price, variant: product_expensive.master, amount: 100.00)
+ end
+
+ it 'orders products by minimum price ascending' do
+ products = described_class.where(id: [product_cheap.id, product_mid.id, product_expensive.id]).ascend_by_price
+ expect(products.map(&:name)).to eq(['Cheap Product', 'Mid Product', 'Expensive Product'])
+ end
+
+ it 'places products without prices at the end' do
+ products = described_class.where(id: [product_cheap.id, product_no_price.id]).ascend_by_price
+ expect(products.map(&:name)).to eq(['Cheap Product', 'No Price Product'])
+ end
+
+ context 'with variant prices' do
+ let!(:product_with_variants) { create(:product, name: 'Variant Product', stores: [store]) }
+ let!(:variant1) { create(:variant, product: product_with_variants) }
+ let!(:variant2) { create(:variant, product: product_with_variants) }
+
+ before do
+ product_with_variants.master.prices.delete_all
+ variant1.prices.delete_all
+ variant2.prices.delete_all
+
+ # Variant prices only (no master price)
+ create(:price, variant: variant1, amount: 5.00)
+ create(:price, variant: variant2, amount: 15.00)
+ end
+
+ it 'uses minimum variant price for sorting' do
+ products = described_class.where(id: [product_cheap.id, product_with_variants.id]).ascend_by_price
+ # product_with_variants has min price 5.00, product_cheap has 10.00
+ expect(products.map(&:name)).to eq(['Variant Product', 'Cheap Product'])
+ end
+ end
+ end
+
+ describe '.descend_by_price' do
+ let!(:product_cheap) { create(:product, name: 'Cheap Product', stores: [store]) }
+ let!(:product_mid) { create(:product, name: 'Mid Product', stores: [store]) }
+ let!(:product_expensive) { create(:product, name: 'Expensive Product', stores: [store]) }
+ let!(:product_no_price) { create(:product, name: 'No Price Product', stores: [store]) }
+
+ before do
+ product_cheap.master.prices.delete_all
+ product_mid.master.prices.delete_all
+ product_expensive.master.prices.delete_all
+ product_no_price.master.prices.delete_all
+
+ create(:price, variant: product_cheap.master, amount: 10.00)
+ create(:price, variant: product_mid.master, amount: 50.00)
+ create(:price, variant: product_expensive.master, amount: 100.00)
+ end
+
+ it 'orders products by maximum price descending' do
+ products = described_class.where(id: [product_cheap.id, product_mid.id, product_expensive.id]).descend_by_price
+ expect(products.map(&:name)).to eq(['Expensive Product', 'Mid Product', 'Cheap Product'])
+ end
+
+ it 'places products without prices at the end' do
+ products = described_class.where(id: [product_expensive.id, product_no_price.id]).descend_by_price
+ expect(products.map(&:name)).to eq(['Expensive Product', 'No Price Product'])
+ end
+
+ context 'with variant prices' do
+ let!(:product_with_variants) { create(:product, name: 'Variant Product', stores: [store]) }
+ let!(:variant1) { create(:variant, product: product_with_variants) }
+ let!(:variant2) { create(:variant, product: product_with_variants) }
+
+ before do
+ product_with_variants.master.prices.delete_all
+ variant1.prices.delete_all
+ variant2.prices.delete_all
+
+ # Variant prices only (no master price)
+ create(:price, variant: variant1, amount: 200.00)
+ create(:price, variant: variant2, amount: 150.00)
+ end
+
+ it 'uses maximum variant price for sorting' do
+ products = described_class.where(id: [product_expensive.id, product_with_variants.id]).descend_by_price
+ # product_with_variants has max price 200.00, product_expensive has 100.00
+ expect(products.map(&:name)).to eq(['Variant Product', 'Expensive Product'])
+ end
+ end
+ end
end
describe 'after_touch :touch_taxons' do