Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Load plugin initializers from lib/rails/initializers/*.rb

  • Loading branch information...
commit e47a1d7865f40ae70a4c38e7ea792843641f3bcb 1 parent b9f551a
@josh josh authored
View
19 railties/lib/rails/plugin.rb
@@ -47,13 +47,20 @@ def config
end
initializer :load_init_rb, :before => :load_application_initializers do |app|
- files = %w(rails/init.rb init.rb).map { |path| File.expand_path path, root }
- if initrb = files.find { |path| File.file? path }
- if initrb == files.first
- ActiveSupport::Deprecation.warn "Use toplevel init.rb; rails/init.rb is deprecated: #{initrb}"
+ initializers = Dir["#{root}/lib/rails/initializers/*.rb"]
+
+ 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
- config = app.config
- eval(File.read(initrb), binding, initrb)
end
end
View
33 railties/test/railties/plugin_test.rb
@@ -94,6 +94,39 @@ class Engine < Rails::Engine
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
+ RUBY
+
+ @plugin.write "init.rb", <<-RUBY
+ raise "b00m"
+ RUBY
+
+ boot_rails
+ assert $loaded
+ end
+
test "loads deprecated rails/init.rb" do
@plugin.write "rails/init.rb", <<-RUBY
$loaded = true
Please sign in to comment.
Something went wrong with that request. Please try again.