Class: Spree::Admin::Table

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/admin/table.rb,
app/models/spree/admin/table/column.rb,
app/models/spree/admin/table/filter.rb,
app/models/spree/admin/table/builder.rb,
app/models/spree/admin/table/visibility.rb,
app/models/spree/admin/table/bulk_action.rb,
app/models/spree/admin/table/filter_group.rb,
app/models/spree/admin/table/query_builder.rb

Defined Under Namespace

Modules: Visibility Classes: Builder, BulkAction, Column, Filter, FilterGroup, QueryBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, key: nil, model_class: nil, search_param: :name_cont, search_placeholder: nil, row_actions: false, row_actions_edit: true, row_actions_delete: false, new_resource: true, date_range_param: nil, date_range_label: nil, link_to_action: :edit) ⇒ Table

Returns a new instance of Table.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/spree/admin/table.rb', line 8

def initialize(context, key: nil, model_class: nil, search_param: :name_cont, search_placeholder: nil, row_actions: false, row_actions_edit: true, row_actions_delete: false, new_resource: true, date_range_param: nil, date_range_label: nil, link_to_action: :edit)
  @context = context
  @key = key
  @model_class = model_class
  @columns = {}
  @bulk_actions = {}
  @search_param = search_param
  @search_placeholder = search_placeholder
  @row_actions = row_actions
  @row_actions_edit = row_actions_edit
  @row_actions_delete = row_actions_delete
  @new_resource = new_resource
  @date_range_param = date_range_param
  @date_range_label = date_range_label
  @link_to_action = link_to_action
end

Instance Attribute Details

#bulk_actionsObject (readonly)

Returns the value of attribute bulk_actions.



4
5
6
# File 'app/models/spree/admin/table.rb', line 4

def bulk_actions
  @bulk_actions
end

#columnsObject (readonly)

Returns the value of attribute columns.



4
5
6
# File 'app/models/spree/admin/table.rb', line 4

def columns
  @columns
end

#contextObject (readonly)

Returns the value of attribute context.



4
5
6
# File 'app/models/spree/admin/table.rb', line 4

def context
  @context
end

#date_range_labelObject

Returns the value of attribute date_range_label.



5
6
7
# File 'app/models/spree/admin/table.rb', line 5

def date_range_label
  @date_range_label
end

#date_range_paramObject

Returns the value of attribute date_range_param.



5
6
7
# File 'app/models/spree/admin/table.rb', line 5

def date_range_param
  @date_range_param
end

#keyObject (readonly)

Returns the value of attribute key.



4
5
6
# File 'app/models/spree/admin/table.rb', line 4

def key
  @key
end

Returns the value of attribute link_to_action.



5
6
7
# File 'app/models/spree/admin/table.rb', line 5

def link_to_action
  @link_to_action
end

#model_classObject (readonly)

Returns the value of attribute model_class.



4
5
6
# File 'app/models/spree/admin/table.rb', line 4

def model_class
  @model_class
end

#new_resourceObject

Returns the value of attribute new_resource.



5
6
7
# File 'app/models/spree/admin/table.rb', line 5

def new_resource
  @new_resource
end

#row_actionsObject

Returns the value of attribute row_actions.



5
6
7
# File 'app/models/spree/admin/table.rb', line 5

def row_actions
  @row_actions
end

#row_actions_deleteObject

Returns the value of attribute row_actions_delete.



5
6
7
# File 'app/models/spree/admin/table.rb', line 5

def row_actions_delete
  @row_actions_delete
end

#row_actions_editObject

Returns the value of attribute row_actions_edit.



5
6
7
# File 'app/models/spree/admin/table.rb', line 5

def row_actions_edit
  @row_actions_edit
end

#search_paramObject

Returns the value of attribute search_param.



5
6
7
# File 'app/models/spree/admin/table.rb', line 5

def search_param
  @search_param
end

#search_placeholderObject

Returns the value of attribute search_placeholder.



5
6
7
# File 'app/models/spree/admin/table.rb', line 5

def search_placeholder
  @search_placeholder
end

Instance Method Details

#add(key, **options, &block) ⇒ Column

Add a column definition

Parameters:

  • key (Symbol)

    unique column identifier

  • options (Hash)

    column options (label, type, sortable, filterable, default, position, etc.)

Returns:

  • (Column)

    the created column

Raises:

  • (ArgumentError)

    if column configuration is invalid



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/spree/admin/table.rb', line 54

def add(key, **options, &block)
  key = key.to_sym
  # 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?
    builder = Builder.new(self, column)
    builder.instance_eval(&block)
  end

  sort_columns!
  column
end

#add_bulk_action(key, **options) ⇒ BulkAction

Add a bulk action definition

Parameters:

  • key (Symbol)

    unique action identifier

  • options (Hash)

    action options (label, icon, action_path, position, etc.)

Returns:

Raises:

  • (ArgumentError)

    if action configuration is invalid



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/models/spree/admin/table.rb', line 203

def add_bulk_action(key, **options)
  key = key.to_sym
  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
end

#apply_custom_sort(collection, sort_param) ⇒ ActiveRecord::Relation

Apply custom sort scope to collection

Parameters:

  • collection (ActiveRecord::Relation)

    the collection to sort

  • sort_param (String)

    the sort param (e.g., “master_price desc”)

Returns:

  • (ActiveRecord::Relation)

    sorted collection



180
181
182
183
184
185
186
187
188
189
190
# File 'app/models/spree/admin/table.rb', line 180

def apply_custom_sort(collection, sort_param)
  column = find_custom_sort_column(sort_param)
  return collection unless column

  direction = sort_param.to_s.include?('desc') ? :desc : :asc
  scope_name = direction == :desc ? column.sort_scope_desc : column.sort_scope_asc

  return collection unless scope_name.present?

  collection.send(scope_name)
end

#available_columnsArray<Column>

Get all available columns that can be displayed

Returns:



156
157
158
# File 'app/models/spree/admin/table.rb', line 156

def available_columns
  @columns.values.select(&:displayable?).sort_by(&:position)
end

#bulk_operations_enabled?Boolean

Check if bulk operations are enabled (has any bulk actions)

Returns:

  • (Boolean)


253
254
255
# File 'app/models/spree/admin/table.rb', line 253

def bulk_operations_enabled?
  @bulk_actions.any?
end

#clearObject

Clear all columns



283
284
285
# File 'app/models/spree/admin/table.rb', line 283

def clear
  @columns.clear
end

#clear_bulk_actionsObject

Clear all bulk actions



288
289
290
# File 'app/models/spree/admin/table.rb', line 288

def clear_bulk_actions
  @bulk_actions.clear
end

#date_range?Boolean

Check if date range filter is enabled

Returns:

  • (Boolean)


27
28
29
# File 'app/models/spree/admin/table.rb', line 27

def date_range?
  @date_range_param.present?
end

#deep_cloneTable

Deep clone the registry

Returns:



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'app/models/spree/admin/table.rb', line 259

def deep_clone
  cloned = self.class.new(
    context,
    model_class: model_class,
    search_param: search_param,
    search_placeholder: search_placeholder,
    row_actions: row_actions,
    row_actions_edit: row_actions_edit,
    row_actions_delete: row_actions_delete,
    new_resource: new_resource,
    date_range_param: date_range_param,
    date_range_label: date_range_label,
    link_to_action: link_to_action
  )
  @columns.each do |key, column|
    cloned.columns[key] = column.deep_clone
  end
  @bulk_actions.each do |key, action|
    cloned.bulk_actions[key] = action.deep_clone
  end
  cloned
end

#default_columnsArray<Column>

Get default columns (marked as default: true and displayable)

Returns:



150
151
152
# File 'app/models/spree/admin/table.rb', line 150

def default_columns
  @columns.values.select { |c| c.default? && c.displayable? }.sort_by(&:position)
end

#exists?(key) ⇒ Boolean

Check if column exists

Parameters:

  • key (Symbol)

    column key

Returns:

  • (Boolean)


106
107
108
# File 'app/models/spree/admin/table.rb', line 106

def exists?(key)
  @columns.key?(key.to_sym)
end

#filterable_columnsArray<Column>

Get filterable columns (columns that can be used in query builder)

Returns:



194
195
196
# File 'app/models/spree/admin/table.rb', line 194

def filterable_columns
  @columns.values.select(&:filterable?).sort_by(&:position)
end

#find(key) ⇒ Column?

Find a column by key

Parameters:

  • key (Symbol)

    column key

Returns:



99
100
101
# File 'app/models/spree/admin/table.rb', line 99

def find(key)
  @columns[key.to_sym]
end

#find_bulk_action(key) ⇒ BulkAction?

Find a bulk action by key

Parameters:

  • key (Symbol)

    action key

Returns:



240
241
242
# File 'app/models/spree/admin/table.rb', line 240

def find_bulk_action(key)
  @bulk_actions[key.to_sym]
end

#find_custom_sort_column(sort_param) ⇒ Column?

Find column with custom sort scope for the given sort param

Parameters:

  • sort_param (String)

    the sort param (e.g., “master_price desc”)

Returns:

  • (Column, nil)

    column with custom sort scope or nil



169
170
171
172
173
174
# File 'app/models/spree/admin/table.rb', line 169

def find_custom_sort_column(sort_param)
  return nil if sort_param.blank?

  attribute = sort_param.to_s.split.first
  @columns.values.find { |c| c.ransack_attribute == attribute && c.custom_sort? }
end

#insert_after(target_key, new_key, **options) ⇒ Column?

Insert column after another column

Parameters:

  • target_key (Symbol)

    existing column key

  • new_key (Symbol)

    new column key

  • options (Hash)

    column options

Returns:



127
128
129
130
131
132
# File 'app/models/spree/admin/table.rb', line 127

def insert_after(target_key, new_key, **options)
  target = find(target_key)
  return nil unless target

  add(new_key, **options.merge(position: target.position + 1))
end

#insert_before(target_key, new_key, **options) ⇒ Column?

Insert column before another column

Parameters:

  • target_key (Symbol)

    existing column key

  • new_key (Symbol)

    new column key

  • options (Hash)

    column options

Returns:



115
116
117
118
119
120
# File 'app/models/spree/admin/table.rb', line 115

def insert_before(target_key, new_key, **options)
  target = find(target_key)
  return nil unless target

  add(new_key, **options.merge(position: target.position - 1))
end

#inspectObject



292
293
294
# File 'app/models/spree/admin/table.rb', line 292

def inspect
  "#<Spree::Admin::Table context=#{context} columns=#{@columns.size} bulk_actions=#{@bulk_actions.size}>"
end

#remove(key) ⇒ Column?

Remove a column

Parameters:

  • key (Symbol)

    column key to remove

Returns:

  • (Column, nil)

    the removed column or nil



79
80
81
# File 'app/models/spree/admin/table.rb', line 79

def remove(key)
  @columns.delete(key.to_sym)
end

#remove_bulk_action(key) ⇒ BulkAction?

Remove a bulk action

Parameters:

  • key (Symbol)

    action key to remove

Returns:



220
221
222
# File 'app/models/spree/admin/table.rb', line 220

def remove_bulk_action(key)
  @bulk_actions.delete(key.to_sym)
end

#row_actions?Boolean

Check if row actions are enabled

Returns:

  • (Boolean)


33
34
35
# File 'app/models/spree/admin/table.rb', line 33

def row_actions?
  @row_actions
end

#row_actions_delete?Boolean

Check if delete row action is enabled

Returns:

  • (Boolean)


45
46
47
# File 'app/models/spree/admin/table.rb', line 45

def row_actions_delete?
  @row_actions_delete
end

#row_actions_edit?Boolean

Check if edit row action is enabled

Returns:

  • (Boolean)


39
40
41
# File 'app/models/spree/admin/table.rb', line 39

def row_actions_edit?
  @row_actions_edit
end

#sortable_columnsArray<Column>

Get sortable columns

Returns:



162
163
164
# File 'app/models/spree/admin/table.rb', line 162

def sortable_columns
  @columns.values.select(&:sortable?).sort_by(&:position)
end

#update(key, **options) ⇒ Column?

Update an existing column

Parameters:

  • key (Symbol)

    column key to update

  • options (Hash)

    attributes to update

Returns:

  • (Column, nil)

    the updated column or nil



87
88
89
90
91
92
93
94
# File 'app/models/spree/admin/table.rb', line 87

def update(key, **options)
  column = @columns[key.to_sym]
  return nil unless column

  apply_attributes(column, options)
  sort_columns!
  column
end

#update_bulk_action(key, **options) ⇒ BulkAction?

Update an existing bulk action

Parameters:

  • key (Symbol)

    action key to update

  • options (Hash)

    attributes to update

Returns:



228
229
230
231
232
233
234
235
# File 'app/models/spree/admin/table.rb', line 228

def update_bulk_action(key, **options)
  action = @bulk_actions[key.to_sym]
  return nil unless action

  apply_attributes(action, options)
  sort_bulk_actions!
  action
end

#visible_bulk_actions(view_context = nil) ⇒ Array<BulkAction>

Get visible bulk actions for the given context

Parameters:

  • view_context (Object, nil) (defaults to: nil)

    view context for visibility checks

Returns:



247
248
249
# File 'app/models/spree/admin/table.rb', line 247

def visible_bulk_actions(view_context = nil)
  @bulk_actions.values.select { |a| a.visible?(view_context) }.sort_by(&:position)
end

#visible_columns(selected_keys = nil, view_context = nil) ⇒ Array<Column>

Get visible columns for user (respecting selection)

Parameters:

  • selected_keys (Array<Symbol>, nil) (defaults to: nil)

    user-selected column keys

  • view_context (Object, nil) (defaults to: nil)

    view context for visibility checks

Returns:



138
139
140
141
142
143
144
145
146
# File 'app/models/spree/admin/table.rb', line 138

def visible_columns(selected_keys = nil, view_context = nil)
  cols = if selected_keys.present?
           selected_keys.map { |k| find(k) }.compact
         else
           default_columns
         end

  cols.select { |c| c.visible?(view_context) }
end