What does Reloadable mean in rails source code?

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

include Reloadable::Subclasses

And following is the definition of Reloadable:

Classes that include this module will automatically be reloaded

by the Rails dispatcher when Dependencies.mechanism = :load.

module Reloadable
class << self
def included(base) #nodoc:
raise TypeError, “Only Classes can be Reloadable!” unless
base.is_a? Class

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

def reloadable_classes
  included_in_classes.select { |klass| klass.reloadable? }
end

end

Captures the common pattern where a base class should not be

reloaded,

but its subclasses should be.

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

Can anybody here explain how “include Reloadable::Subclasses” works

On Jan 29, 2007, at 7:35 AM, Fredzhang wrote:

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
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

end

end

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 }
end
end

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
end
end
end

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
code),

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

– Jean-François.


À la renverse.