提交 7fddb942 编写于 作者: J Jon Leighton

Push source_type and polymorphic conditions out of ThroughAssociation and...

Push source_type and polymorphic conditions out of ThroughAssociation and JoinDependency::JoinAssociation and into the reflection instead.
上级 ddf83d14
......@@ -76,8 +76,6 @@ def join_to(relation)
when :has_many, :has_one
key = reflection.foreign_key
foreign_key = reflection.active_record_primary_key
conditions << polymorphic_conditions(reflection, table)
when :has_and_belongs_to_many
# For habtm, we need to deal with the join table at the same time as the
# target table (because unlike a :through association, there is no reflection
......@@ -103,8 +101,6 @@ def join_to(relation)
when :belongs_to
key = reflection.association_primary_key
foreign_key = reflection.foreign_key
conditions << source_type_conditions(reflection, foreign_table)
when :has_many, :has_one
key = reflection.foreign_key
foreign_key = reflection.source_reflection.active_record_primary_key
......@@ -239,20 +235,6 @@ def sti_conditions(reflection, table)
end
end
def source_type_conditions(reflection, foreign_table)
if reflection.options[:source_type]
foreign_table[reflection.source_reflection.foreign_type].
eq(reflection.options[:source_type])
end
end
def polymorphic_conditions(reflection, table)
if reflection.options[:as]
table[reflection.type].
eq(reflection.active_record.base_class.name)
end
end
end
end
end
......
......@@ -89,7 +89,6 @@ def construct_joins
right_table,
left_table[left.foreign_key],
right_table[right.association_primary_key],
polymorphic_conditions(left, left),
reflection_conditions(right_index)
)
when :has_and_belongs_to_many
......@@ -107,7 +106,6 @@ def construct_joins
right_table,
left_table[left.association_primary_key],
right_table[left.foreign_key],
source_type_conditions(left),
reflection_conditions(right_index)
)
when :has_many, :has_one
......@@ -119,7 +117,6 @@ def construct_joins
right_table,
left_table[left.foreign_key],
right_table[left.source_reflection.active_record_primary_key],
polymorphic_conditions(left, left.source_reflection),
reflection_conditions(right_index)
)
......@@ -254,20 +251,6 @@ def process_conditions(conditions, reflection)
end
end
def polymorphic_conditions(reflection, polymorphic_reflection)
if polymorphic_reflection.options[:as]
tables[reflection][polymorphic_reflection.type].
eq(polymorphic_reflection.active_record.base_class.name)
end
end
def source_type_conditions(reflection)
if reflection.options[:source_type]
tables[reflection.through_reflection][reflection.foreign_type].
eq(reflection.options[:source_type])
end
end
# TODO: Think about this in the context of nested associations
def stale_state
if through_reflection.macro == :belongs_to
......
......@@ -270,7 +270,9 @@ def through_reflection_chain
end
def through_conditions
[Array.wrap(options[:conditions])]
through_conditions = [Array.wrap(options[:conditions])]
through_conditions.first << { type => active_record.base_class.name } if options[:as]
through_conditions
end
def source_reflection
......@@ -453,20 +455,19 @@ def through_reflection_chain
# itself an array of conditions from an arbitrary number of relevant reflections.
def through_conditions
@through_conditions ||= begin
# Initialize the first item - which corresponds to this reflection - either by recursing
# into the souce reflection (if it is itself a through reflection), or by grabbing the
# source reflection conditions.
if source_reflection.source_reflection
conditions = source_reflection.through_conditions
else
conditions = [Array.wrap(source_reflection.options[:conditions])]
end
conditions = source_reflection.through_conditions
# Add to it the conditions from this reflection if necessary.
conditions.first << options[:conditions] if options[:conditions]
through_conditions = through_reflection.through_conditions
if options[:source_type]
through_conditions.first << { foreign_type => options[:source_type] }
end
# Recursively fill out the rest of the array from the through reflection
conditions += through_reflection.through_conditions
conditions += through_conditions
# And return
conditions
......
......@@ -216,7 +216,7 @@ def test_through_reflection_chain
def test_through_conditions
expected = [
["tags.name = 'Blue'"],
["taggings.comment = 'first'"],
["taggings.comment = 'first'", {"taggable_type"=>"Post"}],
["posts.title LIKE 'misc post%'"]
]
actual = Author.reflect_on_association(:misc_post_first_blue_tags).through_conditions
......@@ -224,7 +224,7 @@ def test_through_conditions
expected = [
["tags.name = 'Blue'", "taggings.comment = 'first'", "posts.title LIKE 'misc post%'"],
[],
[{"taggable_type"=>"Post"}],
[]
]
actual = Author.reflect_on_association(:misc_post_first_blue_tags_2).through_conditions
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册