Skip to content

Commit

Permalink
Fix default_scope to work in combination with named scopes
Browse files Browse the repository at this point in the history
Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
  • Loading branch information
tomstuart authored and dhh committed Nov 17, 2008
1 parent 3a33ee2 commit 32cb234
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 9 additions & 3 deletions activerecord/test/cases/method_scoping_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -532,22 +532,22 @@ 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
end
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
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions activerecord/test/models/developer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 32cb234

Please sign in to comment.