diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2008-11-16 23:36:41 +0530 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-11-16 23:36:41 +0530 |
commit | 2530d0eea8eaecd2c61f99225f050ff47973e9a0 (patch) | |
tree | e32d4920a21417e79584c6c7a563a2cb948b4532 /activerecord/test | |
parent | d9f460a39b73fd2cf0f17f523cc4810d0bf44cac (diff) | |
download | rails-2530d0eea8eaecd2c61f99225f050ff47973e9a0.tar.gz rails-2530d0eea8eaecd2c61f99225f050ff47973e9a0.tar.bz2 rails-2530d0eea8eaecd2c61f99225f050ff47973e9a0.zip |
Added default_scope to Base [#1381 state:committed] (Paweł Kondzior)
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/method_scoping_test.rb | 61 | ||||
-rw-r--r-- | activerecord/test/models/developer.rb | 12 |
2 files changed, 73 insertions, 0 deletions
diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index ff10bfaf3e..4ac0018144 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -522,6 +522,67 @@ class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase end +class DefaultScopingTest < ActiveRecord::TestCase + fixtures :developers + + def test_default_scope + expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + assert_equal expected, received + end + + def test_default_scoping_with_threads + scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}] + + 2.times do + Thread.new { assert_equal scope, DeveloperOrderedBySalary.send(:scoped_methods) }.join + end + end + + def test_default_scoping_with_inheritance + scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}] + + # Inherit a class having a default scope and define a new default scope + klass = Class.new(DeveloperOrderedBySalary) + klass.send :default_scope, {} + + # Scopes added on children should append to parent scope + expected_klass_scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}, {:create=>nil, :find=>{}}] + assert_equal expected_klass_scope, klass.send(:scoped_methods) + + # Parent should still have the original scope + assert_equal scope, DeveloperOrderedBySalary.send(:scoped_methods) + end + + def test_method_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary } + assert_equal expected, received + end + + def test_nested_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.with_scope(:find => { :order => 'name DESC'}) do + DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + end + assert_equal expected, received + end + + def test_nested_exclusive_scope + expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.with_exclusive_scope(:find => { :limit => 100 }) do + DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + end + assert_equal expected, received + end + + def test_overwriting_default_scope + expected = Developer.find(:all, :order => 'salary').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary } + assert_equal expected, received + end +end + =begin # We disabled the scoping for has_one and belongs_to as we can't think of a proper use case diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index c08476f728..0c20f97502 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -77,3 +77,15 @@ class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base raise if projects.empty? end end + +class DeveloperOrderedBySalary < ActiveRecord::Base + self.table_name = 'developers' + default_scope :order => "salary DESC" + + def self.all_ordered_by_name + with_scope(:find => { :order => "name DESC" }) do + find(:all) + end + end + +end |