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.

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.rb
index 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 invalid
def 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] = column
if 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 invalid
def 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] = action
sort_bulk_actions!
action
diff --git a/admin/app/models/spree/admin/table/bulk_action.rb b/admin/app/models/spree/admin/table/bulk_action.rb
index 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 Spree
module Admin
class Table
class 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 Spree
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
- options = to_h.except(:key).merge(condition: condition)
- self.class.new(key, **options)
+ self.class.new(**attributes.symbolize_keys.merge(condition: condition))
end
def inspect
diff --git a/admin/app/models/spree/admin/table/column.rb b/admin/app/models/spree/admin/table/column.rb
index 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 Spree
module Admin
class Table
class 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_defaults
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?
- @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 Spree
end
# Resolve label (handles i18n keys)
- # @return [String]
def resolve_label
if 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 Spree
end
# 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)
end
end
- # 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_type
case 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]
end
end
-
- # 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}>"
- end
end
end
end
diff --git a/admin/spec/models/spree/admin/table/bulk_action_spec.rb b/admin/spec/models/spree/admin/table/bulk_action_spec.rb
index 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 do
describe '#initialize' do
it '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({})
end
it '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 do
end
it '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
+ end
end
describe '#visible?' do
it 'returns true when no condition' do
- action = described_class.new(:delete)
+ action = described_class.new(key: :delete)
expect(action.visible?).to be true
end
it '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 false
end
it 'evaluates lambda condition with context' do
context = Object.new
- action = described_class.new(:delete, if: -> { true })
+ action = described_class.new(key: :delete, if: -> { true })
expect(action.visible?(context)).to be true
end
@@ -57,44 +96,45 @@ RSpec.describe Spree::Admin::Table::BulkAction do
true
end
- action = described_class.new(:delete, if: -> { can_delete? })
+ action = described_class.new(key: :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 })
+ action = described_class.new(key: :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')
+ action = described_class.new(key: :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')
+ action = described_class.new(key: :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)
+ action = described_class.new(key: :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 })
+ action = described_class.new(key: :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
+ describe '#attributes' do
it '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 do
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)
+ 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)
end
end
describe '#deep_clone' do
it '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_clone
cloned.label = 'Changed'
@@ -132,7 +171,7 @@ RSpec.describe Spree::Admin::Table::BulkAction do
it 'preserves condition' do
condition = -> { true }
- original = described_class.new(:delete, if: condition, label: 'Delete')
+ original = described_class.new(key: :delete, if: condition, label: 'Delete')
cloned = original.deep_clone
expect(cloned.condition).to eq(condition)
@@ -141,12 +180,12 @@ RSpec.describe Spree::Admin::Table::BulkAction do
describe '#confirm' do
it 'returns nil when not set' do
- action = described_class.new(:delete)
+ action = described_class.new(key: :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?')
+ 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?')
end
end
diff --git a/admin/spec/models/spree/admin/table/column_spec.rb b/admin/spec/models/spree/admin/table/column_spec.rb
index 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 do
describe '#initialize' do
it '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 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.align).to eq('left')
expect(column.ransack_attribute).to eq('name')
end
it '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 false
expect(column.default).to be true
expect(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')
end
it '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
+ end
end
end
describe '#sortable?' do
it '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 true
end
it '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 false
end
end
describe '#filterable?' do
it '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 true
end
it '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 false
end
end
describe '#default?' do
it '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 true
end
it '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 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)
+ column = described_class.new(key: 'price', label: :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)
+ column = described_class.new(key: 'price', label: :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)
+ column = described_class.new(key: 'name', label: :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)
+ column = described_class.new(key: 'name', label: :name)
expect(column.visible?).to be true
end
it '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 false
end
it 'evaluates lambda condition with context' do
context = 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 true
end
@@ -128,26 +157,30 @@ RSpec.describe Spree::Admin::Table::Column do
true
end
- 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 true
end
end
describe '#resolve_label' do
it '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))
end
end
@@ -155,17 +188,17 @@ RSpec.describe Spree::Admin::Table::Column do
let(: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')
end
it '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')
end
it '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 do
str[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')
end
end
- describe '#default_operators_for_type' do
+ describe 'default_operators_for_type' do
it '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)
end
it '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)
end
it '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)
end
it '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)
end
it '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])
end
it '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])
end
end
- describe '#to_h' do
+ describe '#attributes' do
it '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)
end
end
describe '#deep_clone' do
it '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_clone
cloned.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 true
end
end
diff --git a/admin/spec/models/spree/admin/table_spec.rb b/admin/spec/models/spree/admin/table_spec.rb
index 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 do
describe '#add' do
it '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)
end
it 'converts key to symbol' do
- table.add('name', label: 'Name')
+ 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)
+ 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/)
end
end
describe '#remove' do
it '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 do
describe '#update' do
it '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 do
describe '#find' do
it '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 do
describe '#exists?' do
it 'returns true when column exists' do
- table.add(:name, label: 'Name')
+ table.add(:name, label: :name)
expect(table.exists?(:name)).to be true
end
@@ -115,8 +127,8 @@ RSpec.describe Spree::Admin::Table do
describe '#insert_before' do
it '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 do
end
it '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_nil
end
@@ -133,8 +145,8 @@ RSpec.describe Spree::Admin::Table do
describe '#insert_after' do
it '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 do
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)
+ 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)
+ 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)
+ 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 })
+ 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')
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)
+ 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')
end
it '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])
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)
+ 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')
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)
+ 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')
end
end
@@ -223,6 +235,12 @@ RSpec.describe Spree::Admin::Table do
actions = table.visible_bulk_actions
expect(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/)
+ end
end
describe '#remove_bulk_action' do
@@ -258,25 +276,25 @@ RSpec.describe Spree::Admin::Table do
describe '#deep_clone' do
it '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_clone
expect(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 copy
cloned.update(:name, label: 'Changed')
- expect(table.find(:name).label).to eq('Name')
+ 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.add(:name, label: :name)
+ table.add(:status, label: :status)
table.clear
expect(table.available_columns).to be_empty
@@ -295,15 +313,15 @@ RSpec.describe Spree::Admin::Table do
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)
+ 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')
end
it '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