Module: Lipstick::Filterable::ClassMethods
- Defined in:
- lib/lipstick/filterable.rb
Instance Attribute Summary collapse
-
#filterable_fields ⇒ Object
readonly
Returns the value of attribute filterable_fields.
Instance Method Summary collapse
- #filterable_by(*fields) ⇒ Object
- #filterable_filter(query, collation = 'utf8mb4_unicode_ci') ⇒ Object
Instance Attribute Details
#filterable_fields ⇒ Object (readonly)
Returns the value of attribute filterable_fields.
31 32 33 |
# File 'lib/lipstick/filterable.rb', line 31 def filterable_fields @filterable_fields end |
Instance Method Details
#filterable_by(*fields) ⇒ Object
33 34 35 |
# File 'lib/lipstick/filterable.rb', line 33 def filterable_by(*fields) @filterable_fields = fields end |
#filterable_filter(query, collation = 'utf8mb4_unicode_ci') ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/lipstick/filterable.rb', line 37 def filterable_filter(query, collation = 'utf8mb4_unicode_ci') filter_terms(query).reduce(all) do |scope, term| conds = filterable_fields.map do |f| CollatedArelAttribute.new(arel_table[f], collation) .matches(term) end scope.where(conds.reduce { |acc, elem| acc.or(elem) }) end end |