Permalink
Browse files

Reset default scope in Thread.current when class is unloaded [#5497 s…

…tate:resolved]

Signed-off-by: Xavier Noria <fxn@hashref.com>
  • Loading branch information...
1 parent bf87528 commit aefa11be11a0a6b0c98b776c9e6422b2e0e4b13d @pixeltrix pixeltrix committed with fxn Sep 1, 2010
Showing with 23 additions and 0 deletions.
  1. +8 −0 activerecord/lib/active_record/base.rb
  2. +15 −0 activerecord/test/cases/base_test.rb
@@ -890,6 +890,10 @@ def scoped_methods #: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 @@ def current_scoped_methods #:nodoc:
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)
@@ -1415,6 +1415,21 @@ def test_compute_type_no_method_error
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

0 comments on commit aefa11b

Please sign in to comment.