diff options
author | Eric Allam <rubymaverick@gmail.com> | 2011-02-15 21:48:11 -0500 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-02-24 11:17:06 -0800 |
commit | 13547c16d97b5f52db11d9f48704bbea20b54a4c (patch) | |
tree | ef9daa907140d9dcf38c7eac4c8acae0cbb99535 | |
parent | 5f1fc0c8ac6e71bfb4d66da2005b1b0694e18446 (diff) | |
download | rails-13547c16d97b5f52db11d9f48704bbea20b54a4c.tar.gz rails-13547c16d97b5f52db11d9f48704bbea20b54a4c.tar.bz2 rails-13547c16d97b5f52db11d9f48704bbea20b54a4c.zip |
fixes: ActiveRecord::Base.scopes includes all scopes defined in all subclasses
-rw-r--r-- | activerecord/lib/active_record/named_scope.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/named_scope_test.rb | 4 |
2 files changed, 7 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 0f421560f0..d291632260 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -102,10 +102,9 @@ module ActiveRecord 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 @@ module ActiveRecord extension ? relation.extending(extension) : relation end + self.scopes = self.scopes.merge name => scope_proc + singleton_class.send(:redefine_method, name, &scopes[name]) end diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index d05b0ff947..fb050c3e52 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -64,6 +64,10 @@ class NamedScopeTest < ActiveRecord::TestCase 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? |