diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2014-08-03 15:48:14 -0700 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2014-08-03 15:48:14 -0700 |
commit | 611849772dd66c2e4d005dcfe153f7ce79a8a7db (patch) | |
tree | 11ea0257ad55a967bb749fd440530836c86ae066 | |
parent | e5632f37f797fd3cc6e2f793d7c73e97a55a8d73 (diff) | |
download | rails-611849772dd66c2e4d005dcfe153f7ce79a8a7db.tar.gz rails-611849772dd66c2e4d005dcfe153f7ce79a8a7db.tar.bz2 rails-611849772dd66c2e4d005dcfe153f7ce79a8a7db.zip |
Pull in the custom configuration concept from dhh/custom_configuration
-rw-r--r-- | guides/source/configuring.md | 21 | ||||
-rw-r--r-- | railties/CHANGELOG.md | 17 | ||||
-rw-r--r-- | railties/lib/rails/application/configuration.rb | 14 | ||||
-rw-r--r-- | railties/test/application/configuration/base_test.rb | 37 | ||||
-rw-r--r-- | railties/test/application/configuration/custom_test.rb | 15 |
5 files changed, 103 insertions, 1 deletions
diff --git a/guides/source/configuring.md b/guides/source/configuring.md index 6e897d1714..305adb0608 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -996,3 +996,24 @@ If you get the above error, you might want to increase the size of connection pool by incrementing the `pool` option in `database.yml` NOTE. If you are running in a multi-threaded environment, there could be a chance that several threads may be accessing multiple connections simultaneously. So depending on your current request load, you could very well have multiple threads contending for a limited amount of connections. + + +Custom configuration +-------------------- + +You can configure your own code through the Rails configuration object with custom configuration. It works like this: + + ```ruby + config.x.payment_processing.schedule = :daily + config.x.payment_processing.retries = 3 + config.x.super_debugger = true + ``` + +These configuration points are then available through the configuration object: + + ```ruby + Rails.configuration.x.payment_processing.schedule # => :daily + Rails.configuration.x.payment_processing.retries # => 3 + Rails.configuration.x.super_debugger # => true + Rails.configuration.x.super_debugger.not_set # => nil + ``` diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 651f40007e..dac554e015 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,20 @@ +* Pull in the custom configuration concept from dhh/custom_configuration, which allows you to + configure your own code through the Rails configuration object with custom configuration: + + # config/environments/production.rb + config.x.payment_processing.schedule = :daily + config.x.payment_processing.retries = 3 + config.x.super_debugger = true + + These configuration points are then available through the configuration object: + + Rails.configuration.x.payment_processing.schedule # => :daily + Rails.configuration.x.payment_processing.retries # => 3 + Rails.configuration.x.super_debugger # => true + Rails.configuration.x.super_debugger.not_set # => nil + + *DHH* + * Scaffold generator `_form` partial adds `class="field"` for password confirmation fields. diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 5e8f4de847..782bc4b0f1 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -13,7 +13,7 @@ module Rails :railties_order, :relative_url_root, :secret_key_base, :secret_token, :serve_static_assets, :ssl_options, :static_cache_control, :session_options, :time_zone, :reload_classes_only_on_change, - :beginning_of_week, :filter_redirect + :beginning_of_week, :filter_redirect, :x attr_writer :log_level attr_reader :encoding @@ -48,6 +48,7 @@ module Rails @eager_load = nil @secret_token = nil @secret_key_base = nil + @x = Custom.new @assets = ActiveSupport::OrderedOptions.new @assets.enabled = true @@ -154,6 +155,17 @@ module Rails def annotations SourceAnnotationExtractor::Annotation end + + private + class Custom + def initialize + @configurations = Hash.new + end + + def method_missing(method, *args) + @configurations[method] ||= ActiveSupport::OrderedOptions.new + end + end end end end diff --git a/railties/test/application/configuration/base_test.rb b/railties/test/application/configuration/base_test.rb new file mode 100644 index 0000000000..d6a82b139d --- /dev/null +++ b/railties/test/application/configuration/base_test.rb @@ -0,0 +1,37 @@ +require 'isolation/abstract_unit' +require 'rack/test' +require 'env_helpers' + +module ApplicationTests + module ConfigurationTests + class BaseTest < ActiveSupport::TestCase + def setup + build_app + boot_rails + FileUtils.rm_rf("#{app_path}/config/environments") + end + + def teardown + teardown_app + FileUtils.rm_rf(new_app) if File.directory?(new_app) + end + + private + def new_app + File.expand_path("#{app_path}/../new_app") + end + + def copy_app + FileUtils.cp_r(app_path, new_app) + end + + def app + @app ||= Rails.application + end + + def require_environment + require "#{app_path}/config/environment" + end + end + end +end
\ No newline at end of file diff --git a/railties/test/application/configuration/custom_test.rb b/railties/test/application/configuration/custom_test.rb new file mode 100644 index 0000000000..045537fc28 --- /dev/null +++ b/railties/test/application/configuration/custom_test.rb @@ -0,0 +1,15 @@ +require 'application/configuration/base_test' + +class ApplicationTests::ConfigurationTests::CustomTest < ApplicationTests::ConfigurationTests::BaseTest + test 'access custom configuration point' do + add_to_config <<-RUBY + config.x.resque.inline_jobs = :always + config.x.resque.timeout = 60 + RUBY + require_environment + + assert_equal :always, Rails.configuration.x.resque.inline_jobs + assert_equal 60, Rails.configuration.x.resque.timeout + assert_nil Rails.configuration.x.resque.nothing + end +end |