public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Fix default_scope to work in combination with named scopes

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
Tom Stuart (author)
Mon Nov 17 12:10:59 -0800 2008
dhh (committer)
Mon Nov 17 13:00:14 -0800 2008
commit  32cb2345a54b9ab38461b2d4bb0dbd1706f2800e
tree    9da1d7efd04f2f589c777699e2d3c6274820d4be
parent  3a33ee28e9babc5a1a78079f5ca50ea6249f2643
...
2027
2028
2029
2030
 
2031
2032
2033
...
2027
2028
2029
 
2030
2031
2032
2033
0
@@ -2027,7 +2027,7 @@ module ActiveRecord #:nodoc:
0
         #     default_scope :find => { :order => 'last_name, first_name' }
0
         #   end
0
         def default_scope(options = {})
0
-          self.default_scoping << { :find => options, :create => options.is_a?(Hash) ?  options[:conditions] : {} }
0
+          self.default_scoping << { :find => options, :create => (options.is_a?(Hash) && options.has_key?(:conditions)) ? options[:conditions] : {} }
0
         end
0
 
0
         # Test whether the given method and optional key are scoped.
...
532
533
534
535
 
536
537
538
...
540
541
542
543
 
544
545
546
547
548
549
550
 
551
552
553
...
568
569
570
 
 
 
 
 
 
571
572
573
...
532
533
534
 
535
536
537
538
...
540
541
542
 
543
544
545
546
547
548
549
 
550
551
552
553
...
568
569
570
571
572
573
574
575
576
577
578
579
0
@@ -532,7 +532,7 @@ class DefaultScopingTest < ActiveRecord::TestCase
0
   end
0
 
0
   def test_default_scoping_with_threads
0
-    scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}]
0
+    scope = [{ :create => {}, :find => { :order => 'salary DESC' } }]
0
 
0
     2.times do
0
       Thread.new { assert_equal scope, DeveloperOrderedBySalary.send(:scoped_methods) }.join
0
@@ -540,14 +540,14 @@ class DefaultScopingTest < ActiveRecord::TestCase
0
   end
0
 
0
   def test_default_scoping_with_inheritance
0
-    scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}]
0
+    scope = [{ :create => {}, :find => { :order => 'salary DESC' } }]
0
 
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
 
0
     # Scopes added on children should append to parent scope
0
-    expected_klass_scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}, {:create=>nil, :find=>{}}]
0
+    expected_klass_scope = [{ :create => {}, :find => { :order => 'salary DESC' }}, { :create => {}, :find => {} }]
0
     assert_equal expected_klass_scope, klass.send(:scoped_methods)
0
     
0
     # Parent should still have the original scope
0
@@ -568,6 +568,12 @@ class DefaultScopingTest < ActiveRecord::TestCase
0
     assert_equal expected, received
0
   end
0
 
0
+  def test_named_scope
0
+    expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary }
0
+    received = DeveloperOrderedBySalary.by_name.find(:all).collect { |dev| dev.salary }
0
+    assert_equal expected, received
0
+  end
0
+
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
...
79
80
81
82
83
 
 
 
84
85
86
87
88
 
 
 
89
90
 
91
...
79
80
81
 
 
82
83
84
85
 
 
 
 
86
87
88
89
 
90
91
0
@@ -79,13 +79,13 @@ class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base
0
 end
0
 
0
 class DeveloperOrderedBySalary < ActiveRecord::Base
0
-    self.table_name = 'developers'
0
-    default_scope :order => "salary DESC"
0
+  self.table_name = 'developers'
0
+  default_scope :order => 'salary DESC'
0
+  named_scope :by_name, :order => 'name DESC'
0
 
0
-    def self.all_ordered_by_name
0
-      with_scope(:find => { :order => "name DESC" }) do
0
-        find(:all)
0
-      end
0
+  def self.all_ordered_by_name
0
+    with_scope(:find => { :order => 'name DESC' }) do
0
+      find(:all)
0
     end
0
-
0
+  end
0
 end

Comments