Class: ActiveRecord::Associations::Preloader::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/associations/preloader/association.rb

Overview

:nodoc:

Direct Known Subclasses

ThroughAssociation

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.



101
102
103
104
105
106
107
108
109
110
# File 'lib/active_record/associations/preloader/association.rb', line 101

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

#klassObject (readonly)

Returns the value of attribute klass.



99
100
101
# File 'lib/active_record/associations/preloader/association.rb', line 99

def klass
  @klass
end

Instance Method Details

#associate_records_from_unscoped(unscoped_records) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/active_record/associations/preloader/association.rb', line 215

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 # Set inverse on first owner
        association.set_inverse_instance(record)
      end
    end
  end
end

#association_key_nameObject

The name of the key on the associated records



158
159
160
# File 'lib/active_record/associations/preloader/association.rb', line 158

def association_key_name
  reflection.join_primary_key(klass)
end

#future_classesObject



116
117
118
119
120
121
122
# File 'lib/active_record/associations/preloader/association.rb', line 116

def future_classes
  if run?
    []
  else
    [@klass]
  end
end

#load_records(raw_records = nil) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/active_record/associations/preloader/association.rb', line 194

def load_records(raw_records = nil)
  # owners can be duplicated when a relation has a collection association join
  # #compare_by_identity makes such owners different hash keys
  @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

Returns:

  • (Boolean)


173
174
175
# File 'lib/active_record/associations/preloader/association.rb', line 173

def loaded?(owner)
  owner.association(reflection.name).loaded?
end

#loader_queryObject



162
163
164
# File 'lib/active_record/associations/preloader/association.rb', line 162

def loader_query
  LoaderQuery.new(scope, association_key_name)
end

#owners_by_keyObject



166
167
168
169
170
171
# File 'lib/active_record/associations/preloader/association.rb', line 166

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_recordsObject



151
152
153
154
155
# File 'lib/active_record/associations/preloader/association.rb', line 151

def preloaded_records
  load_records unless defined?(@preloaded_records)

  @preloaded_records
end

#records_by_ownerObject



145
146
147
148
149
# File 'lib/active_record/associations/preloader/association.rb', line 145

def records_by_owner
  load_records unless defined?(@records_by_owner)

  @records_by_owner
end

#runObject



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/active_record/associations/preloader/association.rb', line 132

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

Returns:

  • (Boolean)


128
129
130
# File 'lib/active_record/associations/preloader/association.rb', line 128

def run?
  @run
end

#runnable_loadersObject



124
125
126
# File 'lib/active_record/associations/preloader/association.rb', line 124

def runnable_loaders
  [self]
end

#scopeObject



181
182
183
# File 'lib/active_record/associations/preloader/association.rb', line 181

def scope
  @scope ||= build_scope
end

#set_inverse(record) ⇒ Object



185
186
187
188
189
190
191
192
# File 'lib/active_record/associations/preloader/association.rb', line 185

def set_inverse(record)
  if owners = owners_by_key[derive_key(record, association_key_name)]
    # Processing only the first owner
    # because the record is modified but not an owner
    association = owners.first.association(reflection.name)
    association.set_inverse_instance(record)
  end
end

#table_nameObject



112
113
114
# File 'lib/active_record/associations/preloader/association.rb', line 112

def table_name
  @klass.table_name
end

#target_for(owner) ⇒ Object



177
178
179
# File 'lib/active_record/associations/preloader/association.rb', line 177

def target_for(owner)
  Array.wrap(owner.association(reflection.name).target)
end