aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xactiverecord/lib/active_record/base.rb2
-rw-r--r--activerecord/test/cases/method_scoping_test.rb12
-rw-r--r--activerecord/test/models/developer.rb14
3 files changed, 17 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 68f44ef0f6..cff5fd7a42 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -2027,7 +2027,7 @@ module ActiveRecord #: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.
diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb
index 4ac0018144..6372b4f6aa 100644
--- a/activerecord/test/cases/method_scoping_test.rb
+++ b/activerecord/test/cases/method_scoping_test.rb
@@ -532,7 +532,7 @@ class DefaultScopingTest < ActiveRecord::TestCase
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 @@ class DefaultScopingTest < ActiveRecord::TestCase
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 @@ class DefaultScopingTest < ActiveRecord::TestCase
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
diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb
index 0c20f97502..92039a4f54 100644
--- a/activerecord/test/models/developer.rb
+++ b/activerecord/test/models/developer.rb
@@ -79,13 +79,13 @@ class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base
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