Class: Spree::Admin::Table::QueryBuilder
- Inherits:
-
Object
- Object
- Spree::Admin::Table::QueryBuilder
- Defined in:
- app/models/spree/admin/table/query_builder.rb
Instance Attribute Summary collapse
-
#root_group ⇒ Object
readonly
Returns the value of attribute root_group.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
-
#add_filter(field:, operator:, value:) ⇒ Filter
Add a filter to the root group.
-
#add_group(combinator: :and) ⇒ FilterGroup
Add a nested group.
-
#available_fields(view_context = nil) ⇒ Array<Hash>
Get available fields for filtering based on table configuration.
-
#available_operators ⇒ Array<Hash>
Get available operators for UI.
-
#clear ⇒ Object
Clear all filters.
-
#empty? ⇒ Boolean
Check if there are any filters.
-
#initialize(table) ⇒ QueryBuilder
constructor
A new instance of QueryBuilder.
-
#load_from_json(json_string) ⇒ Object
Load state from JSON string.
-
#load_from_params(params) ⇒ Object
Load state from params hash.
-
#to_json_state ⇒ String
Convert to JSON string for storing in hidden field.
-
#to_ransack_params ⇒ Hash
Convert to ransack params format.
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_group ⇒ Object (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 |
#table ⇒ Object (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
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
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
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: (column.), search_url: resolve_search_url(column.search_url, view_context) } end end |
#available_operators ⇒ Array<Hash>
Get available operators for UI
90 91 92 |
# File 'app/models/spree/admin/table/query_builder.rb', line 90 def available_operators Filter.operators_for_select end |
#clear ⇒ Object
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
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
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
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_state ⇒ String
Convert to JSON string for storing in hidden field
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_params ⇒ Hash
Convert to ransack params format
45 46 47 |
# File 'app/models/spree/admin/table/query_builder.rb', line 45 def to_ransack_params @root_group.to_ransack_params end |