Class: ActiveRecord::Associations::Preloader::Association
- Inherits:
-
Object
- Object
- ActiveRecord::Associations::Preloader::Association
show all
- Defined in:
- lib/active_record/associations/preloader/association.rb
Overview
Defined Under Namespace
Classes: LoaderQuery, LoaderRecords
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(klass, owners, reflection, preload_scope, reflection_scope, associate_by_default) ⇒ Association
Returns a new instance of Association.
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/active_record/associations/preloader/association.rb', line 104
def initialize(klass, owners, reflection, preload_scope, reflection_scope, associate_by_default)
@klass = klass
@owners = owners.uniq(&:__id__)
@reflection = reflection
@preload_scope = preload_scope
@reflection_scope = reflection_scope
@associate = associate_by_default || !preload_scope || preload_scope.empty_scope?
@model = owners.first && owners.first.class
@run = false
end
|
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
102
103
104
|
# File 'lib/active_record/associations/preloader/association.rb', line 102
def klass
@klass
end
|
Instance Method Details
#associate_records_from_unscoped(unscoped_records) ⇒ Object
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
# File 'lib/active_record/associations/preloader/association.rb', line 218
def associate_records_from_unscoped(unscoped_records)
return if unscoped_records.nil? || unscoped_records.empty?
return if !reflection_scope.empty_scope?
return if preload_scope && !preload_scope.empty_scope?
return if reflection.collection?
unscoped_records.select { |r| r[association_key_name].present? }.each do |record|
owners = owners_by_key[derive_key(record, association_key_name)]
owners&.each_with_index do |owner, i|
association = owner.association(reflection.name)
association.target = record
if i == 0 association.set_inverse_instance(record)
end
end
end
end
|
#association_key_name ⇒ Object
The name of the key on the associated records
161
162
163
|
# File 'lib/active_record/associations/preloader/association.rb', line 161
def association_key_name
reflection.join_primary_key(klass)
end
|
#future_classes ⇒ Object
119
120
121
122
123
124
125
|
# File 'lib/active_record/associations/preloader/association.rb', line 119
def future_classes
if run?
[]
else
[@klass]
end
end
|
#load_records(raw_records = nil) ⇒ Object
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/active_record/associations/preloader/association.rb', line 197
def load_records(raw_records = nil)
@records_by_owner = {}.compare_by_identity
raw_records ||= loader_query.records_for([self])
@preloaded_records = raw_records.select do |record|
assignments = false
owners_by_key[derive_key(record, association_key_name)]&.each do |owner|
entries = (@records_by_owner[owner] ||= [])
if reflection.collection? || entries.empty?
entries << record
assignments = true
end
end
assignments
end
end
|
#loaded?(owner) ⇒ Boolean
176
177
178
|
# File 'lib/active_record/associations/preloader/association.rb', line 176
def loaded?(owner)
owner.association(reflection.name).loaded?
end
|
#loader_query ⇒ Object
165
166
167
|
# File 'lib/active_record/associations/preloader/association.rb', line 165
def loader_query
LoaderQuery.new(scope, association_key_name)
end
|
#owners_by_key ⇒ Object
169
170
171
172
173
174
|
# File 'lib/active_record/associations/preloader/association.rb', line 169
def owners_by_key
@owners_by_key ||= owners.each_with_object({}) do |owner, result|
key = derive_key(owner, owner_key_name)
(result[key] ||= []) << owner if key
end
end
|
#preloaded_records ⇒ Object
154
155
156
157
158
|
# File 'lib/active_record/associations/preloader/association.rb', line 154
def preloaded_records
load_records unless defined?(@preloaded_records)
@preloaded_records
end
|
#records_by_owner ⇒ Object
148
149
150
151
152
|
# File 'lib/active_record/associations/preloader/association.rb', line 148
def records_by_owner
load_records unless defined?(@records_by_owner)
@records_by_owner
end
|
#run ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/active_record/associations/preloader/association.rb', line 135
def run
return self if run?
@run = true
records = records_by_owner
owners.each do |owner|
associate_records_to_owner(owner, records[owner] || [])
end if @associate
self
end
|
#run? ⇒ Boolean
131
132
133
|
# File 'lib/active_record/associations/preloader/association.rb', line 131
def run?
@run
end
|
#runnable_loaders ⇒ Object
127
128
129
|
# File 'lib/active_record/associations/preloader/association.rb', line 127
def runnable_loaders
[self]
end
|
#scope ⇒ Object
184
185
186
|
# File 'lib/active_record/associations/preloader/association.rb', line 184
def scope
@scope ||= build_scope
end
|
#set_inverse(record) ⇒ Object
188
189
190
191
192
193
194
195
|
# File 'lib/active_record/associations/preloader/association.rb', line 188
def set_inverse(record)
if owners = owners_by_key[derive_key(record, association_key_name)]
association = owners.first.association(reflection.name)
association.set_inverse_instance(record)
end
end
|
#table_name ⇒ Object
115
116
117
|
# File 'lib/active_record/associations/preloader/association.rb', line 115
def table_name
@klass.table_name
end
|
#target_for(owner) ⇒ Object
180
181
182
|
# File 'lib/active_record/associations/preloader/association.rb', line 180
def target_for(owner)
Array.wrap(owner.association(reflection.name).target)
end
|