mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Remove HasManyAssociationStrategy and move the logic to ActiveRecord::Reflection::ThroughReflection.
This commit is contained in:
parent
25ca21ae21
commit
4918e6de98
2 changed files with 21 additions and 27 deletions
|
@ -4,28 +4,6 @@ module ActiveRecord
|
|||
base.extend(ClassMethods)
|
||||
end
|
||||
|
||||
class HasManyAssociationStrategy
|
||||
def initialize(through_reflection)
|
||||
@through_reflection = through_reflection
|
||||
end
|
||||
|
||||
def primary_key
|
||||
if @through_reflection && @through_reflection.macro == :belongs_to
|
||||
@through_reflection.klass.primary_key
|
||||
else
|
||||
@through_reflection.primary_key_name
|
||||
end
|
||||
end
|
||||
|
||||
def primary_key_name
|
||||
if @through_reflection && @through_reflection.macro == :belongs_to
|
||||
@through_reflection.primary_key_name
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
|
||||
# Loads the named associations for the activerecord record (or records) given
|
||||
|
@ -153,9 +131,9 @@ module ActiveRecord
|
|||
|
||||
def preload_has_many_association(records, reflection, preload_options={})
|
||||
options = reflection.options
|
||||
through_reflection = reflections[options[:through]]
|
||||
strat = HasManyAssociationStrategy.new(through_reflection)
|
||||
id_to_record_map, ids = construct_id_map(records, strat.primary_key_name)
|
||||
|
||||
primary_key_name = reflection.through_reflection_primary_key_name
|
||||
id_to_record_map, ids = construct_id_map(records, primary_key_name)
|
||||
records.each {|record| record.send(reflection.name).loaded}
|
||||
|
||||
if options[:through]
|
||||
|
@ -165,7 +143,7 @@ module ActiveRecord
|
|||
source = reflection.source_reflection.name
|
||||
through_records.first.class.preload_associations(through_records, source, options)
|
||||
through_records.each do |through_record|
|
||||
through_record_id = through_record[strat.primary_key].to_s
|
||||
through_record_id = through_record[reflection.through_reflection_primary_key].to_s
|
||||
add_preloaded_records_to_collection(id_to_record_map[through_record_id], reflection.name, through_record.send(source))
|
||||
end
|
||||
end
|
||||
|
|
|
@ -117,6 +117,11 @@ module ActiveRecord
|
|||
@sanitized_conditions ||= klass.send(:sanitize_sql, options[:conditions]) if options[:conditions]
|
||||
end
|
||||
|
||||
# Returns +true+ if +self+ is a +belongs_to+ reflection.
|
||||
def belongs_to?
|
||||
macro == :belongs_to
|
||||
end
|
||||
|
||||
private
|
||||
def derive_class_name
|
||||
name.to_s.camelize
|
||||
|
@ -200,6 +205,9 @@ module ActiveRecord
|
|||
false
|
||||
end
|
||||
|
||||
def through_reflection_primary_key_name
|
||||
end
|
||||
|
||||
def source_reflection
|
||||
nil
|
||||
end
|
||||
|
@ -212,7 +220,7 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def derive_primary_key_name
|
||||
if macro == :belongs_to
|
||||
if belongs_to?
|
||||
"#{name}_id"
|
||||
elsif options[:as]
|
||||
"#{options[:as]}_id"
|
||||
|
@ -281,6 +289,14 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
def through_reflection_primary_key
|
||||
through_reflection.belongs_to? ? through_reflection.klass.primary_key : through_reflection.primary_key_name
|
||||
end
|
||||
|
||||
def through_reflection_primary_key_name
|
||||
through_reflection.primary_key_name if through_reflection.belongs_to?
|
||||
end
|
||||
|
||||
private
|
||||
def derive_class_name
|
||||
# get the class_name of the belongs_to association of the through reflection
|
||||
|
|
Loading…
Reference in a new issue