Module: TheWhere::Like

Included in:
TheWhere
Defined in:
lib/the_where/like.rb

Instance Method Summary collapse

Instance Method Details

#filter_like(params) ⇒ Object



26
27
28
29
30
# File 'lib/the_where/like.rb', line 26

def filter_like(params)
  params.select do |k, _|
    k.end_with?('-like')
  end
end

#like_scope(params) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/the_where/like.rb', line 4

def like_scope(params)
  where_string = []
  where_hash = {}

  params.select{ |k, _| k.end_with?('-like') }.each do |key, value|
    real_key = key.sub(/-like$/, '')
    agent_key = key.gsub(/[-\.]/, '_')

    where_string << "#{real_key} like :#{agent_key}"
    where_hash.merge! agent_key.to_sym => '%' + value.to_s + '%'
  end

  where_string = where_string.join ' AND '

  if where_string.present?
    condition = [where_string, where_hash]
    where(condition)
  else
    all
  end
end