aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-03-05 22:07:30 +0000
committerJon Leighton <j@jonathanleighton.com>2011-03-05 22:07:30 +0000
commit7fddb942624478a23173dfa379f7ade6a0fc9218 (patch)
treebca8fd4125105ff26f1e4cafba432760d8897d44 /activerecord
parentddf83d14f1c7ddae07a285a8ad7c45f652edc843 (diff)
downloadrails-7fddb942624478a23173dfa379f7ade6a0fc9218.tar.gz
rails-7fddb942624478a23173dfa379f7ade6a0fc9218.tar.bz2
rails-7fddb942624478a23173dfa379f7ade6a0fc9218.zip
Push source_type and polymorphic conditions out of ThroughAssociation and JoinDependency::JoinAssociation and into the reflection instead.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/join_dependency/join_association.rb18
-rw-r--r--activerecord/lib/active_record/associations/through_association.rb17
-rw-r--r--activerecord/lib/active_record/reflection.rb21
-rw-r--r--activerecord/test/cases/reflection_test.rb4
4 files changed, 13 insertions, 47 deletions
diff --git a/activerecord/lib/active_record/associations/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/join_dependency/join_association.rb
index 890e77fca9..5da3416023 100644
--- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb
+++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb
@@ -76,8 +76,6 @@ module ActiveRecord
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 @@ module ActiveRecord
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 @@ module ActiveRecord
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
diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb
index 8e9259a28d..11263d5def 100644
--- a/activerecord/lib/active_record/associations/through_association.rb
+++ b/activerecord/lib/active_record/associations/through_association.rb
@@ -89,7 +89,6 @@ module ActiveRecord
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 @@ module ActiveRecord
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 @@ module ActiveRecord
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 @@ module ActiveRecord
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
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index 7ae9bfc928..82f648b873 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -270,7 +270,9 @@ module ActiveRecord
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 @@ module ActiveRecord
# 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
diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb
index 9529ae56b8..baaa08a359 100644
--- a/activerecord/test/cases/reflection_test.rb
+++ b/activerecord/test/cases/reflection_test.rb
@@ -216,7 +216,7 @@ class ReflectionTest < ActiveRecord::TestCase
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 @@ class ReflectionTest < ActiveRecord::TestCase
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