aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2010-01-18 00:03:18 +0530
committerPratik Naik <pratiknaik@gmail.com>2010-01-18 00:03:18 +0530
commitc6850d8361bbf288cf3adefd087cb9a4bc9c97bc (patch)
treea0050bede3bb000f61e0d4e31b9319db10159287 /activerecord
parentdca3de3bc766175f49b56202246d3625c58fd763 (diff)
downloadrails-c6850d8361bbf288cf3adefd087cb9a4bc9c97bc.tar.gz
rails-c6850d8361bbf288cf3adefd087cb9a4bc9c97bc.tar.bz2
rails-c6850d8361bbf288cf3adefd087cb9a4bc9c97bc.zip
Ensure that Scope#proxy_scope is always klass. Rename proxy_scope to klass too.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/named_scope.rb38
-rw-r--r--activerecord/lib/active_record/relation.rb2
-rw-r--r--activerecord/test/cases/named_scope_test.rb8
3 files changed, 22 insertions, 26 deletions
diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb
index 30e75534dd..9e65fb4ca5 100644
--- a/activerecord/lib/active_record/named_scope.rb
+++ b/activerecord/lib/active_record/named_scope.rb
@@ -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
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 7c2a080ead..fc429486e4 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -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
diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb
index 6f84e12a49..09a657791e 100644
--- a/activerecord/test/cases/named_scope_test.rb
+++ b/activerecord/test/cases/named_scope_test.rb
@@ -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