Class: Spree::Admin::Table::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/admin/table/query_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ QueryBuilder

Returns a new instance of QueryBuilder.



7
8
9
10
# File 'app/models/spree/admin/table/query_builder.rb', line 7

def initialize(table)
  @table = table
  @root_group = FilterGroup.new(combinator: :and)
end

Instance Attribute Details

#root_groupObject (readonly)

Returns the value of attribute root_group.



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

def root_group
  @root_group
end

#tableObject (readonly)

Returns the value of attribute table.



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

def table
  @table
end

Instance Method Details

#add_filter(field:, operator:, value:) ⇒ Filter

Add a filter to the root group

Parameters:

  • field (String)

    ransack attribute name

  • operator (Symbol)

    filter operator

  • value (Object)

    filter value

Returns:



17
18
19
20
21
# File 'app/models/spree/admin/table/query_builder.rb', line 17

def add_filter(field:, operator:, value:)
  filter = Filter.new(field: field, operator: operator, value: value)
  @root_group.add_filter(filter)
  filter
end

#add_group(combinator: :and) ⇒ FilterGroup

Add a nested group

Parameters:

  • combinator (Symbol) (defaults to: :and)

    :and or :or

Returns:



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

def add_group(combinator: :and)
  group = FilterGroup.new(combinator: combinator)
  @root_group.add_group(group)
  group
end

#available_fields(view_context = nil) ⇒ Array<Hash>

Get available fields for filtering based on table configuration

Parameters:

  • view_context (ActionView::Base) (defaults to: nil)

    optional view context for resolving dynamic URLs

Returns:

  • (Array<Hash>)


75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/spree/admin/table/query_builder.rb', line 75

def available_fields(view_context = nil)
  @table.filterable_columns.map do |column|
    {
      key: column.ransack_attribute,
      label: column.resolve_label,
      type: column.filter_type.to_s,
      operators: column.operators.map(&:to_s),
      value_options: format_value_options(column.value_options),
      search_url: resolve_search_url(column.search_url, view_context)
    }
  end
end

#available_operatorsArray<Hash>

Get available operators for UI

Returns:

  • (Array<Hash>)


90
91
92
# File 'app/models/spree/admin/table/query_builder.rb', line 90

def available_operators
  Filter.operators_for_select
end

#clearObject

Clear all filters



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

def clear
  @root_group = FilterGroup.new(combinator: :and)
end

#empty?Boolean

Check if there are any filters

Returns:

  • (Boolean)


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

def empty?
  @root_group.empty?
end

#load_from_json(json_string) ⇒ Object

Load state from JSON string

Parameters:

  • json_string (String)


63
64
65
66
67
68
69
70
# File 'app/models/spree/admin/table/query_builder.rb', line 63

def load_from_json(json_string)
  return if json_string.blank?

  params = JSON.parse(json_string, symbolize_names: true)
  @root_group = FilterGroup.from_params(params)
rescue JSON::ParserError
  @root_group = FilterGroup.new(combinator: :and)
end

#load_from_params(params) ⇒ Object

Load state from params hash

Parameters:

  • params (Hash)


57
58
59
# File 'app/models/spree/admin/table/query_builder.rb', line 57

def load_from_params(params)
  @root_group = FilterGroup.from_params(params)
end

#to_json_stateString

Convert to JSON string for storing in hidden field

Returns:

  • (String)


51
52
53
# File 'app/models/spree/admin/table/query_builder.rb', line 51

def to_json_state
  @root_group.to_h.to_json
end

#to_ransack_paramsHash

Convert to ransack params format

Returns:

  • (Hash)


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

def to_ransack_params
  @root_group.to_ransack_params
end