11
12
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
|
# File 'lib/the_where/range.rb', line 11
def range_scope(params)
where_string = []
where_hash = {}
PATTERN.each do |char, sign|
options = params.select{ |key, _| key.end_with?(char) }
options.each do |key, value|
exp = Regexp.new(char + '$')
real_key = key.sub(exp, '')
agent_key = key.gsub(/[-\.]/, '_')
where_string << "#{real_key} #{sign} :#{agent_key}"
where_hash.merge! agent_key.to_sym => value
end
end
where_string = where_string.join ' AND '
if where_string.present?
condition = [where_string, where_hash]
where(condition)
else
all
end
end
|