From dc10fc129c6efe8cf0daa5a2240f1626b9a4541f Mon Sep 17 00:00:00 2001 From: Sergio Gil Date: Sat, 7 Feb 2009 19:27:28 +0100 Subject: [PATCH] Tests to show associations class methods not keeping scope --- activerecord/test/cases/named_scope_test.rb | 24 ++++++++++++++++++++++++ activerecord/test/fixtures/comments.yml | 4 ++-- activerecord/test/models/comment.rb | 4 ++++ activerecord/test/models/post.rb | 4 ++++ activerecord/test/models/topic.rb | 5 +++++ 5 files changed, 39 insertions(+), 2 deletions(-) diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index e1e27fa..f9eb45f 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -89,6 +89,15 @@ class NamedScopeTest < ActiveRecord::TestCase assert_equal approved & replied, Topic.approved.replied end + + def test_scopes_and_class_methods_with_scopes_are_composable + assert_equal (approved = Topic.find(:all, :conditions => {:approved => true})), Topic.approved + assert_equal (replied = Topic.find(:all, :conditions => 'replies_count > 0')), Topic.replied_method + assert !(approved == replied) + assert !(approved & replied).empty? + + assert_equal approved & replied, Topic.approved.replied_method + end def test_procedural_scopes topics_written_before_the_third = Topic.find(:all, :conditions => ['written_on < ?', topics(:third).written_on]) @@ -134,6 +143,13 @@ class NamedScopeTest < ActiveRecord::TestCase assert_equal authors(:david).posts & Post.containing_the_letter_a, authors(:david).posts.containing_the_letter_a end + + def test_has_many_associations_have_access_to_class_methods_with_named_scopes + assert_not_equal Post.containing_the_letter_a_method, authors(:david).posts + assert !Post.containing_the_letter_a_method.empty? + + assert_equal authors(:david).posts & Post.containing_the_letter_a_method, authors(:david).posts.containing_the_letter_a_method + end def test_has_many_through_associations_have_access_to_named_scopes assert_not_equal Comment.containing_the_letter_e, authors(:david).comments @@ -141,6 +157,14 @@ class NamedScopeTest < ActiveRecord::TestCase assert_equal authors(:david).comments & Comment.containing_the_letter_e, authors(:david).comments.containing_the_letter_e end + + def test_has_many_through_associations_have_access_to_class_methods_with_named_scopes + assert_not_equal Comment.containing_the_letter_e_method, authors(:david).comments + assert !Comment.containing_the_letter_e_method.empty? + assert_not_equal authors(:david).comments & Comment.containing_the_letter_e_method, Comment.containing_the_letter_e_method + + assert_equal authors(:david).comments & Comment.containing_the_letter_e_method, authors(:david).comments.containing_the_letter_e_method + end def test_active_records_have_scope_named__all__ assert !Topic.find(:all).empty? diff --git a/activerecord/test/fixtures/comments.yml b/activerecord/test/fixtures/comments.yml index 236bdb2..6eef241 100644 --- a/activerecord/test/fixtures/comments.yml +++ b/activerecord/test/fixtures/comments.yml @@ -55,5 +55,5 @@ check_eager_sti_on_associations2: eager_other_comment1: id: 11 post_id: 7 - body: go crazy - type: SpecialComment + body: went crazy + type: SpecialComment \ No newline at end of file diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb index f7f07c1..de5ecd1 100644 --- a/activerecord/test/models/comment.rb +++ b/activerecord/test/models/comment.rb @@ -2,6 +2,10 @@ class Comment < ActiveRecord::Base named_scope :containing_the_letter_e, :conditions => "comments.body LIKE '%e%'" belongs_to :post, :counter_cache => true + + def self.containing_the_letter_e_method + containing_the_letter_e + end def self.what_are_you 'a comment...' diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 388fff8..d33dea0 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -5,6 +5,10 @@ class Post < ActiveRecord::Base :joins => 'JOIN authors ON authors.id = posts.author_id' } } + + def self.containing_the_letter_a_method + containing_the_letter_a + end belongs_to :author do def greeting diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index 08bb24e..75c4342 100644 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -11,6 +11,11 @@ class Topic < ActiveRecord::Base named_scope :approved_as_hash_condition, :conditions => {:topics => {:approved => true}} named_scope 'approved_as_string', :conditions => {:approved => true} named_scope :replied, :conditions => ['replies_count > 0'] + + def self.replied_method + replied + end + named_scope :anonymous_extension do def one 1 -- 1.6.0.4