Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix default_scope to work in combination with named scopes

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
  • Loading branch information...
commit 32cb2345a54b9ab38461b2d4bb0dbd1706f2800e 1 parent 3a33ee2
@tomstuart tomstuart authored dhh committed
View
2  activerecord/lib/active_record/base.rb
@@ -2027,7 +2027,7 @@ def subclasses #:nodoc:
# default_scope :find => { :order => 'last_name, first_name' }
# end
def default_scope(options = {})
- self.default_scoping << { :find => options, :create => options.is_a?(Hash) ? options[:conditions] : {} }
+ self.default_scoping << { :find => options, :create => (options.is_a?(Hash) && options.has_key?(:conditions)) ? options[:conditions] : {} }
end
# Test whether the given method and optional key are scoped.
View
12 activerecord/test/cases/method_scoping_test.rb
@@ -532,7 +532,7 @@ def test_default_scope
end
def test_default_scoping_with_threads
- scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}]
+ scope = [{ :create => {}, :find => { :order => 'salary DESC' } }]
2.times do
Thread.new { assert_equal scope, DeveloperOrderedBySalary.send(:scoped_methods) }.join
@@ -540,14 +540,14 @@ def test_default_scoping_with_threads
end
def test_default_scoping_with_inheritance
- scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}]
+ scope = [{ :create => {}, :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=>{}}]
+ expected_klass_scope = [{ :create => {}, :find => { :order => 'salary DESC' }}, { :create => {}, :find => {} }]
assert_equal expected_klass_scope, klass.send(:scoped_methods)
# Parent should still have the original scope
@@ -568,6 +568,12 @@ def test_nested_scope
assert_equal expected, received
end
+ def test_named_scope
+ expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary }
+ received = DeveloperOrderedBySalary.by_name.find(:all).collect { |dev| dev.salary }
+ 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
View
14 activerecord/test/models/developer.rb
@@ -79,13 +79,13 @@ def raise_if_projects_empty!
end
class DeveloperOrderedBySalary < ActiveRecord::Base
- self.table_name = 'developers'
- default_scope :order => "salary DESC"
+ self.table_name = 'developers'
+ default_scope :order => 'salary DESC'
+ named_scope :by_name, :order => 'name DESC'
- def self.all_ordered_by_name
- with_scope(:find => { :order => "name DESC" }) do
- find(:all)
- end
+ def self.all_ordered_by_name
+ with_scope(:find => { :order => 'name DESC' }) do
+ find(:all)
end
-
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.