aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/rails.rb5
-rw-r--r--railties/lib/rails/application.rb8
-rw-r--r--railties/lib/rails/configuration.rb68
-rw-r--r--railties/lib/rails/plugin.rb21
-rw-r--r--railties/test/application/configuration_test.rb21
-rw-r--r--railties/test/application/initializer_test.rb1
-rw-r--r--railties/test/plugins/configuration_test.rb36
7 files changed, 142 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 = []
diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb
index 1fcab8c651..89337b7f66 100644
--- a/railties/test/application/configuration_test.rb
+++ b/railties/test/application/configuration_test.rb
@@ -53,5 +53,26 @@ module ApplicationTests
assert_raises(NoMethodError) { 1.day }
end
end
+
+ test "marking the application as threadsafe sets the correct config variables" do
+ add_to_config <<-RUBY
+ config.threadsafe!
+ RUBY
+
+ require "#{app_path}/config/application"
+ assert AppTemplate.configuration.action_controller.allow_concurrency
+ end
+
+ test "the application can be marked as threadsafe when there are no frameworks" do
+ FileUtils.rm_rf("#{app_path}/config/environments")
+ add_to_config <<-RUBY
+ config.frameworks = []
+ config.threadsafe!
+ RUBY
+
+ assert_nothing_raised do
+ require "#{app_path}/config/application"
+ end
+ end
end
end
diff --git a/railties/test/application/initializer_test.rb b/railties/test/application/initializer_test.rb
index 56582c5772..fa00d287ca 100644
--- a/railties/test/application/initializer_test.rb
+++ b/railties/test/application/initializer_test.rb
@@ -6,6 +6,7 @@ module ApplicationTests
def setup
build_app
+ FileUtils.rm_rf("#{app_path}/config/environments")
boot_rails
require "rails"
end
diff --git a/railties/test/plugins/configuration_test.rb b/railties/test/plugins/configuration_test.rb
new file mode 100644
index 0000000000..edf8bb37f5
--- /dev/null
+++ b/railties/test/plugins/configuration_test.rb
@@ -0,0 +1,36 @@
+require "isolation/abstract_unit"
+
+module PluginsTest
+ class ConfigurationTest < Test::Unit::TestCase
+ def setup
+ build_app
+ boot_rails
+ require "rails"
+ end
+
+ test "config is available to plugins" do
+ class Foo < Rails::Plugin ; end
+ assert_nil Foo.config.action_controller.foo
+ end
+
+ test "a config name is available for the plugin" do
+ class Foo < Rails::Plugin ; config.foo.greetings = "hello" ; end
+ assert_equal "hello", Foo.config.foo.greetings
+ end
+
+ test "plugin configurations are available in the application" do
+ class Foo < Rails::Plugin ; config.foo.greetings = "hello" ; end
+ require "#{app_path}/config/application"
+ assert_equal "hello", AppTemplate.config.foo.greetings
+ end
+
+ test "plugin config merges are deep" do
+ class Foo < Rails::Plugin ; config.foo.greetings = 'hello' ; end
+ class MyApp < Rails::Application
+ config.foo.bar = "bar"
+ end
+ assert_equal "hello", MyApp.config.foo.greetings
+ assert_equal "bar", MyApp.config.foo.bar
+ end
+ end
+end \ No newline at end of file