blob: 29dd0643d6116e8ec5055e3fcedf3abef35b0976 (
plain) (
blame)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
module ActiveRecord
module Associations
class Preloader
class Association #:nodoc:
attr_reader :owners, :reflection, :preload_scope, :model, :klass
attr_reader :preloaded_records
def initialize(klass, owners, reflection, preload_scope)
@klass = klass
@owners = owners
@reflection = reflection
@preload_scope = preload_scope
@model = owners.first && owners.first.class
@scope = nil
@preloaded_records = []
end
def run(preloader)
preload(preloader)
end
def preload(preloader)
raise NotImplementedError
end
def scope
@scope ||= build_scope
end
def records_for(ids)
query_scope(ids)
end
def query_scope(ids)
scope.where(association_key_name => ids)
end
def table
klass.arel_table
end
# The name of the key on the associated records
def association_key_name
raise NotImplementedError
end
# This is overridden by HABTM as the condition should be on the foreign_key column in
# the join table
def association_key
table[association_key_name]
end
# The name of the key on the model which declares the association
def owner_key_name
raise NotImplementedError
end
def options
reflection.options
end
private
def associated_records_by_owner(preloader)
records = load_records
owners.each_with_object({}) do |owner, result|
result[owner] = records[convert_key(owner[owner_key_name])] || []
end
end
def owner_keys
unless defined?(@owner_keys)
@owner_keys = owners.map do |owner|
owner[owner_key_name]
end
@owner_keys.uniq!
@owner_keys.compact!
end
@owner_keys
end
def key_conversion_required?
@key_conversion_required ||= association_key_type != owner_key_type
end
def convert_key(key)
if key_conversion_required?
key.to_s
else
key
end
end
def association_key_type
@klass.type_for_attribute(association_key_name.to_s).type
end
def owner_key_type
@model.type_for_attribute(owner_key_name.to_s).type
end
def load_records
return {} if owner_keys.empty?
# Some databases impose a limit on the number of ids in a list (in Oracle it's 1000)
# Make several smaller queries if necessary or make one query if the adapter supports it
slices = owner_keys.each_slice(klass.connection.in_clause_length || owner_keys.size)
@preloaded_records = slices.flat_map do |slice|
records_for(slice)
end
@preloaded_records.group_by do |record|
convert_key(record[association_key_name])
end
end
def reflection_scope
@reflection_scope ||= reflection.scope ? klass.unscoped.instance_exec(nil, &reflection.scope) : klass.unscoped
end
def build_scope
scope = klass.unscoped
values = reflection_scope.values
preload_values = preload_scope.values
scope.where_clause = reflection_scope.where_clause + preload_scope.where_clause
scope.references_values = Array(values[:references]) + Array(preload_values[:references])
if preload_values[:select] || values[:select]
scope._select!(preload_values[:select] || values[:select])
end
scope.includes! preload_values[:includes] || values[:includes]
if preload_scope.joins_values.any?
scope.joins!(preload_scope.joins_values)
else
scope.joins!(reflection_scope.joins_values)
end
scope.order! preload_values[:order] || values[:order]
if preload_values[:readonly] || values[:readonly]
scope.readonly!
end
if options[:as]
scope.where!(klass.table_name => { reflection.type => model.base_class.sti_name })
end
scope.unscope_values = Array(values[:unscope]) + Array(preload_values[:unscope])
klass.default_scoped.merge(scope)
end
end
end
end
end
|