Skip to content

Commit

Permalink
Reset default scope in Thread.current when class is unloaded [#5497 s…
Browse files Browse the repository at this point in the history
…tate:resolved]

Signed-off-by: Xavier Noria <fxn@hashref.com>
  • Loading branch information
pixeltrix authored and fxn committed Sep 1, 2010
1 parent bf87528 commit aefa11b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit aefa11b

Please sign in to comment.