Class: Headmin::Filter::Field
- Defined in:
- app/models/headmin/filter/field.rb
Constant Summary collapse
- OPERATORS =
%w[eq not_eq matches does_not_match]
Constants inherited from Base
Base::OPERATORS_CONVERT_TO, Base::QUERY_OPERATOR_CONVERT_TO, Base::QUERY_VALUE_CONVERT_TO
Instance Attribute Summary
Attributes inherited from Base
#attribute, #instructions, #raw_value
Instance Method Summary collapse
- #build_query(query, collection, instruction) ⇒ Object
- #cast_value(value) ⇒ Object
- #display_value(value) ⇒ Object
-
#initialize(attribute, params, association: nil) ⇒ Field
constructor
A new instance of Field.
- #query(collection) ⇒ Object
Methods inherited from Base
#conditionals, #is_i?, #operators, #string, #values
Constructor Details
#initialize(attribute, params, association: nil) ⇒ Field
Returns a new instance of Field.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'app/models/headmin/filter/field.rb', line 6 def initialize(attribute, params, association: nil) @attribute = association ? "#{association}_#{attribute}".to_sym : attribute @attribute = "field_#{attribute}".to_sym @raw_value = params[@attribute] @association = association @instructions = [] if params.key?(@attribute) parse(@raw_value) end end |
Instance Method Details
#build_query(query, collection, instruction) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/models/headmin/filter/field.rb', line 39 def build_query(query, collection, instruction) query_operator = convert_to_query_operator(instruction[:operator]) query_value = convert_to_query_value(instruction[:value], instruction[:operator]) query_operator, query_value = process_null_operators(query_operator, query_value) model = collection.is_a?(Class) ? collection : collection.model new_attribute = attribute.to_s.gsub("field_", "").to_sym fields_model = model.reflect_on_association(:fields).klass new_query = fields_model.arel_table[:name].matches(new_attribute).and(fields_model.arel_table[:value].send(query_operator, query_value)) query ? query.send(instruction[:conditional], new_query) : new_query end |
#cast_value(value) ⇒ Object
18 19 20 |
# File 'app/models/headmin/filter/field.rb', line 18 def cast_value(value) value end |
#display_value(value) ⇒ Object
22 23 24 |
# File 'app/models/headmin/filter/field.rb', line 22 def display_value(value) value.downcase end |
#query(collection) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/models/headmin/filter/field.rb', line 26 def query(collection) return collection unless @instructions.any? query = nil @instructions.each do |instruction| query = build_query(query, collection, instruction) end collection = collection.joins(:fields) collection.where(query) end |