1
2
3
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
require 'active_support/core_ext/object/blank'
module ActiveRecord
module SpawnMethods
def merge(r)
merged_relation = clone
return merged_relation unless r
return to_a & r if r.is_a?(Array)
Relation::ASSOCIATION_METHODS.each do |method|
value = r.send(:"#{method}_values")
unless value.empty?
if method == :includes
merged_relation = merged_relation.includes(value)
else
merged_relation.send(:"#{method}_values=", value)
end
end
end
(Relation::MULTI_VALUE_METHODS - [:joins, :where]).each do |method|
value = r.send(:"#{method}_values")
merged_relation.send(:"#{method}_values=", merged_relation.send(:"#{method}_values") + value) if value.present?
end
merged_relation = merged_relation.joins(r.joins_values)
merged_wheres = @where_values + r.where_values
# Remove duplicates, last one wins.
seen = {}
merged_wheres = merged_wheres.reverse.reject { |w|
nuke = false
if w.respond_to?(:operator) && w.operator == :==
name = w.left.name
nuke = seen[name]
seen[name] = true
end
nuke
}.reverse
merged_relation.where_values = merged_wheres
(Relation::SINGLE_VALUE_METHODS - [:lock]).each do |method|
value = r.send(:"#{method}_value")
merged_relation.send(:"#{method}_value=", value) unless value.nil?
end
merged_relation.lock_value = r.lock_value unless merged_relation.lock_value
# Apply scope extension modules
merged_relation.send :apply_modules, r.extensions
merged_relation
end
alias :& :merge
def except(*skips)
result = self.class.new(@klass, table)
((Relation::ASSOCIATION_METHODS + Relation::MULTI_VALUE_METHODS) - skips).each do |method|
result.send(:"#{method}_values=", send(:"#{method}_values"))
end
(Relation::SINGLE_VALUE_METHODS - skips).each do |method|
result.send(:"#{method}_value=", send(:"#{method}_value"))
end
result
end
def only(*onlies)
result = self.class.new(@klass, table)
((Relation::ASSOCIATION_METHODS + Relation::MULTI_VALUE_METHODS) & onlies).each do |method|
result.send(:"#{method}_values=", send(:"#{method}_values"))
end
(Relation::SINGLE_VALUE_METHODS & onlies).each do |method|
result.send(:"#{method}_value=", send(:"#{method}_value"))
end
result
end
VALID_FIND_OPTIONS = [ :conditions, :include, :joins, :limit, :offset, :extend,
:order, :select, :readonly, :group, :having, :from, :lock ]
def apply_finder_options(options)
relation = clone
return relation unless options
options.assert_valid_keys(VALID_FIND_OPTIONS)
finders = options.dup
finders.delete_if { |key, value| value.nil? }
([:joins, :select, :group, :order, :having, :limit, :offset, :from, :lock, :readonly] & finders.keys).each do |finder|
relation = relation.send(finder, finders[finder])
end
relation = relation.where(finders[:conditions]) if options.has_key?(:conditions)
relation = relation.includes(finders[:include]) if options.has_key?(:include)
relation = relation.extending(finders[:extend]) if options.has_key?(:extend)
relation
end
end
end
|