aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-03-23 23:50:24 +0100
committerJosé Valim <jose.valim@gmail.com>2010-03-23 23:50:53 +0100
commit5c41cb77d9af28fe6495d5c4047483ce3ceadfe6 (patch)
treecc9ab8088b2d3b05d372db80bb77d79c8f52b32e
parentcc0e402aa870fa710d6b6189dc090244b4462308 (diff)
downloadrails-5c41cb77d9af28fe6495d5c4047483ce3ceadfe6.tar.gz
rails-5c41cb77d9af28fe6495d5c4047483ce3ceadfe6.tar.bz2
rails-5c41cb77d9af28fe6495d5c4047483ce3ceadfe6.zip
Ensure lib/rails/initializers/*.rb also works for gems.
-rw-r--r--railties/lib/rails/engine.rb7
-rw-r--r--railties/lib/rails/engine/configuration.rb36
-rw-r--r--railties/lib/rails/plugin.rb19
-rw-r--r--railties/test/railties/plugin_test.rb20
-rw-r--r--railties/test/railties/shared_tests.rb10
5 files changed, 42 insertions, 50 deletions
diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb
index 85b4ff8470..987573ea03 100644
--- a/railties/lib/rails/engine.rb
+++ b/railties/lib/rails/engine.rb
@@ -109,6 +109,13 @@ module Rails
config.generators.templates.unshift(*paths.lib.templates.to_a)
end
+ initializer :load_lib_initializers do |app|
+ paths.lib.rails.initializers.to_a.sort.each do |initializer|
+ config = app.config
+ eval(File.read(initializer), binding, initializer)
+ end
+ end
+
initializer :load_application_initializers do
paths.config.initializers.to_a.sort.each do |initializer|
load(initializer)
diff --git a/railties/lib/rails/engine/configuration.rb b/railties/lib/rails/engine/configuration.rb
index b8f1f1009c..1996b2fb90 100644
--- a/railties/lib/rails/engine/configuration.rb
+++ b/railties/lib/rails/engine/configuration.rb
@@ -14,23 +14,25 @@ module Rails
def paths
@paths ||= begin
paths = Rails::Paths::Root.new(@root)
- paths.app "app", :eager_load => true, :glob => "*"
- paths.app.controllers "app/controllers", :eager_load => true
- paths.app.helpers "app/helpers", :eager_load => true
- paths.app.models "app/models", :eager_load => true
- paths.app.mailers "app/mailers", :eager_load => true
- paths.app.metals "app/metal", :eager_load => true
- paths.app.views "app/views", :eager_load => true
- paths.lib "lib", :load_path => true
- paths.lib.tasks "lib/tasks", :glob => "**/*.rake"
- paths.lib.templates "lib/templates"
- paths.config "config"
- paths.config.initializers "config/initializers", :glob => "**/*.rb"
- paths.config.locales "config/locales", :glob => "*.{rb,yml}"
- paths.config.routes "config/routes.rb"
- paths.public "public"
- paths.public.javascripts "public/javascripts"
- paths.public.stylesheets "public/stylesheets"
+ paths.app "app", :eager_load => true, :glob => "*"
+ paths.app.controllers "app/controllers", :eager_load => true
+ paths.app.helpers "app/helpers", :eager_load => true
+ paths.app.models "app/models", :eager_load => true
+ paths.app.mailers "app/mailers", :eager_load => true
+ paths.app.metals "app/metal", :eager_load => true
+ paths.app.views "app/views", :eager_load => true
+ paths.lib "lib", :load_path => true
+ paths.lib.rails "lib/rails", :glob => "**/*.rake"
+ paths.lib.rails.initializers "lib/rails/initializers", :glob => "**/*.rb"
+ paths.lib.tasks "lib/tasks", :glob => "**/*.rake"
+ paths.lib.templates "lib/templates"
+ paths.config "config"
+ paths.config.initializers "config/initializers", :glob => "**/*.rb"
+ paths.config.locales "config/locales", :glob => "*.{rb,yml}"
+ paths.config.routes "config/routes.rb"
+ paths.public "public"
+ paths.public.javascripts "public/javascripts"
+ paths.public.stylesheets "public/stylesheets"
paths
end
end
diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb
index 5d6144959d..a08c5b9143 100644
--- a/railties/lib/rails/plugin.rb
+++ b/railties/lib/rails/plugin.rb
@@ -47,20 +47,13 @@ module Rails
end
initializer :load_init_rb, :before => :load_application_initializers do |app|
- initializers = Dir["#{root}/lib/rails/initializers/*.rb"]
+ lib_initializers = paths.lib.rails.initializers.to_a
+ files = %w(rails/init.rb init.rb).map { |path| File.expand_path(path, root) }
- if initializers.any?
- initializers.each do |initializer|
- config = app.config
- eval(File.read(initializer), binding, initializer)
- end
- else
- files = %w(rails/init.rb init.rb).map { |path| File.expand_path path, root }
- if initrb = files.find { |path| File.file? path }
- ActiveSupport::Deprecation.warn "init.rb is deprecated: #{initrb}. Use lib/rails/initializers/#{name}.rb"
- config = app.config
- eval(File.read(initrb), binding, initrb)
- end
+ if lib_initializers.empty? && initrb = files.find { |path| File.file?(path) }
+ ActiveSupport::Deprecation.warn "init.rb is deprecated: #{initrb}. Use lib/rails/initializers/#{name}.rb"
+ config = app.config
+ eval(File.read(initrb), binding, initrb)
end
end
diff --git a/railties/test/railties/plugin_test.rb b/railties/test/railties/plugin_test.rb
index 4ea5460a68..6081890fcb 100644
--- a/railties/test/railties/plugin_test.rb
+++ b/railties/test/railties/plugin_test.rb
@@ -94,26 +94,6 @@ module RailtiesTest
assert rescued, "Expected boot rails to fail"
end
- test "loads lib/rails/initializers/foo.rb" do
- @plugin.write "lib/rails/initializers/foo.rb", <<-RUBY
- $loaded = true
- RUBY
-
- boot_rails
- assert $loaded
- end
-
- test "loads vendored rails/initializers/foo thats already in the $LOAD_PATH" do
- $: << "#{app_path}/vendor/foo/lib"
-
- app_file "vendor/foo/lib/rails/initializers/foo.rb", <<-RUBY
- $loaded = true
- RUBY
-
- boot_rails
- assert $loaded
- end
-
test "skips init.rb if initializer is present" do
@plugin.write "lib/rails/initializers/foo.rb", <<-RUBY
$loaded = true
diff --git a/railties/test/railties/shared_tests.rb b/railties/test/railties/shared_tests.rb
index 83d25be5db..48539c2e99 100644
--- a/railties/test/railties/shared_tests.rb
+++ b/railties/test/railties/shared_tests.rb
@@ -297,6 +297,16 @@ YAML
assert $plugin_initializer
end
+ def test_rails_plugin_initializers
+ $plugin_initializer = false
+ @plugin.write "lib/rails/initializers/foo.rb", <<-RUBY
+ $plugin_initializer = true
+ RUBY
+
+ boot_rails
+ assert $plugin_initializer
+ end
+
def test_plugin_midleware_referenced_in_configuration
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits