Permalink
Browse files

Edit ActiveRecord::NamedScope::Scope#method_missing to check Array me…

…thods

This allows methods to be added to Array after this file has loaded
and be available on named scopes:

	class Array
		def foo
			"bar"
		end
	end

	BlogPost.published.foo # => "bar"
  • Loading branch information...
1 parent 3875e29 commit 391c2a3326e826a067cf0899b6e0a81bd2562ed4 Lewis Marshall committed May 9, 2011
Showing with 12 additions and 8 deletions.
  1. +12 −8 activerecord/lib/active_record/named_scope.rb
@@ -173,17 +173,21 @@ def proxy_found
private
def method_missing(method, *args, &block)
- if scopes.include?(method)
- scopes[method].call(self, *args)
+ if [].respond_to?(method) && !NON_DELEGATE_METHODS.include?(method.to_s)
+ proxy_found.send(method, *args, &block)
else
- with_scope({:find => proxy_options, :create => proxy_options[:conditions].is_a?(Hash) ? proxy_options[:conditions] : {}}, :reverse_merge) do
- method = :new if method == :build
- if current_scoped_methods_when_defined && !scoped_methods.include?(current_scoped_methods_when_defined)
- with_scope current_scoped_methods_when_defined do
+ if scopes.include?(method)
+ scopes[method].call(self, *args)
+ else
+ with_scope({:find => proxy_options, :create => proxy_options[:conditions].is_a?(Hash) ? proxy_options[:conditions] : {}}, :reverse_merge) do
+ method = :new if method == :build
+ if current_scoped_methods_when_defined && !scoped_methods.include?(current_scoped_methods_when_defined)
+ with_scope current_scoped_methods_when_defined do
+ proxy_scope.send(method, *args, &block)
+ end
+ else
proxy_scope.send(method, *args, &block)
end
- else
- proxy_scope.send(method, *args, &block)
end
end
end

0 comments on commit 391c2a3

Please sign in to comment.