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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Move `Table` and `BulkAction` to use ActiveModel with validations for better DX
Damian Legawieccf44be163112
diff --git a/admin/app/models/spree/admin/table.rb b/admin/app/models/spree/admin/table.rbindex 1e7a9b0d45..6ac6ba54e3 100644--- a/admin/app/models/spree/admin/table.rb+++ b/admin/app/models/spree/admin/table.rb@@ -49,9 +49,18 @@ module Spree# @param key [Symbol] unique column identifier# @param options [Hash] column options (label, type, sortable, filterable, default, position, etc.)# @return [Column] the created column+ # @raise [ArgumentError] if column configuration is invaliddef add(key, **options, &block)key = key.to_sym- column = Column.new(key, **options)+ # Handle :if as alias for :condition+ options[:condition] = options.delete(:if) if options.key?(:if)+ column = Column.new(**options.merge(key: key.to_s))++ unless column.valid?+ errors = column.errors.full_messages.join(', ')+ raise ArgumentError, "Invalid column '#{key}' in table '#{@context}': #{errors}"+ end+@columns[key] = columnif block_given?@@ -192,9 +201,16 @@ module Spree# @param key [Symbol] unique action identifier# @param options [Hash] action options (label, icon, modal_path, action_path, position, etc.)# @return [BulkAction] the created action+ # @raise [ArgumentError] if action configuration is invaliddef add_bulk_action(key, **options)key = key.to_sym- action = BulkAction.new(key, **options)+ action = BulkAction.new(**options.merge(key: key))++ unless action.valid?+ errors = action.errors.full_messages.join(', ')+ raise ArgumentError, "Invalid bulk action '#{key}' in table '#{@context}': #{errors}"+ end+@bulk_actions[key] = actionsort_bulk_actions!actiondiff --git a/admin/app/models/spree/admin/table/bulk_action.rb b/admin/app/models/spree/admin/table/bulk_action.rbindex 98c477f725..9ded4092f8 100644--- a/admin/app/models/spree/admin/table/bulk_action.rb+++ b/admin/app/models/spree/admin/table/bulk_action.rb@@ -2,19 +2,31 @@ module Spreemodule Adminclass Tableclass BulkAction- attr_accessor :key, :label, :label_options, :icon, :modal_path, :action_path, :position, :condition, :confirm, :method+ include ActiveModel::Model+ include ActiveModel::Attributes- 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+ METHODS = %i[get post put patch delete].freeze++ attribute :key+ attribute :label+ attribute :label_options, default: -> { {} }+ attribute :icon, :string+ attribute :modal_path, :string+ attribute :action_path, :string+ attribute :position, :integer, default: 999+ attribute :condition+ attribute :confirm, :string+ attribute :method, default: :put++ validates :key, presence: true+ validates :method, presence: true, inclusion: { in: METHODS }++ def initialize(attributes = {})+ # Handle :if as alias for :condition+ attributes[:condition] = attributes.delete(:if) if attributes.key?(:if)+ super+ self.key = key.to_sym if key.is_a?(String)+ self.method = self.method.to_sym if self.method.is_a?(String)end# Check if action is visible for the given context@@ -48,27 +60,10 @@ module Spreeendend- # 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- options = to_h.except(:key).merge(condition: condition)- self.class.new(key, **options)+ self.class.new(**attributes.symbolize_keys.merge(condition: condition))enddef inspectdiff --git a/admin/app/models/spree/admin/table/column.rb b/admin/app/models/spree/admin/table/column.rbindex e74edebbe3..32e9db5c4c 100644--- a/admin/app/models/spree/admin/table/column.rb+++ b/admin/app/models/spree/admin/table/column.rb@@ -2,64 +2,56 @@ module Spreemodule Adminclass Tableclass Column- TYPES = %i[string number date datetime currency status link boolean image custom].freeze- FILTER_TYPES = %i[string number date datetime currency status boolean autocomplete].freeze-- attr_accessor :key, :label, :type, :filter_type, :sortable, :filterable, :displayable, :default, :position,- :partial, :partial_locals, :method, :width, :align, :format, :condition,- :ransack_attribute, :operators, :value_options, :search_url,- :sort_scope_asc, :sort_scope_desc-- def initialize(key, **options)- @key = key.to_sym- @label = options[:label]- @type = (options[:type] || :string).to_sym- @filter_type = (options[:filter_type] || @type).to_sym- @sortable = options.fetch(:sortable, true)- @filterable = options.fetch(:filterable, true)- @displayable = options.fetch(:displayable, true)- @default = options.fetch(:default, false)- @position = options[:position] || 999- @partial = options[:partial]- @partial_locals = options[:partial_locals] || {}- @method = options[:method] || key- @width = options[:width]- @align = options[:align] || :left- @format = options[:format]- @condition = options.key?(:if) ? options[:if] : options[:condition]- @ransack_attribute = options[:ransack_attribute] || key.to_s- @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]+ include ActiveModel::Model+ include ActiveModel::Attributes++ TYPES = %w[string number date datetime currency status link boolean image custom association].freeze+ FILTER_TYPES = %w[string number date datetime currency status boolean autocomplete select].freeze++ attribute :key, :string+ attribute :label+ attribute :type, :string, default: 'string'+ attribute :filter_type, :string+ attribute :sortable, :boolean, default: true+ attribute :filterable, :boolean, default: true+ attribute :displayable, :boolean, default: true+ attribute :default, :boolean, default: false+ attribute :position, :integer, default: 999+ attribute :partial, :string+ attribute :partial_locals, default: -> { {} }+ attribute :method+ attribute :width, :string+ attribute :align, :string, default: 'left'+ attribute :format, :string+ attribute :condition+ attribute :ransack_attribute, :string+ attribute :operators, default: -> { [] }+ attribute :value_options+ attribute :search_url, :string+ attribute :sort_scope_asc+ attribute :sort_scope_desc++ alias_method :sortable?, :sortable+ alias_method :filterable?, :filterable+ alias_method :displayable?, :displayable+ alias_method :default?, :default++ validates :key, presence: true+ validates :label, presence: true+ validates :type, presence: true, inclusion: { in: TYPES }+ validates :filter_type, inclusion: { in: FILTER_TYPES }, allow_blank: true++ def initialize(attributes = {})+ super+ set_defaultsend# 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?- @sortable- end-- def filterable?- @filterable- end-- def displayable?- @displayable- end-- def default?- @default- end-# Check if column 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?@@ -75,10 +67,8 @@ module Spreeend# Resolve label (handles i18n keys)- # @return [String]def resolve_labelif label.is_a?(String) && label.present?- # Check if it looks like a translation key (contains dots)if label.include?('.')return I18n.t(label, default: label.split('.').last.humanize)end@@ -90,79 +80,54 @@ module Spreeend# Resolve value from record- # @param record [Object] the record to extract value from- # @param context [Object, nil] view context- # @return [Object]def resolve_value(record, context = nil)- if method.respond_to?(:call)+ if self.method.respond_to?(:call)if context&.respond_to?(:instance_exec)- context.instance_exec(record, &method)+ context.instance_exec(record, &self.method)else- method.call(record)+ self.method.call(record)end- elsif record.respond_to?(method)- record.send(method)+ elsif record.respond_to?(self.method)+ record.send(self.method)endend- # Get default operators based on column filter_type- # @return [Array<Symbol>]+ # Deep clone the column+ def deep_clone+ self.class.new(**attributes.symbolize_keys)+ end++ def inspect+ "#<Spree::Admin::Table::Column key=#{key} type=#{type} default=#{self.default}>"+ end++ private++ def set_defaults+ self.method ||= key+ self.ransack_attribute ||= key.to_s+ self.filter_type ||= FILTER_TYPES.include?(type) ? type : nil+ self.operators = default_operators_for_type if operators.empty?+ end+def default_operators_for_typecase filter_type- when :string+ when 'string'%i[eq not_eq cont not_cont start end in not_in null not_null]- when :number, :currency+ when 'number', 'currency'%i[eq not_eq gt gteq lt lteq in not_in null not_null]- when :date, :datetime+ when 'date', 'datetime'%i[eq not_eq gt gteq lt lteq null not_null]- when :boolean+ when 'boolean'%i[eq]- when :status+ when 'status', 'select'%i[eq not_eq in not_in]- when :association, :tags, :autocomplete+ when 'association', 'tags', 'autocomplete'%i[in not_in]else%i[eq not_eq cont null not_null]endend-- # Convert to hash- # @return [Hash]- def to_h- {- key: key,- label: label,- type: type,- filter_type: filter_type,- sortable: sortable,- filterable: filterable,- displayable: displayable,- default: default,- position: position,- partial: partial,- method: method,- width: width,- align: align,- format: format,- ransack_attribute: ransack_attribute,- operators: operators,- value_options: value_options,- search_url: search_url,- sort_scope_asc: sort_scope_asc,- sort_scope_desc: sort_scope_desc- }- end-- # Deep clone the column- # @return [Column]- def deep_clone- options = to_h.except(:key).merge(condition: condition, method: method)- self.class.new(key, **options)- end-- def inspect- "#<Spree::Admin::Table::Column key=#{key} type=#{type} default=#{default}>"- endendendenddiff --git a/admin/spec/models/spree/admin/table/bulk_action_spec.rb b/admin/spec/models/spree/admin/table/bulk_action_spec.rbindex c7458976a2..915520d60c 100644--- a/admin/spec/models/spree/admin/table/bulk_action_spec.rb+++ b/admin/spec/models/spree/admin/table/bulk_action_spec.rb@@ -3,15 +3,17 @@ require 'spec_helper'RSpec.describe Spree::Admin::Table::BulkAction dodescribe '#initialize' doit 'sets default values' do- action = described_class.new(:delete)+ action = described_class.new(key: :delete)expect(action.key).to eq(:delete)expect(action.position).to eq(999)expect(action.method).to eq(:put)+ expect(action.label_options).to eq({})endit 'accepts custom options' do- action = described_class.new(:export,+ action = described_class.new(+ key: :export,label: 'Export Products',icon: 'download',modal_path: '/admin/exports/new',@@ -29,25 +31,62 @@ RSpec.describe Spree::Admin::Table::BulkAction doendit 'converts key to symbol' do- action = described_class.new('delete')+ action = described_class.new(key: 'delete')expect(action.key).to eq(:delete)end++ it 'converts method to symbol' do+ action = described_class.new(key: :delete, method: 'post')+ expect(action.method).to eq(:post)+ end++ it 'handles :if as alias for :condition' do+ condition = -> { true }+ action = described_class.new(key: :delete, if: condition)+ expect(action.condition).to eq(condition)+ end+ end++ describe 'validations' do+ it 'is invalid when key is missing' do+ action = described_class.new+ expect(action).not_to be_valid+ expect(action.errors[:key]).to include("can't be blank")+ end++ it 'is valid with just a key' do+ action = described_class.new(key: :delete)+ expect(action).to be_valid+ end++ it 'is invalid when method is not in allowed list' do+ action = described_class.new(key: :delete, method: :invalid)+ expect(action).not_to be_valid+ expect(action.errors[:method]).to include('is not included in the list')+ end++ it 'accepts all valid methods' do+ Spree::Admin::Table::BulkAction::METHODS.each do |method|+ action = described_class.new(key: :test, method: method)+ expect(action).to be_valid+ end+ endenddescribe '#visible?' doit 'returns true when no condition' do- action = described_class.new(:delete)+ action = described_class.new(key: :delete)expect(action.visible?).to be trueendit 'returns false when condition is false' do- action = described_class.new(:delete, if: false)+ action = described_class.new(key: :delete, if: false)expect(action.visible?).to be falseendit 'evaluates lambda condition with context' docontext = Object.new- action = described_class.new(:delete, if: -> { true })+ action = described_class.new(key: :delete, if: -> { true })expect(action.visible?(context)).to be trueend@@ -57,44 +96,45 @@ RSpec.describe Spree::Admin::Table::BulkAction dotrueend- action = described_class.new(:delete, if: -> { can_delete? })+ action = described_class.new(key: :delete, if: -> { can_delete? })expect(action.visible?(context)).to be trueendit 'returns false when condition lambda returns false' docontext = Object.new- action = described_class.new(:delete, if: -> { false })+ action = described_class.new(key: :delete, if: -> { false })expect(action.visible?(context)).to be falseendenddescribe '#resolve_label' doit 'returns string label directly when not a translation key' do- action = described_class.new(:delete, label: 'Delete Selected')+ action = described_class.new(key: :delete, label: 'Delete Selected')expect(action.resolve_label).to eq('Delete Selected')endit 'translates admin translation key' do- action = described_class.new(:delete, label: 'admin.bulk_ops.delete')+ action = described_class.new(key: :delete, label: 'admin.bulk_ops.delete')# Will use Spree.t which falls back to humanized key if translation not foundexpect(action.resolve_label).to be_a(String)endit 'uses key as fallback' do- action = described_class.new(:delete_selected)+ action = described_class.new(key: :delete_selected)expect(action.resolve_label).to include('Delete')endit 'supports label_options' do- action = described_class.new(:delete, label: :delete_count, label_options: { count: 5 })+ action = described_class.new(key: :delete, label: :delete_count, label_options: { count: 5 })# Translation with optionsexpect(action.resolve_label).to be_a(String)endend- describe '#to_h' do+ describe '#attributes' doit 'returns hash representation' do- action = described_class.new(:delete,+ action = described_class.new(+ key: :delete,label: 'Delete',icon: 'trash',modal_path: '/modal',@@ -104,22 +144,21 @@ RSpec.describe Spree::Admin::Table::BulkAction domethod: :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)+ attrs = action.attributes+ 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?')+ expect(attrs['method']).to eq(:delete)endenddescribe '#deep_clone' doit 'creates a deep copy' do- original = described_class.new(:delete, label: 'Original', position: 10)+ original = described_class.new(key: :delete, label: 'Original', position: 10)cloned = original.deep_clonecloned.label = 'Changed'@@ -132,7 +171,7 @@ RSpec.describe Spree::Admin::Table::BulkAction doit 'preserves condition' docondition = -> { true }- original = described_class.new(:delete, if: condition, label: 'Delete')+ original = described_class.new(key: :delete, if: condition, label: 'Delete')cloned = original.deep_cloneexpect(cloned.condition).to eq(condition)@@ -141,12 +180,12 @@ RSpec.describe Spree::Admin::Table::BulkAction dodescribe '#confirm' doit 'returns nil when not set' do- action = described_class.new(:delete)+ action = described_class.new(key: :delete)expect(action.confirm).to be_nilendit 'returns confirmation message when set' do- action = described_class.new(:delete, confirm: 'Are you sure you want to delete?')+ action = described_class.new(key: :delete, confirm: 'Are you sure you want to delete?')expect(action.confirm).to eq('Are you sure you want to delete?')endenddiff --git a/admin/spec/models/spree/admin/table/column_spec.rb b/admin/spec/models/spree/admin/table/column_spec.rbindex 11bf89c9ca..4907e4357b 100644--- a/admin/spec/models/spree/admin/table/column_spec.rb+++ b/admin/spec/models/spree/admin/table/column_spec.rb@@ -3,122 +3,151 @@ require 'spec_helper'RSpec.describe Spree::Admin::Table::Column dodescribe '#initialize' doit 'sets default values' do- column = described_class.new(:name)+ column = described_class.new(key: 'name', label: :name)- expect(column.key).to eq(:name)- expect(column.type).to eq(:string)- expect(column.filter_type).to eq(:string)+ expect(column.key).to eq('name')+ expect(column.type).to eq('string')+ expect(column.filter_type).to eq('string')expect(column.sortable).to be trueexpect(column.filterable).to be trueexpect(column.displayable).to be trueexpect(column.default).to be false- expect(column.align).to eq(:left)+ expect(column.align).to eq('left')expect(column.ransack_attribute).to eq('name')endit 'accepts custom options' do- column = described_class.new(:price,+ column = described_class.new(+ key: 'price',label: 'Product Price',- type: :currency,+ type: 'currency',sortable: false,filterable: true,default: true,position: 10,- align: :right,+ 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.type).to eq('currency')+ expect(column.filter_type).to eq('currency')expect(column.sortable).to be falseexpect(column.default).to be trueexpect(column.position).to eq(10)- expect(column.align).to eq(:right)+ expect(column.align).to eq('right')expect(column.ransack_attribute).to eq('master_price')endit 'uses type as filter_type by default' do- column = described_class.new(:status, type: :status)+ column = described_class.new(key: 'status', label: :status, type: 'status')- expect(column.filter_type).to eq(: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)+ it 'sets filter_type to nil when type has no corresponding filter_type' do+ column = described_class.new(key: 'status', label: :status, type: 'custom')- expect(column.type).to eq(:custom)- expect(column.filter_type).to eq(:select)+ expect(column.type).to eq('custom')+ expect(column.filter_type).to be_nil+ end++ it 'allows different filter_type than type when both are valid' do+ column = described_class.new(key: 'status', label: :status, type: 'string', filter_type: 'autocomplete')++ expect(column.type).to eq('string')+ expect(column.filter_type).to eq('autocomplete')+ end+ end++ describe 'validations' do+ it 'is invalid when label is missing' do+ column = described_class.new(key: 'name')+ expect(column).not_to be_valid+ expect(column.errors[:label]).to include("can't be blank")+ end++ it 'is invalid when type is invalid' do+ column = described_class.new(key: 'name', label: :name, type: 'invalid_type')+ expect(column).not_to be_valid+ expect(column.errors[:type]).to include("is not included in the list")+ end++ it 'accepts all valid types' do+ Spree::Admin::Table::Column::TYPES.each do |type|+ column = described_class.new(key: 'test', label: :test, type: type)+ expect(column).to be_valid+ endendenddescribe '#sortable?' doit 'returns true when sortable' do- column = described_class.new(:name, sortable: true)+ column = described_class.new(key: 'name', label: :name, sortable: true)expect(column.sortable?).to be trueendit 'returns false when not sortable' do- column = described_class.new(:name, sortable: false)+ column = described_class.new(key: 'name', label: :name, sortable: false)expect(column.sortable?).to be falseendenddescribe '#filterable?' doit 'returns true when filterable' do- column = described_class.new(:name, filterable: true)+ column = described_class.new(key: 'name', label: :name, filterable: true)expect(column.filterable?).to be trueendit 'returns false when not filterable' do- column = described_class.new(:name, filterable: false)+ column = described_class.new(key: 'name', label: :name, filterable: false)expect(column.filterable?).to be falseendenddescribe '#default?' doit 'returns true when default' do- column = described_class.new(:name, default: true)+ column = described_class.new(key: 'name', label: :name, default: true)expect(column.default?).to be trueendit 'returns false when not default' do- column = described_class.new(:name, default: false)+ column = described_class.new(key: 'name', label: :name, default: false)expect(column.default?).to be falseendenddescribe '#custom_sort?' doit 'returns true when sort_scope_asc is present' do- column = described_class.new(:price, sort_scope_asc: :ascend_by_price)+ column = described_class.new(key: 'price', label: :price, sort_scope_asc: :ascend_by_price)expect(column.custom_sort?).to be trueendit 'returns true when sort_scope_desc is present' do- column = described_class.new(:price, sort_scope_desc: :descend_by_price)+ column = described_class.new(key: 'price', label: :price, sort_scope_desc: :descend_by_price)expect(column.custom_sort?).to be trueendit 'returns false when no sort scopes' do- column = described_class.new(:name)+ column = described_class.new(key: 'name', label: :name)expect(column.custom_sort?).to be falseendenddescribe '#visible?' doit 'returns true when no condition' do- column = described_class.new(:name)+ column = described_class.new(key: 'name', label: :name)expect(column.visible?).to be trueendit 'returns false when condition is false' do- column = described_class.new(:name, if: false)+ column = described_class.new(key: 'name', label: :name, condition: false)expect(column.visible?).to be falseendit 'evaluates lambda condition with context' docontext = Object.new- column = described_class.new(:name, if: -> { true })+ column = described_class.new(key: 'name', label: :name, condition: -> { true })expect(column.visible?(context)).to be trueend@@ -128,26 +157,30 @@ RSpec.describe Spree::Admin::Table::Column dotrueend- column = described_class.new(:admin_only, if: -> { admin? })+ column = described_class.new(key: 'admin_only', label: :admin_only, condition: -> { admin? })expect(column.visible?(context)).to be trueendenddescribe '#resolve_label' doit 'returns string label directly' do- column = described_class.new(:name, label: 'Custom Label')+ column = described_class.new(key: 'name', label: 'Custom Label')expect(column.resolve_label).to eq('Custom Label')end+ it 'translates dot-separated label as i18n key' do+ column = described_class.new(key: 'name', label: 'admin.products.name')+ expect(column.resolve_label).to be_a(String)+ 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+ column = described_class.new(key: 'name', label: :product_name)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')+ it 'uses label for translation' do+ column = described_class.new(key: 'custom_field', label: :name)+ expect(column.resolve_label).to eq(Spree.t(:name))endend@@ -155,17 +188,17 @@ RSpec.describe Spree::Admin::Table::Column dolet(:product) { build(:product, name: 'Test Product') }it 'calls method on record' do- column = described_class.new(:name)+ column = described_class.new(key: 'name', label: :name)expect(column.resolve_value(product)).to eq('Test Product')endit 'uses custom method' do- column = described_class.new(:display_name, method: :name)+ column = described_class.new(key: 'display_name', label: :display_name, method: :name)expect(column.resolve_value(product)).to eq('Test Product')endit 'evaluates lambda method' do- column = described_class.new(:upper_name, method: ->(record) { record.name.upcase })+ column = described_class.new(key: 'upper_name', label: :upper_name, method: ->(record) { record.name.upcase })expect(column.resolve_value(product)).to eq('TEST PRODUCT')end@@ -175,74 +208,75 @@ RSpec.describe Spree::Admin::Table::Column dostr[0...length]end- column = described_class.new(:truncated, method: ->(record) { truncate(record.name, length: 4) })+ column = described_class.new(key: 'truncated', label: :truncated, method: ->(record) { truncate(record.name, length: 4) })expect(column.resolve_value(product, context)).to eq('Test')endend- describe '#default_operators_for_type' do+ describe 'default_operators_for_type' doit 'returns string operators for string type' do- column = described_class.new(:name, type: :string)+ column = described_class.new(key: 'name', label: :name, type: 'string')expect(column.operators).to include(:eq, :cont, :start, :end)endit 'returns number operators for number type' do- column = described_class.new(:quantity, type: :number)+ column = described_class.new(key: 'quantity', label: :quantity, type: 'number')expect(column.operators).to include(:eq, :gt, :lt, :gteq, :lteq)endit 'returns date operators for date type' do- column = described_class.new(:created_at, type: :date)+ column = described_class.new(key: 'created_at', label: :created_at, type: 'date')expect(column.operators).to include(:eq, :gt, :lt)expect(column.operators).not_to include(:cont)endit 'returns status operators for status type' do- column = described_class.new(:status, type: :status)+ column = described_class.new(key: 'status', label: :status, type: 'status')expect(column.operators).to include(:eq, :not_eq, :in, :not_in)endit 'returns boolean operators for boolean type' do- column = described_class.new(:active, type: :boolean)+ column = described_class.new(key: 'active', label: :active, type: 'boolean')expect(column.operators).to eq([:eq])endit 'returns autocomplete operators for autocomplete type' do- column = described_class.new(:taxon, filter_type: :autocomplete)+ column = described_class.new(key: 'taxon', label: :taxon, filter_type: 'autocomplete')expect(column.operators).to eq([:in, :not_in])endend- describe '#to_h' do+ describe '#attributes' doit 'returns hash representation' do- column = described_class.new(:name,+ column = described_class.new(+ key: 'name',label: 'Name',- type: :string,+ 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)+ attrs = column.attributes+ expect(attrs['key']).to eq('name')+ expect(attrs['label']).to eq('Name')+ expect(attrs['type']).to eq('string')+ expect(attrs['sortable']).to be true+ expect(attrs['default']).to be true+ expect(attrs['position']).to eq(10)endenddescribe '#deep_clone' doit 'creates a deep copy' do- original = described_class.new(:name, label: 'Original', default: true, position: 10)+ original = described_class.new(key: 'name', label: 'Original', default: true, position: 10)cloned = original.deep_clonecloned.label = 'Changed'expect(original.label).to eq('Original')expect(cloned.label).to eq('Changed')- expect(cloned.key).to eq(:name)+ expect(cloned.key).to eq('name')expect(cloned.default).to be trueendenddiff --git a/admin/spec/models/spree/admin/table_spec.rb b/admin/spec/models/spree/admin/table_spec.rbindex 807ff3ad6a..aa5d811496 100644--- a/admin/spec/models/spree/admin/table_spec.rb+++ b/admin/spec/models/spree/admin/table_spec.rb@@ -38,30 +38,42 @@ RSpec.describe Spree::Admin::Table dodescribe '#add' doit 'adds a column' do- column = table.add(:name, label: 'Name', type: :string)+ column = table.add(:name, label: :name, type: :string)expect(table.find(:name)).to eq(column)endit 'converts key to symbol' do- table.add('name', label: 'Name')+ table.add('name', label: :name)expect(table.find(:name)).to be_presentendit 'sorts columns by position' do- table.add(:status, position: 20)- table.add(:name, position: 10)- table.add(:price, position: 30)+ table.add(:status, label: :status, position: 20)+ table.add(:name, label: :name, position: 10)+ table.add(:price, label: :price, position: 30)keys = table.available_columns.map(&:key)- expect(keys).to eq([:name, :status, :price])+ expect(keys).to eq(%w[name status price])+ end++ it 'raises ArgumentError with helpful message when column is invalid' do+ expect {+ table.add(:bad_column)+ }.to raise_error(ArgumentError, "Invalid column 'bad_column' in table 'products': Label can't be blank")+ end++ it 'raises ArgumentError when type is invalid' do+ expect {+ table.add(:bad_column, label: :test, type: :invalid_type)+ }.to raise_error(ArgumentError, /Invalid column 'bad_column' in table 'products':.*Type is not included/)endenddescribe '#remove' doit 'removes a column' do- table.add(:name, label: 'Name')+ table.add(:name, label: :name)table.remove(:name)expect(table.find(:name)).to be_nil@@ -74,7 +86,7 @@ RSpec.describe Spree::Admin::Table dodescribe '#update' doit 'updates an existing column' do- table.add(:name, label: 'Name', sortable: false)+ table.add(:name, label: :name, sortable: false)table.update(:name, label: 'Product Name', sortable: true)column = table.find(:name)@@ -91,7 +103,7 @@ RSpec.describe Spree::Admin::Table dodescribe '#find' doit 'finds a column by key' do- column = table.add(:name, label: 'Name')+ column = table.add(:name, label: :name)expect(table.find(:name)).to eq(column)end@@ -103,7 +115,7 @@ RSpec.describe Spree::Admin::Table dodescribe '#exists?' doit 'returns true when column exists' do- table.add(:name, label: 'Name')+ table.add(:name, label: :name)expect(table.exists?(:name)).to be trueend@@ -115,8 +127,8 @@ RSpec.describe Spree::Admin::Table dodescribe '#insert_before' doit 'inserts column before target' do- table.add(:status, label: 'Status', position: 20)- table.insert_before(:status, :name, label: 'Name')+ table.add(:status, label: :status, position: 20)+ table.insert_before(:status, :name, label: :name)name = table.find(:name)status = table.find(:status)@@ -125,7 +137,7 @@ RSpec.describe Spree::Admin::Table doendit 'returns nil if target does not exist' do- result = table.insert_before(:nonexistent, :name, label: 'Name')+ result = table.insert_before(:nonexistent, :name, label: :name)expect(result).to be_nilend@@ -133,8 +145,8 @@ RSpec.describe Spree::Admin::Table dodescribe '#insert_after' doit 'inserts column after target' do- table.add(:name, label: 'Name', position: 10)- table.insert_after(:name, :status, label: 'Status')+ table.add(:name, label: :name, position: 10)+ table.insert_after(:name, :status, label: :status)name = table.find(:name)status = table.find(:status)@@ -145,67 +157,67 @@ RSpec.describe Spree::Admin::Table dodescribe '#visible_columns' dobefore do- table.add(:name, label: 'Name', default: true)- table.add(:status, label: 'Status', default: true)- table.add(:hidden, label: 'Hidden', default: false)+ table.add(:name, label: :name, default: true)+ table.add(:status, label: :status, default: true)+ table.add(:hidden, label: :hidden, default: false)endit 'returns default columns when no selection' docolumns = table.visible_columns- expect(columns.map(&:key)).to contain_exactly(:name, :status)+ expect(columns.map(&:key)).to contain_exactly('name', 'status')endit 'returns selected columns when provided' docolumns = table.visible_columns([:name, :hidden])- expect(columns.map(&:key)).to contain_exactly(:name, :hidden)+ expect(columns.map(&:key)).to contain_exactly('name', 'hidden')endit 'filters based on visibility condition' do- table.add(:conditional, label: 'Conditional', default: true, if: -> { false })+ 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)+ expect(columns.map(&:key)).to contain_exactly('name', 'status')endenddescribe '#default_columns' doit 'returns only default columns' do- table.add(:name, default: true)- table.add(:status, default: true)- table.add(:hidden, default: false)+ table.add(:name, label: :name, default: true)+ table.add(:status, label: :status, default: true)+ table.add(:hidden, label: :hidden, default: false)columns = table.default_columns- expect(columns.map(&:key)).to contain_exactly(:name, :status)+ expect(columns.map(&:key)).to contain_exactly('name', 'status')endit 'returns columns sorted by position' do- table.add(:status, default: true, position: 20)- table.add(:name, default: true, position: 10)+ table.add(:status, label: :status, default: true, position: 20)+ table.add(:name, label: :name, default: true, position: 10)columns = table.default_columns- expect(columns.map(&:key)).to eq([:name, :status])+ expect(columns.map(&:key)).to eq(%w[name status])endenddescribe '#sortable_columns' doit 'returns only sortable columns' do- table.add(:name, sortable: true)- table.add(:description, sortable: false)- table.add(:status, sortable: true)+ table.add(:name, label: :name, sortable: true)+ table.add(:description, label: :description, sortable: false)+ table.add(:status, label: :status, sortable: true)columns = table.sortable_columns- expect(columns.map(&:key)).to contain_exactly(:name, :status)+ expect(columns.map(&:key)).to contain_exactly('name', 'status')endenddescribe '#filterable_columns' doit 'returns only filterable columns' do- table.add(:name, filterable: true)- table.add(:image, filterable: false)- table.add(:status, filterable: true)+ table.add(:name, label: :name, filterable: true)+ table.add(:image, label: :image, filterable: false)+ table.add(:status, label: :status, filterable: true)columns = table.filterable_columns- expect(columns.map(&:key)).to contain_exactly(:name, :status)+ expect(columns.map(&:key)).to contain_exactly('name', 'status')endend@@ -223,6 +235,12 @@ RSpec.describe Spree::Admin::Table doactions = table.visible_bulk_actionsexpect(actions.map(&:key)).to eq([:delete, :export])end++ it 'raises ArgumentError when method is invalid' do+ expect {+ table.add_bulk_action(:bad_action, method: :invalid)+ }.to raise_error(ArgumentError, /Invalid bulk action 'bad_action' in table 'products':.*Method is not included/)+ endenddescribe '#remove_bulk_action' do@@ -258,25 +276,25 @@ RSpec.describe Spree::Admin::Table dodescribe '#deep_clone' doit 'creates a deep copy of the table' do- table.add(:name, label: 'Name', default: true)+ table.add(:name, label: :name, default: true)table.add_bulk_action(:delete, label: 'Delete', position: 10)cloned = table.deep_cloneexpect(cloned.context).to eq(table.context)- expect(cloned.find(:name).label).to eq('Name')+ expect(cloned.find(:name).label).to eq(:name)expect(cloned.find_bulk_action(:delete).label).to eq('Delete')# Verify it's a deep copycloned.update(:name, label: 'Changed')- expect(table.find(:name).label).to eq('Name')+ expect(table.find(:name).label).to eq(:name)endenddescribe '#clear' doit 'removes all columns' do- table.add(:name)- table.add(:status)+ table.add(:name, label: :name)+ table.add(:status, label: :status)table.clearexpect(table.available_columns).to be_empty@@ -295,15 +313,15 @@ RSpec.describe Spree::Admin::Table dodescribe 'custom sort' doit 'finds column with custom sort scope' do- table.add(:price, sortable: true, ransack_attribute: 'price', sort_scope_asc: :ascend_by_price)+ table.add(:price, label: :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)+ expect(column.key).to eq('price')endit 'returns nil for standard sort' do- table.add(:name, sortable: true)+ table.add(:name, label: :name, sortable: true)column = table.find_custom_sort_column('name asc')expect(column).to be_nil