Class: ActiveRecord::Associations::Preloader::Association
Do not use. This class is for internal use only.
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Inherits: | Object |
Defined in: | activerecord/lib/active_record/associations/preloader/association.rb |
Class Method Summary
Instance Attribute Summary
- #klass readonly
- #run? ⇒ Boolean readonly
- #key_conversion_required? ⇒ Boolean readonly private
- #model readonly private
- #owners readonly private
- #preload_scope readonly private
- #reflection readonly private
Instance Method Summary
- #associate_records_from_unscoped(unscoped_records)
-
#association_key_name
The name of the key on the associated records.
- #future_classes
- #load_records(raw_records = nil)
- #loaded?(owner) ⇒ Boolean
- #loader_query
- #owners_by_key
- #preloaded_records
- #records_by_owner
- #run readonly
- #runnable_loaders
- #scope
- #set_inverse(record)
- #table_name
- #target_for(owner)
- #associate_records_to_owner(owner, records) private
- #association_key_type private
- #build_scope private
- #cascade_strict_loading(scope) private
- #convert_key(key) private
- #derive_key(owner, key) private
-
#owner_key_name
private
The name of the key on the model which declares the association.
- #owner_key_type private
- #reflection_scope private
Constructor Details
.new(klass, owners, reflection, preload_scope, reflection_scope, associate_by_default) ⇒ Association
# File 'activerecord/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
#key_conversion_required? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 258
def key_conversion_required? unless defined?(@key_conversion_required) @key_conversion_required = (association_key_type != owner_key_type) end @key_conversion_required end
#klass (readonly)
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 102
attr_reader :klass
#model (readonly, private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 238
attr_reader :owners, :reflection, :preload_scope, :model
#owners (readonly, private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 238
attr_reader :owners, :reflection, :preload_scope, :model
#preload_scope (readonly, private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 238
attr_reader :owners, :reflection, :preload_scope, :model
#reflection (readonly, private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 238
attr_reader :owners, :reflection, :preload_scope, :model
#run? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 131
def run? @run end
Instance Method Details
#associate_records_from_unscoped(unscoped_records)
[ GitHub ]# File 'activerecord/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 # Set inverse on first owner association.set_inverse_instance(record) end end end end
#associate_records_to_owner(owner, records) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 245
def associate_records_to_owner(owner, records) return if loaded?(owner) association = owner.association(reflection.name) if reflection.collection? not_persisted_records = association.target.reject(&:persisted?) association.target = records + not_persisted_records else association.target = records.first end end
#association_key_name
The name of the key on the associated records
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 161
def association_key_name reflection.join_primary_key(klass) end
#association_key_type (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 282
def association_key_type @klass.type_for_attribute(association_key_name).type end
#build_scope (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 294
def build_scope scope = klass.scope_for_association if reflection.type && !reflection.through_reflection? scope.where!(reflection.type => model.polymorphic_name) end scope.merge!(reflection_scope) unless reflection_scope.empty_scope? if preload_scope && !preload_scope.empty_scope? scope.merge!(preload_scope) end cascade_strict_loading(scope) end
#cascade_strict_loading(scope) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 310
def cascade_strict_loading(scope) preload_scope&.strict_loading_value ? scope.strict_loading : scope end
#convert_key(key) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 274
def convert_key(key) if key_conversion_required? key.to_s else key end end
#derive_key(owner, key) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 266
def derive_key(owner, key) if key.is_a?(Array) key.map { |k| convert_key(owner._read_attribute(k)) } else convert_key(owner._read_attribute(key)) end end
#future_classes
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 119
def future_classes if run? [] else [@klass] end end
#load_records(raw_records = nil)
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 197
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
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 176
def loaded?(owner) owner.association(reflection.name).loaded? end
#loader_query
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 165
def loader_query LoaderQuery.new(scope, association_key_name) end
#owner_key_name (private)
The name of the key on the model which declares the association
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 241
def owner_key_name reflection.join_foreign_key end
#owner_key_type (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 286
def owner_key_type @model.type_for_attribute(owner_key_name).type end
#owners_by_key
[ GitHub ]# File 'activerecord/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
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 154
def preloaded_records load_records unless defined?(@preloaded_records) @preloaded_records end
#records_by_owner
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 148
def records_by_owner load_records unless defined?(@records_by_owner) @records_by_owner end
#reflection_scope (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 290
def reflection_scope @reflection_scope ||= reflection.join_scopes(klass.arel_table, klass.predicate_builder, klass).inject(klass.unscoped, &:merge!) end
#run (readonly)
[ GitHub ]# File 'activerecord/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
#runnable_loaders
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 127
def runnable_loaders [self] end
#scope
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 184
def scope @scope ||= build_scope end
#set_inverse(record)
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 188
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_name
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 115
def table_name @klass.table_name end
#target_for(owner)
[ GitHub ]# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 180
def target_for(owner) Array.wrap(owner.association(reflection.name).target) end