1  require 'active_record'
 2  require 'action_controller'
 3  require 'action_mailer'
 4  require 'action_view'
 5
 6  # A module that defines the behavior of a class that may contain a registry
 7  # instance.
 8  module RegistryContainer
 9
10    # Return the registry instance in effect for this class and all subclasses.
11    def registry
12      self.class.registry
13    end
14
15    # The #registry=, #registry, and #register methods must be dynamically
16    # added by this method because they use class variables.
17    def self.included( mod )
18      super
19      mod.class_eval <<-EOF
20        def self.registry=( reg )
21          @@registry = reg
22        end
23
24        def self.registry
25          @@registry
26        end
27
28        def self.register( name )
29          registry.register( name ) { self }
30        end
31      EOF
32    end
33
34  end
35
36  class ActiveRecord::Base
37    include RegistryContainer
38
39    class << self
40      alias :old_inherited :inherited
41
42      # Automatically add this class as a service to the class's registry. The
43      # service will be an underscored, demodulized, symbolized version of the
44      # class name.
45      def inherited( klass )
46        old_inherited klass
47        klass.register( Inflector.underscore( Inflector.demodulize( klass.name ) ).to_sym )
48      end
49    end
50  end
51
52  class ActionController::Base
53    include RegistryContainer
54  end
55
56  class ActionMailer::Base
57    include RegistryContainer
58  end
59
60  class ActionView::Base
61    include RegistryContainer
62  end