From 95f6d53797f3e37c23f2e34479786f1beb677e97 Mon Sep 17 00:00:00 2001 From: Ryan Burrows Date: Mon, 14 Feb 2011 12:43:57 -0800 Subject: [PATCH] Inherit named scopes created at runtime --- activerecord/lib/active_record/named_scope.rb | 4 ++++ activerecord/test/cases/named_scope_test.rb | 5 +++++ 2 files changed, 9 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 5e49de6..b4c4bc6 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -102,6 +102,10 @@ module ActiveRecord singleton_class.send :define_method, name do |*args| scopes[name].call(self, *args) end + + subclasses.each do |s| + s.scopes[name] = scopes[name] + end end end diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 755fb04..066d3fa 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -349,6 +349,11 @@ class NamedScopeTest < ActiveRecord::TestCase Comment.for_first_post.for_first_author.all end end + + def test_dynamically_added_scopes_are_inherited + Comment.named_scope :thoughtful, :conditions => "body LIKE '%think%'" + assert_equal SpecialComment.thoughtful.find(3), SpecialComment.find(3) + end end class DynamicScopeMatchTest < ActiveRecord::TestCase -- 1.7.0.4