4
5
6
7
8
9
10
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/netzke/sequel/relation_extensions.rb', line 4
def self.included receiver
receiver.class_eval do
def_dataset_method :extend_with do |*params|
scope = params.shift
case scope.class.name
when "Symbol" self.send(scope, *params)
when "String" params.empty? ? self.where(scope) : self.where([scope, *params])
when "Array"
self.extend_with(*scope)
when "Hash" self.where(scope)
when "ActiveSupport::HashWithIndifferentAccess" self.where(scope)
when "Proc" scope.call(self)
else
raise ArgumentError, "Wrong parameter type for ActiveRecord::Relation#extend_with"
end
end
def_dataset_method :extend_with_netzke_conditions do |cond|
cond.each_pair.inject(self) do |r, (k,v)|
assoc, method, *operator = k.to_s.split("__")
operator.empty? ? r.where(assoc.to_sym.send(method) => v) : r.where(assoc.to_sym => {method.to_sym.send(operator.last) => v}).joins(assoc.to_sym)
end
end
end
end
|