Skip to content
Browse files

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

…klass too.
  • Loading branch information...
1 parent dca3de3 commit c6850d8361bbf288cf3adefd087cb9a4bc9c97bc @lifo lifo committed Jan 18, 2010
View
38 activerecord/lib/active_record/named_scope.rb
@@ -121,17 +121,18 @@ def named_scope(name, options = {}, &block)
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)
delegate m, :to => :proxy_found
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 @@ def initialize(proxy_scope, options, &block)
@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 @@ def empty?
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 @@ def many?
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 @@ def proxy_found
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
2 activerecord/lib/active_record/relation.rb
@@ -20,6 +20,8 @@ def new(*args, &block)
with_create_scope { @klass.new(*args, &block) }
end
+ alias build new
+
def create(*args, &block)
with_create_scope { @klass.create(*args, &block) }
end
View
8 activerecord/test/cases/named_scope_test.rb
@@ -345,14 +345,14 @@ def test_chaining_should_use_latest_conditions_when_creating
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

0 comments on commit c6850d8

Please sign in to comment.
Something went wrong with that request. Please try again.