1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

scopes can take an object that responds to call

This commit is contained in:
Aaron Patterson 2010-10-19 17:27:50 -07:00
parent 56be4c897a
commit d2898d4ef8
3 changed files with 14 additions and 1 deletions

View file

@ -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)

View file

@ -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

View file

@ -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