Class: Spree::Admin::Table::Filter
- Inherits:
-
Object
- Object
- Spree::Admin::Table::Filter
- Defined in:
- app/models/spree/admin/table/filter.rb
Constant Summary collapse
- OPERATORS =
{ eq: { label: 'equals', predicate: '_eq' }, not_eq: { label: 'does not equal', predicate: '_not_eq' }, cont: { label: 'contains', predicate: '_cont' }, not_cont: { label: 'does not contain', predicate: '_not_cont' }, start: { label: 'starts with', predicate: '_start' }, end: { label: 'ends with', predicate: '_end' }, gt: { label: 'greater than', predicate: '_gt' }, gteq: { label: 'greater than or equal to', predicate: '_gteq' }, lt: { label: 'less than', predicate: '_lt' }, lteq: { label: 'less than or equal to', predicate: '_lteq' }, in: { label: 'is any of', predicate: '_in' }, not_in: { label: 'is none of', predicate: '_not_in' }, null: { label: 'is empty', predicate: '_null', no_value: true }, not_null: { label: 'is not empty', predicate: '_not_null', no_value: true } }.freeze
Instance Attribute Summary collapse
-
#field ⇒ Object
Returns the value of attribute field.
-
#id ⇒ Object
Returns the value of attribute id.
-
#operator ⇒ Object
Returns the value of attribute operator.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
-
.from_params(params) ⇒ Filter?
Create filter from params hash.
-
.operators_for_select ⇒ Array<Hash>
Get available operators with labels for UI.
Instance Method Summary collapse
-
#initialize(field: nil, operator: :eq, value: nil, id: nil) ⇒ Filter
constructor
A new instance of Filter.
-
#operator_label ⇒ String
Get human-readable operator label.
-
#requires_value? ⇒ Boolean
Check if this operator requires a value.
-
#to_h ⇒ Hash
Convert to hash.
-
#to_ransack_param ⇒ Hash
Convert to ransack parameter format.
Constructor Details
#initialize(field: nil, operator: :eq, value: nil, id: nil) ⇒ Filter
Returns a new instance of Filter.
24 25 26 27 28 29 |
# File 'app/models/spree/admin/table/filter.rb', line 24 def initialize(field: nil, operator: :eq, value: nil, id: nil) @field = field @operator = operator.to_sym @value = value @id = id || SecureRandom.hex(8) end |
Instance Attribute Details
#field ⇒ Object
Returns the value of attribute field.
22 23 24 |
# File 'app/models/spree/admin/table/filter.rb', line 22 def field @field end |
#id ⇒ Object
Returns the value of attribute id.
22 23 24 |
# File 'app/models/spree/admin/table/filter.rb', line 22 def id @id end |
#operator ⇒ Object
Returns the value of attribute operator.
22 23 24 |
# File 'app/models/spree/admin/table/filter.rb', line 22 def operator @operator end |
#value ⇒ Object
Returns the value of attribute value.
22 23 24 |
# File 'app/models/spree/admin/table/filter.rb', line 22 def value @value end |
Class Method Details
.from_params(params) ⇒ Filter?
Create filter from params hash
94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/models/spree/admin/table/filter.rb', line 94 def self.from_params(params) return nil unless params.is_a?(Hash) params = params.symbolize_keys new( field: params[:field], operator: params[:operator] || :eq, value: params[:value], id: params[:id] ) end |
.operators_for_select ⇒ Array<Hash>
Get available operators with labels for UI
108 109 110 111 112 |
# File 'app/models/spree/admin/table/filter.rb', line 108 def self.operators_for_select OPERATORS.map do |key, config| { value: key.to_s, label: config[:label], no_value: config[:no_value] || false } end end |
Instance Method Details
#operator_label ⇒ String
Get human-readable operator label
75 76 77 |
# File 'app/models/spree/admin/table/filter.rb', line 75 def operator_label OPERATORS.dig(@operator, :label) || @operator.to_s.humanize end |
#requires_value? ⇒ Boolean
Check if this operator requires a value
81 82 83 |
# File 'app/models/spree/admin/table/filter.rb', line 81 def requires_value? !OPERATORS.dig(@operator, :no_value) end |
#to_h ⇒ Hash
Convert to hash
87 88 89 |
# File 'app/models/spree/admin/table/filter.rb', line 87 def to_h { field: field, operator: operator, value: value, id: id } end |
#to_ransack_param ⇒ Hash
Convert to ransack parameter format
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/models/spree/admin/table/filter.rb', line 33 def to_ransack_param return {} if field.blank? operator_config = OPERATORS[@operator] return {} unless operator_config param_name = "#{field}#{operator_config[:predicate]}" if operator_config[:no_value] { param_name => true } else { param_name => extract_ransack_value } end end |