diff options
author | Yaroslav Markin <yaroslav@markin.net> | 2008-12-28 22:25:55 +0300 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2008-12-28 20:52:46 +0100 |
commit | 66ee5890c5f21995b7fe0c486547f1287afe2b55 (patch) | |
tree | 81b44888712ff3ea7093209b3235858948354d25 /activerecord/test/cases | |
parent | 1fb275541a58e6a2100261c6117e96e6c014cc6c (diff) | |
download | rails-66ee5890c5f21995b7fe0c486547f1287afe2b55.tar.gz rails-66ee5890c5f21995b7fe0c486547f1287afe2b55.tar.bz2 rails-66ee5890c5f21995b7fe0c486547f1287afe2b55.zip |
Introduce dynamic scopes for ActiveRecord: you can now use class methods like scoped_by_user_name(user_name) and scoped_by_user_name_and_password(user_name, password) that will use the scoped method with attributes you supply. [#1648 state:committed]
Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/named_scope_test.rb | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 64e899780c..b152f95a15 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -278,3 +278,23 @@ class NamedScopeTest < ActiveRecord::TestCase assert_equal post.comments.size, Post.scoped(:joins => join).scoped(:joins => join, :conditions => "posts.id = #{post.id}").size end end + +class DynamicScopeMatchTest < ActiveRecord::TestCase + def test_scoped_by_no_match + assert_nil ActiveRecord::DynamicScopeMatch.match("not_scoped_at_all") + end + + def test_scoped_by + match = ActiveRecord::DynamicScopeMatch.match("scoped_by_age_and_sex_and_location") + assert_not_nil match + assert match.scope? + assert_equal %w(age sex location), match.attribute_names + end +end + +class DynamicScopeTest < ActiveRecord::TestCase + def test_dynamic_scope + assert_equal Post.scoped_by_author_id(1).find(1), Post.find(1) + assert_equal Post.scoped_by_author_id_and_title(1, "Welcome to the weblog").first, Post.find(:first, :conditions => { :author_id => 1, :title => "Welcome to the weblog"}) + end +end |