diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2010-09-01 15:09:04 +0100 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2010-09-01 22:32:41 +0200 |
commit | 4e67bf26aa11a59e4f513359208fe3314c5a1ba5 (patch) | |
tree | fe85848facd45aa82627651dce257831339a4e74 /activerecord | |
parent | f1966337fa744a1c62054120693072ebabe996a8 (diff) | |
download | rails-4e67bf26aa11a59e4f513359208fe3314c5a1ba5.tar.gz rails-4e67bf26aa11a59e4f513359208fe3314c5a1ba5.tar.bz2 rails-4e67bf26aa11a59e4f513359208fe3314c5a1ba5.zip |
Reset default scope in Thread.current when class is unloaded [#5497 state:resolved]
Signed-off-by: Xavier Noria <fxn@hashref.com>
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/base.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 15 |
2 files changed, 23 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index f465a38dbe..c3a34ae104 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -890,6 +890,10 @@ module ActiveRecord #:nodoc: Thread.current[key] = Thread.current[key].presence || self.default_scoping.dup end + def before_remove_const #:nodoc: + reset_scoped_methods + end + private def relation #:nodoc: @@ -1174,6 +1178,10 @@ MSG scoped_methods.last end + def reset_scoped_methods #:nodoc: + Thread.current[:"#{self}_scoped_methods"] = nil + end + # Returns the class type of the record using the current module as a prefix. So descendants of # MyApp::Business::Account would appear as MyApp::Business::AccountSubclass. def compute_type(type_name) diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 55f0b1ce21..d58e302cb2 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1415,6 +1415,21 @@ class BasicsTest < ActiveRecord::TestCase end end + def test_default_scope_is_reset + Object.const_set :UnloadablePost, Class.new(ActiveRecord::Base) + UnloadablePost.table_name = 'posts' + UnloadablePost.class_eval do + default_scope order('posts.comments_count ASC') + end + + UnloadablePost.unloadable + assert_not_nil Thread.current[:UnloadablePost_scoped_methods] + ActiveSupport::Dependencies.remove_unloadable_constants! + assert_nil Thread.current[:UnloadablePost_scoped_methods] + ensure + Object.class_eval{ remove_const :UnloadablePost } if defined?(UnloadablePost) + end + protected def with_env_tz(new_tz = 'US/Eastern') old_tz, ENV['TZ'] = ENV['TZ'], new_tz |