Module: Querifier::Queries::Where
- Defined in:
- lib/querifier/queries/where.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(message, *args, &block) ⇒ Object
30
31
32
33
|
# File 'lib/querifier/queries/where.rb', line 30
def method_missing(message, *args, &block)
return filter_by(Regexp.last_match(1).to_sym, args.first) if message.to_s =~ /filter_by_(.*)/ super
end
|
Class Method Details
.included(klass) ⇒ Object
45
46
47
|
# File 'lib/querifier/queries/where.rb', line 45
def self.included(klass)
klass.extend(ClassMethods)
end
|
Instance Method Details
#collection ⇒ Object
4
5
6
7
8
|
# File 'lib/querifier/queries/where.rb', line 4
def collection
super
filter
@collection
end
|
#filter ⇒ Object
10
11
12
13
14
15
16
17
18
|
# File 'lib/querifier/queries/where.rb', line 10
def filter
return self if @filtered
filter_params.each do |(attribute, _)|
value = filter_value(attribute)
send("filter_by_#{attribute}", value) if value
end
@filtered = true
self
end
|
#filter_by(name, value) ⇒ Object
24
25
26
27
28
|
# File 'lib/querifier/queries/where.rb', line 24
def filter_by(name, value)
filter_content = filter_value(name)
@collection = @collection.where("#{name} LIKE ?", "%#{filter_content}%") if filter_content
self
end
|
#filter_params ⇒ Object
39
40
41
42
|
# File 'lib/querifier/queries/where.rb', line 39
def filter_params
@filter_params ||=
params.fetch(Config.where_param, {}).select { |(k, _)| valid_option? k.to_sym }
end
|
#filter_value(key) ⇒ Object
20
21
22
|
# File 'lib/querifier/queries/where.rb', line 20
def filter_value(key)
filter_params.fetch(key.to_sym, nil)
end
|
#valid_option?(option) ⇒ Boolean
35
36
37
|
# File 'lib/querifier/queries/where.rb', line 35
def valid_option?(option)
self.class.where_attributes.include?(option.to_sym)
end
|