From aac65baab4ee3c4d1d7fed974421b4dd7744cfbd Mon Sep 17 00:00:00 2001 From: Bruno Michel Date: Sun, 29 Mar 2009 20:27:07 +0200 Subject: [PATCH 1/3] Fix default_scope with a condition for new objects --- activerecord/lib/active_record/base.rb | 2 +- activerecord/test/cases/method_scoping_test.rb | 18 ++++++++++++++++++ activerecord/test/models/developer.rb | 10 ++++++++++ 3 files changed, 29 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 2a53851..f5e809c 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2168,7 +2168,7 @@ module ActiveRecord #:nodoc: # default_scope :order => 'last_name, first_name' # end def default_scope(options = {}) - self.default_scoping << { :find => options, :create => (options.is_a?(Hash) && options.has_key?(:conditions)) ? options[:conditions] : {} } + self.default_scoping << { :find => options, :create => (options.is_a?(Hash) && options.has_key?(:conditions) && options[:conditions].is_a?(Hash)) ? options[:conditions] : {} } end # Test whether the given method and optional key are scoped. diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index 3c34cde..40d8eb1 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -647,6 +647,24 @@ class DefaultScopingTest < ActiveRecord::TestCase received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary } assert_equal expected, received end + + def test_default_scope_for_new_objects + received = DeveloperNamedDavid.new.name + assert_equal 'David', received + assert_nil DeveloperNamedJamis.new.name + end + + def test_default_scope_with_hash_conditions + expected = Developer.all(:conditions => {:name => 'David'}).collect { |dev| dev.salary } + received = DeveloperNamedDavid.all.collect { |dev| dev.salary } + assert_equal expected, received + end + + def test_default_scope_with_string_conditions + expected = Developer.all(:conditions => "name LIKE '%Jamis%'").collect { |dev| dev.salary } + received = DeveloperNamedJamis.all.collect { |dev| dev.salary } + assert_equal expected, received + end end =begin diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index 92039a4..a4bec83 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -89,3 +89,13 @@ class DeveloperOrderedBySalary < ActiveRecord::Base end end end + +class DeveloperNamedDavid < ActiveRecord::Base + self.table_name = 'developers' + default_scope :conditions => {:name => 'David'} +end + +class DeveloperNamedJamis < ActiveRecord::Base + self.table_name = 'developers' + default_scope :conditions => "name LIKE '%Jamis%'" +end -- 1.6.2.1 From 3102515fca058ca57ae5c878c6e4ce50ec9818e3 Mon Sep 17 00:00:00 2001 From: Bruno Michel Date: Mon, 30 Mar 2009 23:52:06 +0200 Subject: [PATCH 2/3] A new test case for default_scope --- activerecord/test/cases/method_scoping_test.rb | 6 ++++++ activerecord/test/models/developer.rb | 1 + 2 files changed, 7 insertions(+), 0 deletions(-) diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index 40d8eb1..09b8fb3 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -665,6 +665,12 @@ class DefaultScopingTest < ActiveRecord::TestCase received = DeveloperNamedJamis.all.collect { |dev| dev.salary } assert_equal expected, received end + + def test_merging_default_scope_with_named_scope_conditions + expected = Developer.all(:conditions => {:name => 'Jamis'}).collect { |dev| dev.salary } + received = DeveloperNamedJamis.jamises.collect { |dev| dev.salary } + assert_equal expected, received + end end =begin diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index a4bec83..eb8099d 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -98,4 +98,5 @@ end class DeveloperNamedJamis < ActiveRecord::Base self.table_name = 'developers' default_scope :conditions => "name LIKE '%Jamis%'" + named_scope :jamises, :conditions => {:name => 'Jamis'} end -- 1.6.2.1 From c2ebcf5000b048e56f8b4f2087f22551de159d58 Mon Sep 17 00:00:00 2001 From: Nick Muerdter Date: Tue, 7 Apr 2009 16:04:08 -0600 Subject: [PATCH 3/3] New test case for default_scope with associations. --- activerecord/test/cases/method_scoping_test.rb | 8 +++++++- activerecord/test/models/author.rb | 1 + activerecord/test/models/post.rb | 5 +++++ 3 files changed, 13 insertions(+), 1 deletions(-) diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index 09b8fb3..3fe5d22 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -583,7 +583,7 @@ class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase end class DefaultScopingTest < ActiveRecord::TestCase - fixtures :developers + fixtures :developers, :authors, :posts def test_default_scope expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary } @@ -666,6 +666,12 @@ class DefaultScopingTest < ActiveRecord::TestCase assert_equal expected, received end + def test_default_scope_associations_with_string_conditions + expected = Author.find_by_id(authors(:david)).hello_posts.collect { |post| post.id } + received = Author.find_by_id(authors(:david)).default_scope_hello_posts.collect { |post| post.id } + assert_equal expected, received + end + def test_merging_default_scope_with_named_scope_conditions expected = Developer.all(:conditions => {:name => 'Jamis'}).collect { |dev| dev.salary } received = DeveloperNamedJamis.jamises.collect { |dev| dev.salary } diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index 4ffac4f..36c352c 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -44,6 +44,7 @@ class Author < ActiveRecord::Base has_many :nonexistant_comments, :through => :posts has_many :hello_posts, :class_name => "Post", :conditions => "posts.body = 'hello'" + has_many :default_scope_hello_posts, :primary_key => "id" has_many :hello_post_comments, :through => :hello_posts, :source => :comments has_many :posts_with_no_comments, :class_name => 'Post', :conditions => 'comments.id is null', :include => :comments diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 374e536..2a40d37 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -98,3 +98,8 @@ end class SubStiPost < StiPost self.table_name = Post.table_name end + +class DefaultScopeHelloPost < ActiveRecord::Base + self.table_name = Post.table_name + default_scope :conditions => "posts.body = 'hello'" +end -- 1.6.2.1