Class: Collavre::Creatives::Filters::SearchFilter
- Inherits:
-
BaseFilter
- Object
- BaseFilter
- Collavre::Creatives::Filters::SearchFilter
show all
- Defined in:
- app/services/collavre/creatives/filters/search_filter.rb
Constant Summary
collapse
- MAX_SEARCH_WORDS =
5
Instance Method Summary
collapse
Methods inherited from BaseFilter
#initialize
Instance Method Details
#active? ⇒ Boolean
7
8
9
|
# File 'app/services/collavre/creatives/filters/search_filter.rb', line 7
def active?
params[:search].present?
end
|
#match ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'app/services/collavre/creatives/filters/search_filter.rb', line 11
def match
words = params[:search].to_s.strip.split(/\s+/).first(MAX_SEARCH_WORDS)
return [] if words.empty?
conditions = []
binds = {}
words.each_with_index do |word, i|
key = :"q#{i}"
binds[key] = "%#{sanitize_like(word.downcase)}%"
conditions << "(LOWER(creatives.description) LIKE :#{key} ESCAPE '\\' " \
"OR LOWER(comments.content) LIKE :#{key} ESCAPE '\\')"
end
scope
.left_joins(:comments)
.where(conditions.join(" AND "), **binds)
.distinct
.pluck(:id)
end
|