aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-10-19 17:27:50 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2010-10-19 17:29:25 -0700
commitd2898d4ef80dc74ef0d6204b5c7f50877659e50e (patch)
tree1b1744f416f4ec6030833a573a966ca1ada58e5b
parent56be4c897ae52d73583c51dec155e6161e3dc900 (diff)
downloadrails-d2898d4ef80dc74ef0d6204b5c7f50877659e50e.tar.gz
rails-d2898d4ef80dc74ef0d6204b5c7f50877659e50e.tar.bz2
rails-d2898d4ef80dc74ef0d6204b5c7f50877659e50e.zip
scopes can take an object that responds to `call`
-rw-r--r--activerecord/lib/active_record/named_scope.rb2
-rw-r--r--activerecord/test/cases/named_scope_test.rb6
-rw-r--r--activerecord/test/models/topic.rb7
3 files changed, 14 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb
index b767a2b4a8..0b92ba5caa 100644
--- a/activerecord/lib/active_record/named_scope.rb
+++ b/activerecord/lib/active_record/named_scope.rb
@@ -104,7 +104,7 @@ module ActiveRecord
extension = Module.new(&Proc.new) if block_given?
scopes[name] = lambda do |*args|
- options = scope_options.is_a?(Proc) ? scope_options.call(*args) : scope_options
+ options = scope_options.respond_to?(:call) ? scope_options.call(*args) : scope_options
relation = if options.is_a?(Hash)
scoped.apply_finder_options(options)
diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb
index cc4438395e..fb24c65fff 100644
--- a/activerecord/test/cases/named_scope_test.rb
+++ b/activerecord/test/cases/named_scope_test.rb
@@ -125,6 +125,12 @@ class NamedScopeTest < ActiveRecord::TestCase
assert_equal posts_with_authors_at_address_titles, Post.with_authors_at_address(address).find(:all, :select => 'title')
end
+ def test_scope_with_object
+ objects = Topic.with_object
+ assert_operator objects.length, :>, 0
+ assert objects.all?(&:approved?), 'all objects should be approved'
+ end
+
def test_extensions
assert_equal 1, Topic.anonymous_extension.one
assert_equal 2, Topic.named_extension.two
diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb
index ba2fe1987b..82d4b5997f 100644
--- a/activerecord/test/models/topic.rb
+++ b/activerecord/test/models/topic.rb
@@ -18,6 +18,13 @@ class Topic < ActiveRecord::Base
1
end
end
+
+ scope :with_object, Class.new(Struct.new(:klass)) {
+ def call
+ klass.where(:approved => true)
+ end
+ }.new(self)
+
module NamedExtension
def two
2