Forum: Ruby on Rails What does Reloadable mean in rails source code?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1ad8ede8ab356db4c7efac0fe6dc2fdf?d=identicon&s=25 Fredzhang (Guest)
on 2007-01-29 07:35
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
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2007-01-29 09:41
(Received via mailing list)
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
13ea48f056b19349027d2f4a6921a46a?d=identicon&s=25 Jean-François (Guest)
on 2007-01-29 11:39
(Received via mailing list)
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.
This topic is locked and can not be replied to.