What does Reloadable mean in rails source code?

In Rails pre-1.2 (in 1.2 this has been revised, I need to reread the code), the Reloadable module marked classes that we wanted to be reloaded if reloading was active. But sometimes you don't want your very class to be reloadable, but its subclasses.

Models and controllers are reloaded in development mode because ActiveRecord::Base and ActionController::Base mixin Reloadable::Subclasses:

   module ActiveRecord
     class Base
       include Reloadable::Subclasses

The rationale here is that Rails evolves fast but not _that_ fast :-), so there's no point reloading ActiveRecord::Base itself in each request. With that little trick you skip reloading the framework AND free the programmer from having to mark each model and controller.

When you include Reloadable in a class, it gets a class method called reloadable? that returns true.

   unless base.respond_to?(:reloadable?)
     class << base
       define_method(:reloadable?) { true }

When you include Reloadable::Subclasses in a class it gets a class methods called reloadable? that returns true unless the class is the one where the method was defined. That's the clousure:

   module Subclasses
     def self.included(base) #nodoc:
       base.send :include, Reloadable
       (class << base; self; end).send(:define_method, :reloadable?) do
          base != self

And so, when Rails wants to reload the part of the application that is reloadable, it iterates over all classes in the ObjectSpace, and asks to those classes whether they respond to reloadable? In that case Rails send :reloadable? and the constant is removed if the method returns true.

That happens at the end of each request (if reloading is turned on). As you see the way to get reloading is to remove the constant, if we just reinterpreted the file we would be *reopening* the class, which is not the same thing. Thus, the very reloading is delegated again to const_missing.

-- fxn

Xavier :

> In the actionpack-1.12.5\lib\action_controller\base.rb,
> you can see the definition:
> class Base
> include Reloadable::Subclasses

In Rails pre-1.2 (in 1.2 this has been revised, I need to reread the

Reloadable mechanism is deprecated in 1.2. If my understanding is
correct, this is now handled by the Dependencies new code.

   -- Jean-François.