Ensure that Associations#include_eager_conditions? checks both scoped and explicit conditions [Rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4232 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Rick Olson
2006-04-19 14:50:10 +00:00
parent 5ea76fab87
commit 2a2afca095
3 changed files with 51 additions and 5 deletions

View File

@@ -1,5 +1,7 @@
*SVN*
* Ensure that Associations#include_eager_conditions? checks both scoped and explicit conditions [Rick]
* Associations#select_limited_ids_list adds the ORDER BY columns to the SELECT DISTINCT List for postgresql. [Rick]
* DRY up association collection reader method generation. [Marcel Molina Jr.]

View File

@@ -1192,16 +1192,24 @@ module ActiveRecord
add_limit!(sql, options, scope)
return sanitize_sql(sql)
end
# Checks if the conditions reference a table other than the current model table
def include_eager_conditions?(options)
conditions = scope(:find, :conditions) || options[:conditions]
return false unless conditions
conditions = conditions.first if conditions.is_a?(Array)
conditions.scan(/(\w+)\.\w+/).flatten.any? do |condition_table_name|
# look in both sets of conditions
conditions = [scope(:find, :conditions), options[:conditions]].inject([]) do |all, cond|
case cond
when nil then all
when Array then all << cond.first
else all << cond
end
end
return false unless conditions.any?
conditions.join(' ').scan(/(\w+)\.\w+/).flatten.any? do |condition_table_name|
condition_table_name != table_name
end
end
# Checks if the query order references a table other than the current model's table.
def include_eager_order?(options)
order = options[:order]
return false unless order

View File

@@ -179,6 +179,42 @@ class EagerAssociationTest < Test::Unit::TestCase
assert_equal count, posts.size
end
def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
posts = nil
Post.with_scope(:find => {
:include => :comments,
:conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'"
}) do
posts = authors(:david).posts.find(:all, :limit => 2)
assert_equal 2, posts.size
end
Post.with_scope(:find => {
:include => [ :comments, :author ],
:conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')"
}) do
count = Post.count(:limit => 2)
assert_equal count, posts.size
end
end
def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers
Post.with_scope(:find => { :conditions => "1=1" }) do
posts = authors(:david).posts.find(:all,
:include => :comments,
:conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
:limit => 2
)
assert_equal 2, posts.size
count = Post.count(
:include => [ :comments, :author ],
:conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
:limit => 2
)
assert_equal count, posts.size
end
end
def test_eager_association_loading_with_habtm
posts = Post.find(:all, :include => :categories, :order => "posts.id")
assert_equal 2, posts[0].categories.size