Module: Spree::Admin::TableConcern

Extended by:
ActiveSupport::Concern
Included in:
AllowedOriginsController, ApiKeysController, CheckoutsController, OrdersController, ResourceController, StockItemsController, StockMovementsController, WebhookDeliveriesController, WebhookEndpointsController
Defined in:
app/controllers/concerns/spree/admin/table_concern.rb

Instance Method Summary collapse

Instance Method Details

#apply_table_sort(collection) ⇒ ActiveRecord::Relation

Apply custom sort scopes if the table has them configured Should be called after ransack result is obtained

Parameters:

  • collection (ActiveRecord::Relation)

    the collection to sort

Returns:

  • (ActiveRecord::Relation)

    sorted collection



42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/concerns/spree/admin/table_concern.rb', line 42

def apply_table_sort(collection)
  return collection unless table
  return collection unless params[:q].respond_to?(:dig)

  sort_param = params.dig(:q, :s)
  column = table.find_custom_sort_column(sort_param)

  return collection unless column

  # Remove sort from ransack params since we'll apply it manually
  table.apply_custom_sort(collection.reorder(nil), sort_param)
end

#custom_sort_active?Boolean

Check if current sort uses a custom scope

Returns:

  • (Boolean)


57
58
59
60
61
62
63
# File 'app/controllers/concerns/spree/admin/table_concern.rb', line 57

def custom_sort_active?
  return false unless table
  return false unless params[:q].respond_to?(:dig)

  sort_param = params.dig(:q, :s)
  table.find_custom_sort_column(sort_param).present?
end

#process_table_query_statevoid

This method returns an undefined value.

Process query_state parameter from table query builder and merge it into params for ransack



68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/concerns/spree/admin/table_concern.rb', line 68

def process_table_query_state
  return unless params[:query_state].present?

  query_builder = Spree::Admin::Table::QueryBuilder.new(table)
  query_builder.load_from_json(params[:query_state])

  ransack_params = query_builder.to_ransack_params
  return if ransack_params.blank?

  params[:q] ||= {}
  params[:q].merge!(ransack_params)
end

#tableSpree::Admin::Table?

Get the table for this controller

Returns:



28
29
30
# File 'app/controllers/concerns/spree/admin/table_concern.rb', line 28

def table
  @table ||= Spree.admin.tables.get(table_key)
end

#table_keySymbol

Get the table key for this controller Uses explicitly set key or derives from controller_name

Returns:

  • (Symbol)


22
23
24
# File 'app/controllers/concerns/spree/admin/table_concern.rb', line 22

def table_key
  _table_key || controller_name.to_sym
end

#table_registered?Boolean

Check if a table is registered for this controller

Returns:

  • (Boolean)


34
35
36
# File 'app/controllers/concerns/spree/admin/table_concern.rb', line 34

def table_registered?
  Spree.admin.tables.registered?(table_key)
end