Class: Spree::Admin::Table
- Inherits:
-
Object
- Object
- Spree::Admin::Table
- 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
-
#bulk_actions ⇒ Object
readonly
Returns the value of attribute bulk_actions.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#date_range_label ⇒ Object
Returns the value of attribute date_range_label.
-
#date_range_param ⇒ Object
Returns the value of attribute date_range_param.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#link_to_action ⇒ Object
Returns the value of attribute link_to_action.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#new_resource ⇒ Object
Returns the value of attribute new_resource.
-
#row_actions ⇒ Object
Returns the value of attribute row_actions.
-
#row_actions_delete ⇒ Object
Returns the value of attribute row_actions_delete.
-
#row_actions_edit ⇒ Object
Returns the value of attribute row_actions_edit.
-
#search_param ⇒ Object
Returns the value of attribute search_param.
-
#search_placeholder ⇒ Object
Returns the value of attribute search_placeholder.
Instance Method Summary collapse
-
#add(key, **options, &block) ⇒ Column
Add a column definition.
-
#add_bulk_action(key, **options) ⇒ BulkAction
Add a bulk action definition.
-
#apply_custom_sort(collection, sort_param) ⇒ ActiveRecord::Relation
Apply custom sort scope to collection.
-
#available_columns ⇒ Array<Column>
Get all available columns that can be displayed.
-
#bulk_operations_enabled? ⇒ Boolean
Check if bulk operations are enabled (has any bulk actions).
-
#clear ⇒ Object
Clear all columns.
-
#clear_bulk_actions ⇒ Object
Clear all bulk actions.
-
#date_range? ⇒ Boolean
Check if date range filter is enabled.
-
#deep_clone ⇒ Table
Deep clone the registry.
-
#default_columns ⇒ Array<Column>
Get default columns (marked as default: true and displayable).
-
#exists?(key) ⇒ Boolean
Check if column exists.
-
#filterable_columns ⇒ Array<Column>
Get filterable columns (columns that can be used in query builder).
-
#find(key) ⇒ Column?
Find a column by key.
-
#find_bulk_action(key) ⇒ BulkAction?
Find a bulk action by key.
-
#find_custom_sort_column(sort_param) ⇒ Column?
Find column with custom sort scope for the given sort param.
-
#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
constructor
A new instance of Table.
-
#insert_after(target_key, new_key, **options) ⇒ Column?
Insert column after another column.
-
#insert_before(target_key, new_key, **options) ⇒ Column?
Insert column before another column.
- #inspect ⇒ Object
-
#remove(key) ⇒ Column?
Remove a column.
-
#remove_bulk_action(key) ⇒ BulkAction?
Remove a bulk action.
-
#row_actions? ⇒ Boolean
Check if row actions are enabled.
-
#row_actions_delete? ⇒ Boolean
Check if delete row action is enabled.
-
#row_actions_edit? ⇒ Boolean
Check if edit row action is enabled.
-
#sortable_columns ⇒ Array<Column>
Get sortable columns.
-
#update(key, **options) ⇒ Column?
Update an existing column.
-
#update_bulk_action(key, **options) ⇒ BulkAction?
Update an existing bulk action.
-
#visible_bulk_actions(view_context = nil) ⇒ Array<BulkAction>
Get visible bulk actions for the given context.
-
#visible_columns(selected_keys = nil, view_context = nil) ⇒ Array<Column>
Get visible columns for user (respecting selection).
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_actions ⇒ Object (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 |
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
4 5 6 |
# File 'app/models/spree/admin/table.rb', line 4 def columns @columns end |
#context ⇒ Object (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_label ⇒ Object
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_param ⇒ Object
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 |
#key ⇒ Object (readonly)
Returns the value of attribute key.
4 5 6 |
# File 'app/models/spree/admin/table.rb', line 4 def key @key end |
#link_to_action ⇒ Object
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_class ⇒ Object (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_resource ⇒ Object
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_actions ⇒ Object
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_delete ⇒ Object
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_edit ⇒ Object
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_param ⇒ Object
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_placeholder ⇒ Object
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
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, **, &block) key = key.to_sym # Handle :if as alias for :condition [:condition] = .delete(:if) if .key?(:if) column = Column.new(**.merge(key: key.to_s)) unless column.valid? errors = column.errors..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
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, **) key = key.to_sym action = BulkAction.new(**.merge(key: key)) unless action.valid? errors = action.errors..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
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_columns ⇒ Array<Column>
Get all available columns that can be displayed
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)
253 254 255 |
# File 'app/models/spree/admin/table.rb', line 253 def bulk_operations_enabled? @bulk_actions.any? end |
#clear ⇒ Object
Clear all columns
283 284 285 |
# File 'app/models/spree/admin/table.rb', line 283 def clear @columns.clear end |
#clear_bulk_actions ⇒ Object
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
27 28 29 |
# File 'app/models/spree/admin/table.rb', line 27 def date_range? @date_range_param.present? end |
#deep_clone ⇒ Table
Deep clone the registry
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_columns ⇒ Array<Column>
Get default columns (marked as default: true and displayable)
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
106 107 108 |
# File 'app/models/spree/admin/table.rb', line 106 def exists?(key) @columns.key?(key.to_sym) end |
#filterable_columns ⇒ Array<Column>
Get filterable columns (columns that can be used in query builder)
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
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
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
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
127 128 129 130 131 132 |
# File 'app/models/spree/admin/table.rb', line 127 def insert_after(target_key, new_key, **) target = find(target_key) return nil unless target add(new_key, **.merge(position: target.position + 1)) end |
#insert_before(target_key, new_key, **options) ⇒ Column?
Insert column before another column
115 116 117 118 119 120 |
# File 'app/models/spree/admin/table.rb', line 115 def insert_before(target_key, new_key, **) target = find(target_key) return nil unless target add(new_key, **.merge(position: target.position - 1)) end |
#inspect ⇒ Object
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
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
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
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
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
39 40 41 |
# File 'app/models/spree/admin/table.rb', line 39 def row_actions_edit? @row_actions_edit end |
#sortable_columns ⇒ Array<Column>
Get sortable columns
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
87 88 89 90 91 92 93 94 |
# File 'app/models/spree/admin/table.rb', line 87 def update(key, **) column = @columns[key.to_sym] return nil unless column apply_attributes(column, ) sort_columns! column end |
#update_bulk_action(key, **options) ⇒ BulkAction?
Update an existing bulk action
228 229 230 231 232 233 234 235 |
# File 'app/models/spree/admin/table.rb', line 228 def update_bulk_action(key, **) action = @bulk_actions[key.to_sym] return nil unless action apply_attributes(action, ) sort_bulk_actions! action end |
#visible_bulk_actions(view_context = nil) ⇒ Array<BulkAction>
Get visible bulk actions for the given context
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)
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 |