diff options
author | Carl Lerche <carllerche@mac.com> | 2009-12-15 22:07:12 -0800 |
---|---|---|
committer | Carl Lerche <carllerche@mac.com> | 2009-12-15 22:13:52 -0800 |
commit | 1b27f5c4f72384081083fa025b6b10e5ab02ae5e (patch) | |
tree | 43bbf10ed39c70f7937709de8b0805002f93eca5 /railties/lib | |
parent | f0bbc647c2086e9536c9d2b4ea7c4c18fe2edd3e (diff) | |
download | rails-1b27f5c4f72384081083fa025b6b10e5ab02ae5e.tar.gz rails-1b27f5c4f72384081083fa025b6b10e5ab02ae5e.tar.bz2 rails-1b27f5c4f72384081083fa025b6b10e5ab02ae5e.zip |
Add the ability for plugins to set default configuration settings.
Diffstat (limited to 'railties/lib')
-rw-r--r-- | railties/lib/rails.rb | 5 | ||||
-rw-r--r-- | railties/lib/rails/application.rb | 8 | ||||
-rw-r--r-- | railties/lib/rails/configuration.rb | 68 | ||||
-rw-r--r-- | railties/lib/rails/plugin.rb | 21 |
4 files changed, 84 insertions, 18 deletions
diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index b7cae9a9ac..85aeb4af24 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -3,9 +3,11 @@ require "pathname" require 'active_support' require 'active_support/core_ext/kernel/reporting' require 'active_support/core_ext/logger' +require 'action_dispatch' require 'rails/initializable' require 'rails/application' +require 'rails/plugin' require 'rails/railties_path' require 'rails/version' require 'rails/rack' @@ -14,7 +16,6 @@ require 'rails/core' require 'rails/configuration' require 'rails/deprecation' require 'rails/initializer' -require 'rails/plugin' require 'rails/ruby_version_check' # For Ruby 1.8, this initialization sets $KCODE to 'u' to enable the @@ -29,4 +30,4 @@ else Encoding.default_external = Encoding::UTF_8 end -RAILS_ENV = (ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development").dup unless defined?(RAILS_ENV)
\ No newline at end of file +RAILS_ENV = (ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development").dup unless defined?(RAILS_ENV) diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index bc74ac8646..9e57354047 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -13,7 +13,11 @@ module Rails end def config - @config ||= Configuration.new + @config ||= begin + config = Configuration.new + Plugin.plugins.each { |p| config.merge(p.config) } + config + end end # TODO: change the plugin loader to use config @@ -104,6 +108,8 @@ module Rails def plugins @plugins ||= begin + plugin_names = config.plugins || [:all] + Plugin.plugins.select { |p| plugin_names.include?(p.plugin_name) } + Plugin::Vendored.all(config.plugins || [:all], config.paths.vendor.plugins) end end diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 0449fe4ce5..0fa42091dd 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -1,10 +1,54 @@ require 'active_support/ordered_options' module Rails - class Configuration - attr_accessor :action_controller, :action_mailer, :action_view, - :active_record, :active_resource, :active_support, - :after_initialize_blocks, :cache_classes, + # Temporarily separate the plugin configuration class from the main + # configuration class while this bit is being cleaned up. + class Plugin::Configuration + + def initialize + @options = Hash.new { |h,k| h[k] = ActiveSupport::OrderedOptions.new } + end + + def middleware + @middleware ||= ActionDispatch::MiddlewareStack.new + end + + def respond_to?(name) + super || name.to_s =~ config_key_regexp + end + + def merge(config) + @options = config.options.merge(@options) + end + + protected + + attr_reader :options + + private + + def method_missing(name, *args, &blk) + if name.to_s =~ config_key_regexp + return $2 == '=' ? @options[$1] = args.first : @options[$1] + end + + super + end + + def config_key_regexp + bits = config_keys.map { |n| Regexp.escape(n.to_s) }.join('|') + /^(#{bits})(?:=)?$/ + end + + def config_keys + ([ :active_support, :active_record, :action_controller, + :action_view, :action_mailer, :active_resource ] + + Plugin.plugin_names).map { |n| n.to_s }.uniq + end + end + + class Configuration < Plugin::Configuration + attr_accessor :after_initialize_blocks, :cache_classes, :consider_all_requests_local, :dependency_loading, :gems, :load_once_paths, :logger, :metals, :plugins, :preload_frameworks, :reload_plugins, :serve_static_assets, @@ -17,15 +61,11 @@ module Rails :view_path def initialize + super @load_once_paths = [] @after_initialize_blocks = [] @dependency_loading = true @serve_static_assets = true - - for framework in frameworks - self.send("#{framework}=", ActiveSupport::OrderedOptions.new) - end - self.active_support = ActiveSupport::OrderedOptions.new end def after_initialize(&blk) @@ -84,7 +124,10 @@ module Rails self.preload_frameworks = true self.cache_classes = true self.dependency_loading = false - self.action_controller.allow_concurrency = true + + if respond_to?(:action_controller) + action_controller.allow_concurrency = true + end self end @@ -103,11 +146,6 @@ module Rails defined?(::RAILS_FRAMEWORK_ROOT) ? ::RAILS_FRAMEWORK_ROOT : "#{root}/vendor/rails" end - def middleware - require 'action_dispatch' - @middleware ||= ActionDispatch::MiddlewareStack.new - end - # Loads and returns the contents of the #database_configuration_file. The # contents of the file are processed via ERB before being sent through # YAML::load. diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index 8025135a64..90dc1ad8dd 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -2,6 +2,27 @@ module Rails class Plugin include Initializable + def self.plugin_name + @plugin_name || name.demodulize.underscore + end + + def self.inherited(klass) + @plugins ||= [] + @plugins << klass unless klass == Vendored + end + + def self.plugins + @plugins + end + + def self.plugin_names + plugins.map { |p| p.plugin_name } + end + + def self.config + @config ||= Configuration.new + end + class Vendored < Plugin def self.all(list, paths) plugins = [] |