diff options
author | David Chelimsky + Brian Tatnall <dchelimsky@gmail.com> | 2010-05-14 16:30:12 -0500 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-05-15 08:38:28 +0200 |
commit | 35a114a8941cb22d29a536f1215a23a8cf7c4756 (patch) | |
tree | ab4191bbaa7a87735d9e4fb7cd80ac63bd578c2f /activerecord | |
parent | a0621c1086165e4b3cff71b54f08a190851b6314 (diff) | |
download | rails-35a114a8941cb22d29a536f1215a23a8cf7c4756.tar.gz rails-35a114a8941cb22d29a536f1215a23a8cf7c4756.tar.bz2 rails-35a114a8941cb22d29a536f1215a23a8cf7c4756.zip |
Modified default_scope to merge with any pre-existing default_scope
and added AR::Base::clear_default_scope
- clear_default_scope provides users who rely on the old behaviour
of each call to default_scope overwriting any previous default
scopes an opportunity to maintain that behaviour.
[#4583 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'activerecord')
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/method_scoping_test.rb | 31 |
2 files changed, 34 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 650a91b385..e7319ce8b9 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1190,7 +1190,11 @@ module ActiveRecord #:nodoc: # default_scope order('last_name, first_name') # end def default_scope(options = {}) - self.default_scoping << construct_finder_arel(options) + self.default_scoping << construct_finder_arel(options, default_scoping.pop) + end + + def clear_default_scope + self.default_scoping.clear end def scoped_methods #:nodoc: diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index a3b496a0e6..3a6354ec6d 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -587,6 +587,18 @@ class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase end end +class ClearDefaultScopeTest < ActiveRecord::TestCase + fixtures :developers + + def test_should_clear_default_scope + klass = Class.new(DeveloperCalledDavid) + klass.__send__ :clear_default_scope + expected = Developer.all.collect { |dev| dev.name } + actual = klass.all.collect { |dev| dev.name } + assert_equal expected, actual + end +end + class DefaultScopingTest < ActiveRecord::TestCase fixtures :developers, :posts @@ -615,15 +627,30 @@ class DefaultScopingTest < ActiveRecord::TestCase def test_default_scoping_with_inheritance # Inherit a class having a default scope and define a new default scope klass = Class.new(DeveloperOrderedBySalary) - klass.send :default_scope, {} + klass.send :default_scope, :limit => 1 # Scopes added on children should append to parent scope - assert klass.scoped.order_values.blank? + assert_equal 1, klass.scoped.limit_value + assert_equal ['salary DESC'], klass.scoped.order_values # Parent should still have the original scope + assert_equal nil, DeveloperOrderedBySalary.scoped.limit_value assert_equal ['salary DESC'], DeveloperOrderedBySalary.scoped.order_values end + def test_default_scope_called_twice_merges_conditions + Developer.destroy_all + Developer.create!(:name => "David", :salary => 80000) + Developer.create!(:name => "David", :salary => 100000) + Developer.create!(:name => "Brian", :salary => 100000) + + klass = Class.new(Developer) + klass.__send__ :default_scope, :conditions => { :name => "David" } + klass.__send__ :default_scope, :conditions => { :salary => 100000 } + assert_equal 1, klass.count + assert_equal "David", klass.first.name + assert_equal 100000, klass.first.salary + 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 } |