mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix default_scope to work in combination with named scopes
Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
This commit is contained in:
parent
3a33ee28e9
commit
32cb2345a5
3 changed files with 17 additions and 11 deletions
|
@ -2027,7 +2027,7 @@ module ActiveRecord #:nodoc:
|
||||||
# default_scope :find => { :order => 'last_name, first_name' }
|
# default_scope :find => { :order => 'last_name, first_name' }
|
||||||
# end
|
# end
|
||||||
def default_scope(options = {})
|
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
|
end
|
||||||
|
|
||||||
# Test whether the given method and optional key are scoped.
|
# Test whether the given method and optional key are scoped.
|
||||||
|
|
|
@ -532,7 +532,7 @@ class DefaultScopingTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_default_scoping_with_threads
|
def test_default_scoping_with_threads
|
||||||
scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}]
|
scope = [{ :create => {}, :find => { :order => 'salary DESC' } }]
|
||||||
|
|
||||||
2.times do
|
2.times do
|
||||||
Thread.new { assert_equal scope, DeveloperOrderedBySalary.send(:scoped_methods) }.join
|
Thread.new { assert_equal scope, DeveloperOrderedBySalary.send(:scoped_methods) }.join
|
||||||
|
@ -540,14 +540,14 @@ class DefaultScopingTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_default_scoping_with_inheritance
|
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
|
# Inherit a class having a default scope and define a new default scope
|
||||||
klass = Class.new(DeveloperOrderedBySalary)
|
klass = Class.new(DeveloperOrderedBySalary)
|
||||||
klass.send :default_scope, {}
|
klass.send :default_scope, {}
|
||||||
|
|
||||||
# Scopes added on children should append to parent 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)
|
assert_equal expected_klass_scope, klass.send(:scoped_methods)
|
||||||
|
|
||||||
# Parent should still have the original scope
|
# Parent should still have the original scope
|
||||||
|
@ -568,6 +568,12 @@ class DefaultScopingTest < ActiveRecord::TestCase
|
||||||
assert_equal expected, received
|
assert_equal expected, received
|
||||||
end
|
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
|
def test_nested_exclusive_scope
|
||||||
expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary }
|
expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary }
|
||||||
received = DeveloperOrderedBySalary.with_exclusive_scope(:find => { :limit => 100 }) do
|
received = DeveloperOrderedBySalary.with_exclusive_scope(:find => { :limit => 100 }) do
|
||||||
|
|
|
@ -79,13 +79,13 @@ class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
class DeveloperOrderedBySalary < ActiveRecord::Base
|
class DeveloperOrderedBySalary < ActiveRecord::Base
|
||||||
self.table_name = 'developers'
|
self.table_name = 'developers'
|
||||||
default_scope :order => "salary DESC"
|
default_scope :order => 'salary DESC'
|
||||||
|
named_scope :by_name, :order => 'name DESC'
|
||||||
|
|
||||||
def self.all_ordered_by_name
|
def self.all_ordered_by_name
|
||||||
with_scope(:find => { :order => "name DESC" }) do
|
with_scope(:find => { :order => 'name DESC' }) do
|
||||||
find(:all)
|
find(:all)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue