Skip to content

Commit

Permalink
Merge pull request #16129 from rafaelfranca/config_for
Browse files Browse the repository at this point in the history
Add Rails::Application#config_for
  • Loading branch information
rafaelfranca committed Jul 15, 2014
2 parents dc0d303 + 9629dea commit 1fbb5c1
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
18 changes: 18 additions & 0 deletions railties/CHANGELOG.md
@@ -1,3 +1,21 @@
* Add `Rails::Application.config_for` to load a configuration for the current
environment.

# config/exception_notification.yml:
production:
url: http://127.0.0.1:8080
namespace: my_app_production
development:
url: http://localhost:3001
namespace: my_app_development

# config/production.rb
MyApp::Application.configure do
config.middleware.use ExceptionNotifier, config_for(:exception_notification)
end

*Rafael Mendonça França*, *DHH*

* Deprecate `Rails::Rack::LogTailer` without replacement.

*Rafael Mendonça França*
Expand Down
32 changes: 32 additions & 0 deletions railties/lib/rails/application.rb
Expand Up @@ -187,6 +187,38 @@ def message_verifier(verifier_name)
end
end

# Convenience for loading config/foo.yml for the current Rails env.
#
# Example:
#
# # config/exception_notification.yml:
# production:
# url: http://127.0.0.1:8080
# namespace: my_app_production
# development:
# url: http://localhost:3001
# namespace: my_app_development
#
# # config/production.rb
# MyApp::Application.configure do
# config.middleware.use ExceptionNotifier, config_for(:exception_notification)
# end
def config_for(name)
yaml = Pathname.new("#{paths["config"].existent.first}/#{name}.yml")

if yaml.exist?
require "yaml"
require "erb"
(YAML.load(ERB.new(yaml.read).result) || {})[Rails.env] || {}
else
raise "Could not load configuration. No such file - #{yaml}"
end
rescue Psych::SyntaxError => e
raise "YAML syntax error occurred while parsing #{yaml}. " \
"Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
"Error: #{e.message}"
end

# Stores some of the Rails initial environment parameters which
# will be used by middlewares and engines to configure themselves.
def env_config
Expand Down
84 changes: 84 additions & 0 deletions railties/test/application/configuration_test.rb
Expand Up @@ -989,5 +989,89 @@ def index

assert_equal Rails.application.config.action_mailer.show_previews, true
end

test "config_for loads custom configuration from yaml files" do
app_file 'config/custom.yml', <<-RUBY
development:
key: 'custom key'
RUBY

add_to_config <<-RUBY
config.my_custom_config = config_for('custom')
RUBY

require "#{app_path}/config/environment"

assert_equal 'custom key', Rails.application.config.my_custom_config['key']
end

test "config_for raises an exception if the file does not exist" do
add_to_config <<-RUBY
config.my_custom_config = config_for('custom')
RUBY

exception = assert_raises(RuntimeError) do
require "#{app_path}/config/environment"
end

assert_equal "Could not load configuration. No such file - #{app_path}/config/custom.yml", exception.message
end

test "config_for without the environment configured returns an empty hash" do
app_file 'config/custom.yml', <<-RUBY
test:
key: 'custom key'
RUBY

add_to_config <<-RUBY
config.my_custom_config = config_for('custom')
RUBY
require "#{app_path}/config/environment"

assert_equal({}, Rails.application.config.my_custom_config)
end

test "config_for with empty file returns an empty hash" do
app_file 'config/custom.yml', <<-RUBY
RUBY

add_to_config <<-RUBY
config.my_custom_config = config_for('custom')
RUBY
require "#{app_path}/config/environment"

assert_equal({}, Rails.application.config.my_custom_config)
end

test "config_for containing ERB tags should evaluate" do
app_file 'config/custom.yml', <<-RUBY
development:
key: <%= 'custom key' %>
RUBY

add_to_config <<-RUBY
config.my_custom_config = config_for('custom')
RUBY
require "#{app_path}/config/environment"

assert_equal 'custom key', Rails.application.config.my_custom_config['key']
end

test "config_for with syntax error show a more descritive exception" do
app_file 'config/custom.yml', <<-RUBY
development:
key: foo:
RUBY

add_to_config <<-RUBY
config.my_custom_config = config_for('custom')
RUBY

exception = assert_raises(RuntimeError) do
require "#{app_path}/config/environment"
end

assert_match 'YAML syntax error occurred while parsing', exception.message
end
end
end

0 comments on commit 1fbb5c1

Please sign in to comment.