Ensure that Scope#proxy_scope is always klass. Rename proxy_scope to klass too.

This commit is contained in:
Pratik Naik
2010-01-18 00:03:18 +05:30
parent dca3de3bc7
commit c6850d8361
3 changed files with 22 additions and 26 deletions

View File

@@ -121,7 +121,7 @@ module ActiveRecord
end
class Scope
attr_reader :proxy_scope, :proxy_options, :current_scoped_methods_when_defined
attr_reader :klass, :proxy_options, :current_scoped_methods_when_defined
NON_DELEGATE_METHODS = %w(nil? send object_id class extend find size count sum average maximum minimum paginate first last empty? any? many? respond_to?).to_set
[].methods.each do |m|
unless m =~ /^__/ || NON_DELEGATE_METHODS.include?(m.to_s)
@@ -129,9 +129,10 @@ module ActiveRecord
end
end
delegate :scopes, :with_scope, :scoped_methods, :unscoped, :to => :proxy_scope
delegate :scopes, :with_scope, :with_exclusive_scope, :scoped_methods, :scoped, :to => :klass
delegate :new, :build, :all, :to => :relation
def initialize(proxy_scope, options, &block)
def initialize(klass, options, &block)
extend Module.new(&block) if block_given?
options ||= {}
@@ -142,11 +143,11 @@ module ActiveRecord
@proxy_options = options
end
unless Scope === proxy_scope
@current_scoped_methods_when_defined = proxy_scope.send(:current_scoped_methods)
unless Scope === klass
@current_scoped_methods_when_defined = klass.send(:current_scoped_methods)
end
@proxy_scope = proxy_scope
@klass = klass
end
def reload
@@ -178,7 +179,7 @@ module ActiveRecord
end
def respond_to?(method, include_private = false)
super || @proxy_scope.respond_to?(method, include_private)
super || @klass.respond_to?(method, include_private)
end
def any?
@@ -198,14 +199,12 @@ module ActiveRecord
end
end
protected
def relation
@relation ||= begin
if proxy_options.is_a?(Hash)
unscoped.apply_finder_options(proxy_options)
scoped.apply_finder_options(proxy_options)
else
unscoped.merge(proxy_options)
scoped.merge(proxy_options)
end
end
end
@@ -217,18 +216,13 @@ module ActiveRecord
private
def method_missing(method, *args, &block)
if scopes.include?(method)
scopes[method].call(self, *args)
else
with_scope(relation, :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)
with_scope(relation, :reverse_merge) do
if current_scoped_methods_when_defined && !scoped_methods.include?(current_scoped_methods_when_defined) && !scopes.include?(method)
with_scope current_scoped_methods_when_defined do
klass.send(method, *args, &block)
end
else
klass.send(method, *args, &block)
end
end
end

View File

@@ -20,6 +20,8 @@ module ActiveRecord
with_create_scope { @klass.new(*args, &block) }
end
alias build new
def create(*args, &block)
with_create_scope { @klass.create(*args, &block) }
end

View File

@@ -345,14 +345,14 @@ class NamedScopeTest < ActiveRecord::TestCase
def test_chaining_should_use_latest_conditions_when_searching
# Normal hash conditions
assert_equal Topic.where(:approved => true).to_a, Topic.rejected.approved.all.to_a
assert_equal Topic.where(:approved => false).to_a, Topic.approved.rejected.all.to_a
assert_equal Topic.where(:approved => true).to_a, Topic.rejected.approved.all
assert_equal Topic.where(:approved => false).to_a, Topic.approved.rejected.all
# Nested hash conditions with same keys
assert_equal [posts(:sti_comments)], Post.with_special_comments.with_very_special_comments.all.to_a
assert_equal [posts(:sti_comments)], Post.with_special_comments.with_very_special_comments.all
# Nested hash conditions with different keys
assert_equal [posts(:sti_comments)], Post.with_special_comments.with_post(4).all.to_a.uniq
assert_equal [posts(:sti_comments)], Post.with_special_comments.with_post(4).all.uniq
end
def test_named_scopes_batch_finders