diff options
author | José Valim <jose.valim@gmail.com> | 2010-01-23 18:41:53 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-01-23 18:41:53 +0100 |
commit | 788fce2550ed587d86d239d8fcd488f919d15b5d (patch) | |
tree | 779386fb3bb2341af6a03e390233420b184cb891 /railties | |
parent | 2b75b94ac0c329de12a0a94ba77f8aad5574514f (diff) | |
download | rails-788fce2550ed587d86d239d8fcd488f919d15b5d.tar.gz rails-788fce2550ed587d86d239d8fcd488f919d15b5d.tar.bz2 rails-788fce2550ed587d86d239d8fcd488f919d15b5d.zip |
Create configurable modules and ensure that they are added only on direct children.
Diffstat (limited to 'railties')
-rw-r--r-- | railties/lib/rails/application.rb | 58 | ||||
-rw-r--r-- | railties/lib/rails/engine.rb | 38 | ||||
-rw-r--r-- | railties/lib/rails/engine/configurable.rb | 24 | ||||
-rw-r--r-- | railties/lib/rails/plugin.rb | 32 | ||||
-rw-r--r-- | railties/lib/rails/railtie.rb | 34 | ||||
-rw-r--r-- | railties/lib/rails/railtie/configurable.rb | 23 |
6 files changed, 128 insertions, 81 deletions
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 6d1702bf93..676e395d39 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -9,6 +9,8 @@ module Rails # TODO Check helpers works as expected # TODO Check routes namespaces + # TODO raise "You cannot have more than one Rails::Application" if Rails.application + # TODO Ensure production settings are read properly class << self private :new alias :configure :class_eval @@ -17,30 +19,10 @@ module Rails @instance ||= new end - def config - @config ||= Configuration.new(self.original_root) - end - - def original_root - @original_root ||= find_root_with_file_flag("config.ru", Dir.pwd) - end - def inherited(base) - # TODO Add this check - # raise "You cannot have more than one Rails::Application" if Rails.application super - - # TODO Add a test which ensures me - # Railtie.plugins.delete(base) - Rails.application ||= base.instance - - base.rake_tasks do - require "rails/tasks" - task :environment do - $rails_rake_task = true - initialize! - end - end + Rails.application = base.instance + base.require_environment! end protected @@ -50,16 +32,15 @@ module Rails end end - # Application is always reloadable when config.cache_classes is false. - def reloadable?(app) - true - end - - def initialize + def require_environment! environment = config.paths.config.environment.to_a.first require environment if environment end + def config + @config ||= ::Rails::Configuration.new(self.class.find_root_with_flag("config.ru", Dir.pwd)) + end + def routes ::ActionController::Routing::Routes end @@ -82,12 +63,14 @@ module Rails end def load_tasks + initialize_tasks super railties.all { |r| r.load_tasks } self end def load_generators + initialize_generators super railties.all { |r| r.load_generators } self @@ -109,5 +92,24 @@ module Rails initializers += Finisher.initializers initializers end + + protected + + def initialize_tasks + require "rails/tasks" + task :environment do + $rails_rake_task = true + initialize! + end + end + + def initialize_generators + require "rails/generators" + end + + # Application is always reloadable when config.cache_classes is false. + def reloadable?(app) + true + end end end diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 39afac0604..b373b931f9 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -2,29 +2,23 @@ require 'active_support/core_ext/module/delegation' module Rails class Engine < Railtie + autoload :Configurable, "rails/engine/configurable" + class << self attr_accessor :called_from - delegate :middleware, :root, :paths, :to => :config - - def original_root - @original_root ||= find_root_with_file_flag("lib") - end - - def config - @config ||= Configuration.new(original_root) - end def inherited(base) - base.called_from = begin - call_stack = caller.map { |p| p.split(':').first } - File.dirname(call_stack.detect { |p| p !~ %r[railties/lib/rails|rack/lib/rack] }) + unless abstract_railtie?(base) + base.called_from = begin + call_stack = caller.map { |p| p.split(':').first } + File.dirname(call_stack.detect { |p| p !~ %r[railties/lib/rails|rack/lib/rack] }) + end end + super end - protected - - def find_root_with_file_flag(flag, default=nil) + def find_root_with_flag(flag, default=nil) root_path = self.called_from while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/#{flag}") @@ -42,14 +36,6 @@ module Rails delegate :middleware, :paths, :root, :to => :config - def config - self.class.config - end - - def reloadable?(app) - app.config.reload_plugins - end - def load_tasks super config.paths.lib.tasks.to_a.sort.each { |ext| load(ext) } @@ -113,5 +99,11 @@ module Rails end end end + + protected + + def reloadable?(app) + app.config.reload_plugins + end end end
\ No newline at end of file diff --git a/railties/lib/rails/engine/configurable.rb b/railties/lib/rails/engine/configurable.rb new file mode 100644 index 0000000000..fb420e8a12 --- /dev/null +++ b/railties/lib/rails/engine/configurable.rb @@ -0,0 +1,24 @@ +module Rails + class Engine + module Configurable + def self.included(base) + base.extend ClassMethods + base.delegate :middleware, :root, :paths, :to => :config + end + + module ClassMethods + def config + @config ||= Configuration.new(find_root_with_flag("lib")) + end + + def inherited(base) + raise "You cannot inherit from a Rails::Engine child" + end + end + + def config + self.class.config + end + end + end +end
\ No newline at end of file diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index b4fd503631..cb3fdc8501 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -1,27 +1,21 @@ module Rails class Plugin < Engine - class << self - def inherited(base) - raise "You should not inherit from Rails::Plugin" - end - - def config - raise "Plugins does not provide configuration at the class level" - end + def self.inherited(base) + raise "You cannot inherit from Rails::Plugin" + end - def all(list, paths) - plugins = [] - paths.each do |path| - Dir["#{path}/*"].each do |plugin_path| - plugin = new(plugin_path) - next unless list.include?(plugin.name) || list.include?(:all) - plugins << plugin - end + def self.all(list, paths) + plugins = [] + paths.each do |path| + Dir["#{path}/*"].each do |plugin_path| + plugin = new(plugin_path) + next unless list.include?(plugin.name) || list.include?(:all) + plugins << plugin end + end - plugins.sort_by do |p| - [list.index(p.name) || list.index(:all), p.name.to_s] - end + plugins.sort_by do |p| + [list.index(p.name) || list.index(:all), p.name.to_s] end end diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index 84f7a86946..208b017348 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -1,19 +1,21 @@ module Rails class Railtie + autoload :Configurable, "rails/railtie/configurable" + include Initializable ABSTRACT_RAILTIES = %w(Rails::Plugin Rails::Engine Rails::Application) class << self - attr_reader :subclasses - - def abstract_railtie?(base) - ABSTRACT_RAILTIES.include?(base.name) + def subclasses + @subclasses ||= [] end def inherited(base) - @subclasses ||= [] - @subclasses << base unless abstract_railtie?(base) + unless abstract_railtie?(base) + base.send(:include, self::Configurable) if add_configurable?(base) + subclasses << base + end end # TODO This should be called railtie_name and engine_name @@ -25,7 +27,7 @@ module Rails # TODO Deprecate me def plugins - @subclasses + subclasses end # TODO Deprecate me @@ -33,10 +35,6 @@ module Rails plugins.map { |p| p.plugin_name } end - def config - Configuration.default - end - def subscriber(subscriber) Rails::Subscriber.add(plugin_name, subscriber) end @@ -52,6 +50,20 @@ module Rails @generators << blk if blk @generators end + + protected + + def abstract_railtie?(base) + ABSTRACT_RAILTIES.include?(base.name) + end + + # 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 diff --git a/railties/lib/rails/railtie/configurable.rb b/railties/lib/rails/railtie/configurable.rb new file mode 100644 index 0000000000..3850efa4a7 --- /dev/null +++ b/railties/lib/rails/railtie/configurable.rb @@ -0,0 +1,23 @@ +module Rails + class Railtie + module Configurable + def self.included(base) + base.extend ClassMethods + end + + module ClassMethods + def config + @config ||= Configuration.new + end + + def inherited(base) + raise "You cannot inherit from a Rails::Railtie child" + end + end + + def config + self.class.config + end + end + end +end |