提交 e5203391 编写于 作者: M Matthew Draper

Merge pull request #15630 from eileencodes/refactor-join-keys-on-add_constraints

begin refactoring add_constraints by moving join keys
......@@ -105,18 +105,9 @@ def add_constraints(scope, owner, assoc_klass, refl, tracker)
chain.each_with_index do |reflection, i|
table, foreign_table = tables.shift, tables.first
if reflection.source_macro == :belongs_to
if reflection.polymorphic?
key = reflection.association_primary_key(assoc_klass)
else
key = reflection.association_primary_key
end
foreign_key = reflection.foreign_key
else
key = reflection.foreign_key
foreign_key = reflection.active_record_primary_key
end
join_keys = reflection.join_keys(assoc_klass)
key = join_keys.key
foreign_key = join_keys.foreign_key
if reflection == chain.last
bind_val = bind scope, table.table_name, key.to_s, owner[foreign_key], tracker
......
......@@ -188,6 +188,23 @@ def ==(other_aggregation)
active_record == other_aggregation.active_record
end
JoinKeys = Struct.new(:key, :foreign_key) # :nodoc:
def join_keys(assoc_klass)
if source_macro == :belongs_to
if polymorphic?
reflection_key = association_primary_key(assoc_klass)
else
reflection_key = association_primary_key
end
reflection_foreign_key = foreign_key
else
reflection_key = foreign_key
reflection_foreign_key = active_record_primary_key
end
JoinKeys.new(reflection_key, reflection_foreign_key)
end
private
def derive_class_name
name.to_s.camelize
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册