diff options
author | Yehuda Katz <wycats@Yehuda-Katz.local> | 2009-12-27 14:36:59 -0800 |
---|---|---|
committer | Yehuda Katz <wycats@Yehuda-Katz.local> | 2009-12-27 14:36:59 -0800 |
commit | 12e43494a748e0144195be12dc19161cc3e4d39f (patch) | |
tree | c06f88b7ddf6e41205838cc6f4edd2257a801dcb /railties | |
parent | 1c26ba486c23f229a12fea6ccad33e6cb3122b91 (diff) | |
parent | 97db79ab3c0af7b6805dcaee99384d96ccb3567d (diff) | |
download | rails-12e43494a748e0144195be12dc19161cc3e4d39f.tar.gz rails-12e43494a748e0144195be12dc19161cc3e4d39f.tar.bz2 rails-12e43494a748e0144195be12dc19161cc3e4d39f.zip |
Merge remote branch 'jose/perf'
Diffstat (limited to 'railties')
-rw-r--r-- | railties/lib/rails/configuration.rb | 20 | ||||
-rw-r--r-- | railties/lib/rails/plugin.rb | 18 | ||||
-rw-r--r-- | railties/test/plugins/configuration_test.rb | 16 |
3 files changed, 53 insertions, 1 deletions
diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 086f67a419..cb321536d2 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -1,6 +1,24 @@ require 'active_support/ordered_options' module Rails + # Create a Plugin::Options from ActiveSuppot::OrderedOptions, + # which support the following syntax: + # + # controller.action_controller.include FooBar + # + class Plugin::Options < ActiveSupport::OrderedOptions #:nodoc: + attr_reader :includes + + def initialize(*args) + @includes = [] + super + end + + def include(*args) + @includes.concat(args) + end + end + # Temporarily separate the plugin configuration class from the main # configuration class while this bit is being cleaned up. class Plugin::Configuration @@ -16,7 +34,7 @@ module Rails @options = base.options.dup @middleware = base.middleware.dup else - @options = Hash.new { |h,k| h[k] = ActiveSupport::OrderedOptions.new } + @options = Hash.new { |h,k| h[k] = Rails::Plugin::Options.new } @middleware = ActionDispatch::MiddlewareStack.new end end diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index 0699affea7..aa325c6f76 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -25,6 +25,24 @@ module Rails Configuration.default end + # Creates an initializer which includes all given modules to the given class. + # + # module Rails + # class ActionController < Rails::Plugin + # plugin_name :action_controller + # include_modules_in "ActionController::Base" + # end + # end + # + def self.include_modules_in(klass, from=plugin_name) + self.initializer :"#{from}.include_modules" do |app| + klass = klass.constantize if klass.is_a?(String) + app.config.send(from).includes.each do |mod| + klass.send(:include, mod.is_a?(String) ? mod.constantize : mod) + end + end + end + class Vendored < Plugin def self.all(list, paths) plugins = [] diff --git a/railties/test/plugins/configuration_test.rb b/railties/test/plugins/configuration_test.rb index 5786316d1d..0843d05577 100644 --- a/railties/test/plugins/configuration_test.rb +++ b/railties/test/plugins/configuration_test.rb @@ -8,6 +8,10 @@ module PluginsTest require "rails" end + module Bar; end + module Baz; end + module All; end + test "config is available to plugins" do class Foo < Rails::Plugin ; end assert_nil Foo.config.action_controller.foo @@ -24,6 +28,18 @@ module PluginsTest assert_equal "hello", AppTemplate::Application.config.foo.greetings end + test "plugin configurations allow modules to be given" do + class Foo < Rails::Plugin ; config.foo.include(Bar, Baz) ; end + assert_equal [Bar, Baz], Foo.config.foo.includes + end + + test "plugin includes given modules in given class" do + class Foo < Rails::Plugin ; config.foo.include(Bar, "PluginsTest::ConfigurationTest::Baz") ; include_modules_in All ; end + Foo.new.run_initializers(Foo) + assert All.ancestors.include?(Bar) + assert All.ancestors.include?(Baz) + end + test "plugin config merges are deep" do class Foo < Rails::Plugin ; config.foo.greetings = 'hello' ; end class MyApp < Rails::Application |