From 445cb5c08d4eea7bd035e86d1f38ae258c635e88 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Mon, 27 Mar 2006 06:19:31 +0000 Subject: Add support for :include to with_scope [andrew@redlinesoftware.com] Remove overrated warning from adapter_test git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4064 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/adapter_test.rb | 2 - activerecord/test/method_scoping_test.rb | 65 +++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 4 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/adapter_test.rb b/activerecord/test/adapter_test.rb index 3da1bf3e54..772dd0e5a8 100644 --- a/activerecord/test/adapter_test.rb +++ b/activerecord/test/adapter_test.rb @@ -43,8 +43,6 @@ class AdapterTest < Test::Unit::TestCase def test_current_database if @connection.respond_to?(:current_database) assert_equal "activerecord_unittest", @connection.current_database - else - warn "#{@connection.class} does not respond to #current_database" end end diff --git a/activerecord/test/method_scoping_test.rb b/activerecord/test/method_scoping_test.rb index c01aab6a36..bceb3869a6 100644 --- a/activerecord/test/method_scoping_test.rb +++ b/activerecord/test/method_scoping_test.rb @@ -1,11 +1,12 @@ require 'abstract_unit' require 'fixtures/developer' +require 'fixtures/project' require 'fixtures/comment' require 'fixtures/post' require 'fixtures/category' class MethodScopingTest < Test::Unit::TestCase - fixtures :developers, :comments, :posts + fixtures :developers, :projects, :comments, :posts def test_set_conditions Developer.with_scope(:find => { :conditions => 'just a test...' }) do @@ -60,6 +61,23 @@ class MethodScopingTest < Test::Unit::TestCase end end + def test_scoped_find_include + # with the include, will retrieve only developers for the given project + scoped_developers = Developer.with_scope(:find => { :include => :projects }) do + Developer.find(:all, :conditions => 'projects.id = 2') + end + assert scoped_developers.include?(developers(:david)) + assert !scoped_developers.include?(developers(:jamis)) + assert_equal 1, scoped_developers.size + end + + def test_scoped_count_include + # with the include, will retrieve only developers for the given project + Developer.with_scope(:find => { :include => :projects }) do + assert_equal 1, Developer.count('projects.id = 2') + end + end + def test_scoped_create new_comment = nil @@ -108,7 +126,7 @@ class MethodScopingTest < Test::Unit::TestCase end class NestedScopingTest < Test::Unit::TestCase - fixtures :developers, :comments, :posts + fixtures :developers, :projects, :comments, :posts def test_merge_options Developer.with_scope(:find => { :conditions => 'salary = 80000' }) do @@ -160,6 +178,49 @@ class NestedScopingTest < Test::Unit::TestCase end end + def test_nested_scoped_find_include + Developer.with_scope(:find => { :include => :projects }) do + Developer.with_scope(:find => { :conditions => "projects.id = 2" }) do + assert_nothing_raised { Developer.find(1) } + assert_equal('David', Developer.find(:first).name) + end + end + end + + def test_nested_scoped_find_merged_include + # :include's remain unique and don't "double up" when merging + Developer.with_scope(:find => { :include => :projects, :conditions => "projects.id = 2" }) do + Developer.with_scope(:find => { :include => :projects }) do + assert_equal 1, Developer.instance_eval('current_scoped_methods')[:find][:include].length + assert_equal('David', Developer.find(:first).name) + end + end + + # the nested scope doesn't remove the first :include + Developer.with_scope(:find => { :include => :projects, :conditions => "projects.id = 2" }) do + Developer.with_scope(:find => { :include => [] }) do + assert_equal 1, Developer.instance_eval('current_scoped_methods')[:find][:include].length + assert_equal('David', Developer.find(:first).name) + end + end + + # mixing array and symbol include's will merge correctly + Developer.with_scope(:find => { :include => [:projects], :conditions => "projects.id = 2" }) do + Developer.with_scope(:find => { :include => :projects }) do + assert_equal 1, Developer.instance_eval('current_scoped_methods')[:find][:include].length + assert_equal('David', Developer.find(:first).name) + end + end + end + + def test_nested_scoped_find_replace_include + Developer.with_scope(:find => { :include => :projects }) do + Developer.with_exclusive_scope(:find => { :include => [] }) do + assert_equal 0, Developer.instance_eval('current_scoped_methods')[:find][:include].length + end + end + end + def test_three_level_nested_exclusive_scoped_find Developer.with_scope(:find => { :conditions => "name = 'Jamis'" }) do assert_equal('Jamis', Developer.find(:first).name) -- cgit v1.2.3