Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix infinite recursion where a lazy default scope references a scope.…

… Fixes #1264.
  • Loading branch information...
commit c69111ba5fde8445237f682c88b927bcde1588d4 1 parent d21fef3
@jonleighton jonleighton authored
View
15 activerecord/lib/active_record/base.rb
@@ -428,6 +428,10 @@ class Base
class_attribute :default_scopes, :instance_writer => false
self.default_scopes = []
+ # Boolean flag to prevent infinite recursion when evaluating default scopes
+ class_attribute :apply_default_scope, :instance_writer => false
+ self.apply_default_scope = true
@josevalim Owner

@jonleighton, maybe you would like to use @variables instead? setting a class_attribute is a bit expensive (as it defines a new method) and @variable is enough because it does not seem like you need inheritance here.

@jonleighton Collaborator

Ha. Yes. I think I just defined this as a class_attribute without thinking, because default_scopes is a class attribute. I will change it. Come to think of it, default_scopes probably doesn't need to be a class attribute either.

@pixeltrix Owner

What about STI where a subclass adds a default scope? Sounds like you'd want inheritance then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
# Returns a hash of all the attributes that have been specified for serialization as
# keys and their class restriction as values.
class_attribute :serialized_attributes
@@ -1261,11 +1265,14 @@ def default_scope(scope = {})
self.default_scopes = default_scopes + [scope]
end
+ # The apply_default_scope flag is used to prevent an infinite recursion situation where
+ # a default scope references a scope which has a default scope which references a scope...
def build_default_scope #:nodoc:
+ return unless apply_default_scope
+ self.apply_default_scope = false
+
if method(:default_scope).owner != Base.singleton_class
- # Use relation.scoping to ensure we ignore whatever the current value of
- # self.current_scope may be.
- relation.scoping { default_scope }
+ default_scope
elsif default_scopes.any?
default_scopes.inject(relation) do |default_scope, scope|
if scope.is_a?(Hash)
@@ -1277,6 +1284,8 @@ def build_default_scope #:nodoc:
end
end
end
+ ensure
+ self.apply_default_scope = true
end
# Returns the class type of the record using the current module as a prefix. So descendants of
View
2  activerecord/lib/active_record/relation.rb
@@ -424,7 +424,7 @@ def inspect
end
def with_default_scope #:nodoc:
- if default_scoped? && default_scope = @klass.send(:build_default_scope)
+ if default_scoped? && default_scope = klass.send(:build_default_scope)
default_scope = default_scope.merge(self)
default_scope.default_scoped = false
default_scope
View
8 activerecord/test/cases/relation_scoping_test.rb
@@ -312,6 +312,14 @@ def test_default_scope_as_class_method
assert_equal [developers(:david).becomes(ClassMethodDeveloperCalledDavid)], ClassMethodDeveloperCalledDavid.all
end
+ def test_default_scope_as_class_method_referencing_scope
+ assert_equal [developers(:david).becomes(ClassMethodReferencingScopeDeveloperCalledDavid)], ClassMethodReferencingScopeDeveloperCalledDavid.all
+ end
+
+ def test_default_scope_as_block_referencing_scope
+ assert_equal [developers(:david).becomes(LazyBlockReferencingScopeDeveloperCalledDavid)], LazyBlockReferencingScopeDeveloperCalledDavid.all
+ end
+
def test_default_scope_with_lambda
assert_equal [developers(:david).becomes(LazyLambdaDeveloperCalledDavid)], LazyLambdaDeveloperCalledDavid.all
end
View
15 activerecord/test/models/developer.rb
@@ -127,6 +127,21 @@ def self.default_scope
end
end
+class ClassMethodReferencingScopeDeveloperCalledDavid < ActiveRecord::Base
+ self.table_name = 'developers'
+ scope :david, where(:name => 'David')
+
+ def self.default_scope
+ david
+ end
+end
+
+class LazyBlockReferencingScopeDeveloperCalledDavid < ActiveRecord::Base
+ self.table_name = 'developers'
+ scope :david, where(:name => 'David')
+ default_scope { david }
+end
+
class DeveloperCalledJamis < ActiveRecord::Base
self.table_name = 'developers'
Please sign in to comment.
Something went wrong with that request. Please try again.