Skip to content

Commit

Permalink
Fix rails#6635. We should call Scoping methods, before calling Array …
Browse files Browse the repository at this point in the history
…methods.
  • Loading branch information
kennyj committed Jun 10, 2012
1 parent 42259f6 commit cd1be1a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
10 changes: 5 additions & 5 deletions activerecord/lib/active_record/relation/delegation.rb
Expand Up @@ -32,12 +32,12 @@ def respond_to?(method, include_private = false)
protected

def method_missing(method, *args, &block)
if Array.method_defined?(method)
::ActiveRecord::Delegation.delegate method, :to => :to_a
to_a.send(method, *args, &block)
elsif @klass.respond_to?(method)
if @klass.respond_to?(method)
::ActiveRecord::Delegation.delegate_to_scoped_klass(method)
scoping { @klass.send(method, *args, &block) }
elsif Array.method_defined?(method)
::ActiveRecord::Delegation.delegate method, :to => :to_a
to_a.send(method, *args, &block)
elsif arel.respond_to?(method)
::ActiveRecord::Delegation.delegate method, :to => :arel
arel.send(method, *args, &block)
Expand All @@ -46,4 +46,4 @@ def method_missing(method, *args, &block)
end
end
end
end
end
9 changes: 9 additions & 0 deletions activerecord/test/cases/named_scope_test.rb
Expand Up @@ -45,6 +45,15 @@ def test_delegates_finds_and_calculations_to_the_base_class
assert_equal Topic.average(:replies_count), Topic.base.average(:replies_count)
end

def test_method_missing_priority_when_delegating
klazz = Class.new(ActiveRecord::Base) do
self.table_name = "topics"
scope :since, Proc.new { where('written_on >= ?', Time.now - 1.day) }
scope :to, Proc.new { where('written_on <= ?', Time.now) }
end
assert_equal klazz.to.since.all, klazz.since.to.all
end

def test_scope_should_respond_to_own_methods_and_methods_of_the_proxy
assert Topic.approved.respond_to?(:limit)
assert Topic.approved.respond_to?(:count)
Expand Down

0 comments on commit cd1be1a

Please sign in to comment.