Permalink
Browse files

fixes: ActiveRecord::Base.scopes includes all scopes defined in all s…

…ubclasses
  • Loading branch information...
1 parent 5f1fc0c commit 13547c16d97b5f52db11d9f48704bbea20b54a4c Eric Allam committed with tenderlove Feb 16, 2011
Showing with 7 additions and 2 deletions.
  1. +3 −2 activerecord/lib/active_record/named_scope.rb
  2. +4 −0 activerecord/test/cases/named_scope_test.rb
@@ -102,10 +102,9 @@ def scoped(options = nil)
def scope(name, scope_options = {})
name = name.to_sym
valid_scope_name?(name)
-
extension = Module.new(&Proc.new) if block_given?
- scopes[name] = lambda do |*args|
+ scope_proc = lambda do |*args|
options = scope_options.respond_to?(:call) ? scope_options.call(*args) : scope_options
relation = if options.is_a?(Hash)
@@ -119,6 +118,8 @@ def scope(name, scope_options = {})
extension ? relation.extending(extension) : relation
end
+ self.scopes = self.scopes.merge name => scope_proc
+
singleton_class.send(:redefine_method, name, &scopes[name])
end
@@ -64,6 +64,10 @@ def test_subclasses_inherit_scopes
assert Reply.scopes.include?(:base)
assert_equal Reply.find(:all), Reply.base
end
+
+ def test_classes_dont_inherit_subclasses_scopes
+ assert !ActiveRecord::Base.scopes.include?(:base)
+ end
def test_scopes_with_options_limit_finds_to_those_matching_the_criteria_specified
assert !Topic.find(:all, :conditions => {:approved => true}).empty?

0 comments on commit 13547c1

Please sign in to comment.