Class: ActiveRecord::SearchService::Search
- Inherits:
-
Object
- Object
- ActiveRecord::SearchService::Search
- Defined in:
- app/services/active_record/search_service.rb
Instance Attribute Summary collapse
-
#fuzzy ⇒ Object
readonly
Returns the value of attribute fuzzy.
-
#param_key ⇒ Object
readonly
Returns the value of attribute param_key.
-
#search_in ⇒ Object
readonly
Returns the value of attribute search_in.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #apply(relation) ⇒ Object
-
#initialize(params, search) ⇒ Search
constructor
A new instance of Search.
Constructor Details
#initialize(params, search) ⇒ Search
Returns a new instance of Search.
6 7 8 9 10 11 |
# File 'app/services/active_record/search_service.rb', line 6 def initialize(params, search) @search_in = search[:in] || [] @param_key = search[:param_key] @fuzzy = search[:fuzzy] @value = params.delete(param_key) end |
Instance Attribute Details
#fuzzy ⇒ Object (readonly)
Returns the value of attribute fuzzy.
4 5 6 |
# File 'app/services/active_record/search_service.rb', line 4 def fuzzy @fuzzy end |
#param_key ⇒ Object (readonly)
Returns the value of attribute param_key.
4 5 6 |
# File 'app/services/active_record/search_service.rb', line 4 def param_key @param_key end |
#search_in ⇒ Object (readonly)
Returns the value of attribute search_in.
4 5 6 |
# File 'app/services/active_record/search_service.rb', line 4 def search_in @search_in end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
4 5 6 |
# File 'app/services/active_record/search_service.rb', line 4 def value @value end |
Instance Method Details
#apply(relation) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/services/active_record/search_service.rb', line 13 def apply(relation) return relation if value.blank? values = [] if fuzzy query = search_in.collect do |attr| # if attr.include? '.' # relation = relation.joins(attr.split('.').first.singularize) # end if relation.model.columns_hash[attr]&.array values << value.to_s.downcase "? = ANY (#{attr})" else values << "%#{value.to_s.downcase}%" "#{attr} iLIKE ?" end end.join(' OR ') else query = search_in.collect do |attr| # if attr.include? '.' # relation = relation.joins(attr.split('.').first.singularize) # end if relation.model.columns_hash[attr]&.array "? = ANY (#{attr})" else "lower(#{attr}) = ?" end end.join(' OR ') values = [value.to_s.downcase] * search_in.count end relation.where(query, *values) end |