aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-07-22 20:05:42 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-07-22 20:05:42 +0000
commit34f9d30e399050e776263441ee1d4415d0b2c254 (patch)
tree104acd17ac26f01436de6149e374ed9b43205e06 /activerecord/test
parentea8f3f0a3765883c993cdd1c28ae958f097d2632 (diff)
downloadrails-34f9d30e399050e776263441ee1d4415d0b2c254.tar.gz
rails-34f9d30e399050e776263441ee1d4415d0b2c254.tar.bz2
rails-34f9d30e399050e776263441ee1d4415d0b2c254.zip
Added support for calling constrained class methods on has_many and has_and_belongs_to_many collections #1764 [Tobias Luetke]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1894 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/conditions_scoping_test.rb137
-rw-r--r--activerecord/test/fixtures/category.rb12
-rw-r--r--activerecord/test/fixtures/comment.rb24
-rw-r--r--activerecord/test/fixtures/post.rb4
4 files changed, 174 insertions, 3 deletions
diff --git a/activerecord/test/conditions_scoping_test.rb b/activerecord/test/conditions_scoping_test.rb
new file mode 100644
index 0000000000..8ff708658c
--- /dev/null
+++ b/activerecord/test/conditions_scoping_test.rb
@@ -0,0 +1,137 @@
+require 'abstract_unit'
+require 'fixtures/developer'
+require 'fixtures/comment'
+require 'fixtures/post'
+require 'fixtures/category'
+
+class ConditionsScopingTest < Test::Unit::TestCase
+ fixtures :developers
+
+ def test_set_conditions
+ Developer.constrain(:conditions => 'just a test...') do
+ assert_equal 'just a test...', Thread.current[:constrains][Developer][:conditions]
+ end
+ end
+
+ def test_scoped_find
+ Developer.constrain(:conditions => "name = 'David'") do
+ assert_nothing_raised { Developer.find(1) }
+ end
+ end
+
+ def test_scoped_find_first
+ Developer.constrain(:conditions => "salary = 100000") do
+ assert_equal Developer.find(10), Developer.find(:first, :order => 'name')
+ end
+ end
+
+ def test_scoped_find_all
+ Developer.constrain(:conditions => "name = 'David'") do
+ assert_equal [Developer.find(1)], Developer.find(:all)
+ assert_equal [Developer.find(1)], Developer.find(:all, :condtions => '1 = 2')
+ end
+ end
+
+ def test_scoped_count
+ Developer.constrain(:conditions => "name = 'David'") do
+ assert_equal 1, Developer.count
+ end
+
+ Developer.constrain(:conditions => 'salary = 100000') do
+ assert_equal 8, Developer.count
+ assert_equal 1, Developer.count("name LIKE 'fixture_1%'")
+ end
+ end
+end
+
+class HasManyScopingTest< Test::Unit::TestCase
+ fixtures :comments, :posts
+
+ def setup
+ @welcome = Post.find(1)
+ end
+
+ def test_forwarding_of_static_methods
+ assert_equal 'a comment...', Comment.what_are_you
+ assert_equal 'a comment...', @welcome.comments.what_are_you
+ end
+
+ def test_forwarding_to_scoped
+ assert_equal 4, Comment.search_by_type('Comment').size
+ assert_equal 2, @welcome.comments.search_by_type('Comment').size
+ end
+
+ def test_forwarding_to_dynamic_finders
+ assert_equal 4, Comment.find_all_by_type('Comment').size
+ assert_equal 2, @welcome.comments.find_all_by_type('Comment').size
+ end
+
+end
+
+
+class HasAndBelongsToManyScopingTest< Test::Unit::TestCase
+ fixtures :posts, :categories
+
+ def setup
+ @welcome = Post.find(1)
+ end
+
+ def test_forwarding_of_static_methods
+ assert_equal 'a category...', Category.what_are_you
+ assert_equal 'a category...', @welcome.categories.what_are_you
+ end
+
+ def test_forwarding_to_dynamic_finders
+ assert_equal 1, Category.find_all_by_type('SpecialCategory').size
+ assert_equal 0, @welcome.categories.find_all_by_type('SpecialCategory').size
+ assert_equal 2, @welcome.categories.find_all_by_type('Category').size
+ end
+
+end
+
+
+=begin
+# We disabled the scoping for has_one and belongs_to as we can't think of a proper use case
+
+
+class BelongsToScopingTest< Test::Unit::TestCase
+ fixtures :comments, :posts
+
+ def setup
+ @greetings = Comment.find(1)
+ end
+
+ def test_forwarding_of_static_method
+ assert_equal 'a post...', Post.what_are_you
+ assert_equal 'a post...', @greetings.post.what_are_you
+ end
+
+ def test_forwarding_to_dynamic_finders
+ assert_equal 4, Post.find_all_by_type('Post').size
+ assert_equal 1, @greetings.post.find_all_by_type('Post').size
+ end
+
+end
+
+
+class HasOneScopingTest< Test::Unit::TestCase
+ fixtures :comments, :posts
+
+ def setup
+ @sti_comments = Post.find(4)
+ end
+
+ def test_forwarding_of_static_methods
+ assert_equal 'a comment...', Comment.what_are_you
+ assert_equal 'a very special comment...', @sti_comments.very_special_comment.what_are_you
+ end
+
+ def test_forwarding_to_dynamic_finders
+ assert_equal 1, Comment.find_all_by_type('VerySpecialComment').size
+ assert_equal 1, @sti_comments.very_special_comment.find_all_by_type('VerySpecialComment').size
+ assert_equal 0, @sti_comments.very_special_comment.find_all_by_type('Comment').size
+ end
+
+end
+
+=end \ No newline at end of file
diff --git a/activerecord/test/fixtures/category.rb b/activerecord/test/fixtures/category.rb
index 822defa03e..880eb1573d 100644
--- a/activerecord/test/fixtures/category.rb
+++ b/activerecord/test/fixtures/category.rb
@@ -1,5 +1,15 @@
class Category < ActiveRecord::Base
has_and_belongs_to_many :posts
+
+ def self.what_are_you
+ 'a category...'
+ end
end
-class SpecialCategory < Category; end;
+class SpecialCategory < Category
+
+ def self.what_are_you
+ 'a special category...'
+ end
+
+end
diff --git a/activerecord/test/fixtures/comment.rb b/activerecord/test/fixtures/comment.rb
index 982cbc6a7a..0605fd7046 100644
--- a/activerecord/test/fixtures/comment.rb
+++ b/activerecord/test/fixtures/comment.rb
@@ -1,7 +1,27 @@
class Comment < ActiveRecord::Base
belongs_to :post
+
+ def self.what_are_you
+ 'a comment...'
+ end
+
+ def self.search_by_type(q)
+ self.find(:all, :conditions => ['type = ?', q])
+ end
end
-class SpecialComment < Comment; end;
+class SpecialComment < Comment;
-class VerySpecialComment < Comment; end;
+ def self.what_are_you
+ 'a special comment...'
+ end
+
+end;
+
+class VerySpecialComment < Comment;
+
+ def self.what_are_you
+ 'a very special comment...'
+ end
+
+end;
diff --git a/activerecord/test/fixtures/post.rb b/activerecord/test/fixtures/post.rb
index e347d94fb8..f5adac41dc 100644
--- a/activerecord/test/fixtures/post.rb
+++ b/activerecord/test/fixtures/post.rb
@@ -5,6 +5,10 @@ class Post < ActiveRecord::Base
has_many :special_comments, :class_name => "SpecialComment"
has_and_belongs_to_many :categories
has_and_belongs_to_many :special_categories, :join_table => "categories_posts"
+
+ def self.what_are_you
+ 'a post...'
+ end
end
class SpecialPost < Post; end;