Skip to content

Commit f196633

Browse files
pixeltrixfxn
authored andcommitted
Add before_remove_const callback to ActiveSupport::Dependencies.remove_unloadable_constants!
Signed-off-by: Xavier Noria <fxn@hashref.com>
1 parent 2ce57fd commit f196633

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

activesupport/CHANGELOG

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
*Rails 3.1.0 (unreleased)*
22

3-
* No changes
3+
* Added before_remove_const callback to ActiveSupport::Dependencies.remove_unloadable_constants! [Andrew White]
44

55

66
*Rails 3.0.0 (August 29, 2010)*

activesupport/lib/active_support/dependencies.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,12 @@ def load_missing_constant(from_mod, const_name)
511511
end
512512

513513
# Remove the constants that have been autoloaded, and those that have been
514-
# marked for unloading.
514+
# marked for unloading. Before each constant is removed a callback is sent
515+
# to its class/module if it implements +before_remove_const+.
516+
#
517+
# The callback implementation should be restricted to cleaning up caches, etc.
518+
# as the enviroment will be in an inconsistent state, e.g. other constants
519+
# may have already been unloaded and not accessible.
515520
def remove_unloadable_constants!
516521
autoloaded_constants.each { |const| remove_constant const }
517522
autoloaded_constants.clear
@@ -636,6 +641,7 @@ def remove_constant(const) #:nodoc:
636641
parent = Inflector.constantize(names * '::')
637642

638643
log "removing constant #{const}"
644+
constantize(const).before_remove_const if constantize(const).respond_to?(:before_remove_const)
639645
parent.instance_eval { remove_const to_remove }
640646

641647
return true

activesupport/test/dependencies_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,17 @@ def test_unloadable_should_return_change_flag
574574
end
575575
end
576576

577+
def test_unloadable_constants_should_receive_callback
578+
Object.const_set :C, Class.new
579+
C.unloadable
580+
C.expects(:before_remove_const).once
581+
assert C.respond_to?(:before_remove_const)
582+
ActiveSupport::Dependencies.clear
583+
assert !defined?(C)
584+
ensure
585+
Object.class_eval { remove_const :C } if defined?(C)
586+
end
587+
577588
def test_new_contants_in_without_constants
578589
assert_equal [], (ActiveSupport::Dependencies.new_constants_in(Object) { })
579590
assert ActiveSupport::Dependencies.constant_watch_stack.all? {|k,v| v.empty? }

0 commit comments

Comments
 (0)