mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
extract the string handling part to a method.
We know the structure passed in to the `construct` method will be a hash, so we don't need to test it all the time. The key value will be a symbol or string, so handle it with the special method
This commit is contained in:
parent
77e5c1262c
commit
d2d6e4a980
1 changed files with 17 additions and 17 deletions
|
@ -182,26 +182,26 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def construct(parent, associations, join_parts, row)
|
||||
case associations
|
||||
when Symbol, String
|
||||
name = associations.to_s
|
||||
|
||||
join_part = join_parts.detect { |j|
|
||||
j.reflection.name.to_s == name &&
|
||||
j.parent_table_name == parent.class.table_name }
|
||||
|
||||
raise(ConfigurationError, "No such association") unless join_part
|
||||
|
||||
join_parts.delete(join_part)
|
||||
construct_association(parent, join_part, row)
|
||||
when Hash
|
||||
associations.sort_by { |k,_| k.to_s }.each do |association_name, assoc|
|
||||
association = construct(parent, association_name, join_parts, row)
|
||||
construct(association, assoc, join_parts, row) if association
|
||||
end
|
||||
associations.sort_by { |k,_| k.to_s }.each do |association_name, assoc|
|
||||
association = construct_scalar(parent, association_name, join_parts, row)
|
||||
construct(association, assoc, join_parts, row) if association
|
||||
end
|
||||
end
|
||||
|
||||
def construct_scalar(parent, associations, join_parts, row)
|
||||
name = associations.to_s
|
||||
|
||||
join_part = join_parts.detect { |j|
|
||||
j.reflection.name.to_s == name &&
|
||||
j.parent_table_name == parent.class.table_name
|
||||
}
|
||||
|
||||
raise(ConfigurationError, "No such association") unless join_part
|
||||
|
||||
join_parts.delete(join_part)
|
||||
construct_association(parent, join_part, row)
|
||||
end
|
||||
|
||||
def construct_association(record, join_part, row)
|
||||
return if record.id.to_s != join_part.parent.record_id(row).to_s
|
||||
|
||||
|
|
Loading…
Reference in a new issue