Class: Collavre::Creatives::Filters::SearchFilter

Inherits:
BaseFilter
  • Object
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

Constructor Details

This class inherits a constructor from Collavre::Creatives::Filters::BaseFilter

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'app/services/collavre/creatives/filters/search_filter.rb', line 7

def active?
  params[:search].present?
end

#matchObject



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?

  # Build AND conditions: each word must appear in description OR comments
  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