0
@@ -522,6 +522,67 @@ class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase
0
+class DefaultScopingTest < ActiveRecord::TestCase
0
+ def test_default_scope
0
+ expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary }
0
+ received = DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary }
0
+ assert_equal expected, received
0
+ def test_default_scoping_with_threads
0
+ scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}]
0
+ Thread.new { assert_equal scope, DeveloperOrderedBySalary.send(:scoped_methods) }.join
0
+ def test_default_scoping_with_inheritance
0
+ scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}]
0
+ # Inherit a class having a default scope and define a new default scope
0
+ klass = Class.new(DeveloperOrderedBySalary)
0
+ klass.send :default_scope, {}
0
+ # Scopes added on children should append to parent scope
0
+ expected_klass_scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}, {:create=>nil, :find=>{}}]
0
+ assert_equal expected_klass_scope, klass.send(:scoped_methods)
0
+ # Parent should still have the original scope
0
+ assert_equal scope, DeveloperOrderedBySalary.send(:scoped_methods)
0
+ expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary }
0
+ received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary }
0
+ assert_equal expected, received
0
+ expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary }
0
+ received = DeveloperOrderedBySalary.with_scope(:find => { :order => 'name DESC'}) do
0
+ DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary }
0
+ assert_equal expected, received
0
+ def test_nested_exclusive_scope
0
+ expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary }
0
+ received = DeveloperOrderedBySalary.with_exclusive_scope(:find => { :limit => 100 }) do
0
+ DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary }
0
+ assert_equal expected, received
0
+ def test_overwriting_default_scope
0
+ expected = Developer.find(:all, :order => 'salary').collect { |dev| dev.salary }
0
+ received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary }
0
+ assert_equal expected, received
0
# We disabled the scoping for has_one and belongs_to as we can't think of a proper use case
Thanks for adding this; it’ll make things easier in many cases (even though I learned to live without it since I played around with default scopes )
Thank you very much for this new feature. It will be very helpful for me.
Awesome, finally it’s in!
Thank you guys, I owe you a beer.