diff options
Diffstat (limited to 'railties/lib/rails/railtie.rb')
-rw-r--r-- | railties/lib/rails/railtie.rb | 91 |
1 files changed, 59 insertions, 32 deletions
diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index e3297148e5..6424be5a55 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -1,44 +1,73 @@ +require 'rails/initializable' +require 'rails/configuration' + module Rails class Railtie + autoload :Configurable, "rails/railtie/configurable" + autoload :Configuration, "rails/railtie/configuration" + include Initializable - def self.plugin_name(plugin_name = nil) - @plugin_name ||= name.demodulize.underscore - @plugin_name = plugin_name if plugin_name - @plugin_name - end + ABSTRACT_RAILTIES = %w(Rails::Plugin Rails::Engine Rails::Application) - def self.inherited(klass) - @plugins ||= [] - @plugins << klass unless klass == Plugin - end + class << self + def subclasses + @subclasses ||= [] + end - def self.plugins - @plugins - end + def inherited(base) + unless abstract_railtie?(base) + base.send(:include, self::Configurable) if add_configurable?(base) + subclasses << base + end + end - def self.plugin_names - plugins.map { |p| p.plugin_name } - end + # TODO This should be called railtie_name and engine_name + def plugin_name(plugin_name = nil) + @plugin_name ||= name.demodulize.underscore + @plugin_name = plugin_name if plugin_name + @plugin_name + end - def self.config - Configuration.default - end + # TODO Deprecate me + def plugins + subclasses + end - def self.subscriber(subscriber) - Rails::Subscriber.add(plugin_name, subscriber) - end + # TODO Deprecate me + def plugin_names + plugins.map { |p| p.plugin_name } + end - def self.rake_tasks(&blk) - @rake_tasks ||= [] - @rake_tasks << blk if blk - @rake_tasks - end + def subscriber(subscriber) + Rails::Subscriber.add(plugin_name, subscriber) + end + + def rake_tasks(&blk) + @rake_tasks ||= [] + @rake_tasks << blk if blk + @rake_tasks + end + + def generators(&blk) + @generators ||= [] + @generators << blk if blk + @generators + end + + protected + + def abstract_railtie?(base) + ABSTRACT_RAILTIES.include?(base.name) + end - def self.generators(&blk) - @generators ||= [] - @generators << blk if blk - @generators + # Just add configurable behavior if a Configurable module is defined + # and the class is a direct child from self. This is required to avoid + # application or plugins getting class configuration method from Railties + # and/or Engines. + def add_configurable?(base) + defined?(self::Configurable) && base.ancestors[1] == self + end end def rake_tasks @@ -50,12 +79,10 @@ module Rails end def load_tasks - return unless rake_tasks rake_tasks.each { |blk| blk.call } end def load_generators - return unless generators generators.each { |blk| blk.call } end end |