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

Inherits:
Object
  • Object
show all
Defined in:
activerecord/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.


87
88
89
90
91
92
93
94
95
96
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 87

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.


85
86
87
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 85

def klass
  @klass
end

Instance Method Details

#associate_records_from_unscoped(unscoped_records) ⇒ Object


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 202

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[convert_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


144
145
146
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 144

def association_key_name
  reflection.join_primary_key(klass)
end

#future_classesObject


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

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

#load_records(raw_records = nil) ⇒ Object


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 180

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[convert_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)

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

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

#loader_queryObject


148
149
150
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 148

def loader_query
  LoaderQuery.new(scope, association_key_name)
end

#owners_by_keyObject


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

def owners_by_key
  @owners_by_key ||= owners.each_with_object({}) do |owner, result|
    key = convert_key(owner[owner_key_name])
    (result[key] ||= []) << owner if key
  end
end

#preloaded_recordsObject


137
138
139
140
141
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 137

def preloaded_records
  load_records unless defined?(@preloaded_records)

  @preloaded_records
end

#records_by_ownerObject


131
132
133
134
135
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 131

def records_by_owner
  load_records unless defined?(@records_by_owner)

  @records_by_owner
end

#runObject


118
119
120
121
122
123
124
125
126
127
128
129
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 118

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)

114
115
116
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 114

def run?
  @run
end

#runnable_loadersObject


110
111
112
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 110

def runnable_loaders
  [self]
end

#scopeObject


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

def scope
  @scope ||= build_scope
end

#set_inverse(record) ⇒ Object


171
172
173
174
175
176
177
178
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 171

def set_inverse(record)
  if owners = owners_by_key[convert_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


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

def table_name
  @klass.table_name
end

#target_for(owner) ⇒ Object


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

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