Skip to content
Browse files

Revert "Improve custom configuration"

This reverts commit de48913.

Conflicts:
	railties/lib/rails/railtie/configuration.rb

It added regression. Will be back after the beta
  • Loading branch information...
1 parent 867631f commit 43073b393d234acd094ac7c220163f4e419d11f5 @rafaelfranca rafaelfranca committed
View
18 guides/source/4_2_release_notes.md
@@ -78,24 +78,22 @@ Please refer to the [Changelog][railties] for detailed changes.
* Introduced an `after_bundle` callback for use in Rails templates.
([Pull Request](https://github.com/rails/rails/pull/16359))
-* Custom configuration options can be chained:
+* Introduced the `x` namespace for defining custom configuration options:
```ruby
# config/environments/production.rb
- config.payment_processing.schedule = :daily
- config.payment_processing.retries = 3
- config.resque = { timeout: 60, inline_jobs: :always }
- config.super_debugger = true
+ config.x.payment_processing.schedule = :daily
+ config.x.payment_processing.retries = 3
+ config.x.super_debugger = true
```
These options are then available through the configuration object:
```ruby
- Rails.configuration.payment_processing.schedule # => :daily
- Rails.configuration.payment_processing.retries # => 3
- Rails.configuration.resque.timeout # => 60
- Rails.configuration.resque.inline_jobs # => :always
- Rails.configuration.super_debugger # => true
+ 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
```
([Commit](https://github.com/rails/rails/commit/611849772dd66c2e4d005dcfe153f7ce79a8a7db))
View
16 guides/source/configuring.md
@@ -1006,18 +1006,16 @@ Custom configuration
You can configure your own code through the Rails configuration object with custom configuration. It works like this:
```ruby
- config.payment_processing.schedule = :daily
- config.payment_processing.retries = 3
- config.resque = { timeout: 60, inline_jobs: :always }
- config.super_debugger = true
+ 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.payment_processing.schedule # => :daily
- Rails.configuration.payment_processing.retries # => 3
- Rails.configuration.resque.timeout # => 60
- Rails.configuration.resque.inline_jobs # => :always
- Rails.configuration.super_debugger # => true
+ 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
```
View
16 railties/CHANGELOG.md
@@ -20,18 +20,16 @@
configure your own code through the Rails configuration object with custom configuration:
# config/environments/production.rb
- config.payment_processing.schedule = :daily
- config.payment_processing.retries = 3
- config.resque = { timeout: 60, inline_jobs: :always }
- config.super_debugger = true
+ 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.payment_processing.schedule # => :daily
- Rails.configuration.payment_processing.retries # => 3
- Rails.configuration.resque.timeout # => 60
- Rails.configuration.resque.inline_jobs # => :always
- Rails.configuration.super_debugger # => true
+ 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*
View
14 railties/lib/rails/application/configuration.rb
@@ -13,7 +13,7 @@ class Configuration < ::Rails::Engine::Configuration
: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 @@ def initialize(*)
@eager_load = nil
@secret_token = nil
@secret_key_base = nil
+ @x = Custom.new
@assets = ActiveSupport::OrderedOptions.new
@assets.enabled = true
@@ -154,6 +155,17 @@ def session_store(*args)
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
View
38 railties/lib/rails/railtie/configuration.rb
@@ -88,45 +88,11 @@ def respond_to?(name, include_private = false)
def method_missing(name, *args, &blk)
if name.to_s =~ /=$/
- key = $`.to_sym
- value = args.first
-
- if value.is_a?(Hash)
- @@options[key] = ChainedConfigurationOptions.new value
- else
- @@options[key] = value
- end
+ @@options[$`.to_sym] = args.first
elsif @@options.key?(name)
@@options[name]
else
- @@options[name] = ActiveSupport::OrderedOptions.new
- end
- end
-
- class ChainedConfigurationOptions < ActiveSupport::OrderedOptions # :nodoc:
- def initialize(value)
- value.each_pair { |k, v| set_value k, v }
- end
-
- def method_missing(meth, *args)
- if meth =~ /=$/
- key = $`.to_sym
- value = args.first
-
- set_value key, value
- else
- self.fetch(meth) { super }
- end
- end
-
- private
-
- def set_value(key, value)
- if value.is_a?(Hash)
- value = self.class.new(value)
- end
-
- self[key] = value
+ super
end
end
end
View
6 railties/test/application/configuration/base_test.rb
@@ -5,8 +5,6 @@
module ApplicationTests
module ConfigurationTests
class BaseTest < ActiveSupport::TestCase
- include ActiveSupport::Testing::Isolation
-
def setup
build_app
boot_rails
@@ -32,8 +30,8 @@ def app
end
def require_environment
- require "#{app_path}/config/environment"
+ require "#{app_path}/config/environment"
end
end
end
-end
+end
View
81 railties/test/application/configuration/custom_test.rb
@@ -1,84 +1,15 @@
require 'application/configuration/base_test'
class ApplicationTests::ConfigurationTests::CustomTest < ApplicationTests::ConfigurationTests::BaseTest
- test 'configuration top level can be chained' do
+ test 'access custom configuration point' do
add_to_config <<-RUBY
- config.resque.inline_jobs = :always
- config.resque.timeout = 60
+ config.x.resque.inline_jobs = :always
+ config.x.resque.timeout = 60
RUBY
require_environment
- assert_equal :always, Rails.configuration.resque.inline_jobs
- assert_equal 60, Rails.configuration.resque.timeout
- assert_nil Rails.configuration.resque.nothing
- end
-
- test 'configuration top level accept normal values' do
- add_to_config <<-RUBY
- config.timeout = 60
- config.something_nil = nil
- config.something_false = false
- config.something_true = true
- RUBY
- require_environment
-
- assert_equal 60, Rails.configuration.timeout
- assert_equal nil, Rails.configuration.something_nil
- assert_equal false, Rails.configuration.something_false
- assert_equal true, Rails.configuration.something_true
- end
-
- test 'configuration top level builds options from hashes' do
- add_to_config <<-RUBY
- config.resque = { timeout: 60, inline_jobs: :always }
- RUBY
- require_environment
-
- assert_equal :always, Rails.configuration.resque.inline_jobs
- assert_equal 60, Rails.configuration.resque.timeout
- assert_nil Rails.configuration.resque.nothing
- end
-
- test 'configuration top level builds options from hashes with string keys' do
- add_to_config <<-RUBY
- config.resque = { 'timeout' => 60, 'inline_jobs' => :always }
- RUBY
- require_environment
-
- assert_equal :always, Rails.configuration.resque.inline_jobs
- assert_equal 60, Rails.configuration.resque.timeout
- assert_nil Rails.configuration.resque.nothing
- end
-
- test 'configuration top level builds nested options from hashes with symbol keys' do
- add_to_config <<-RUBY
- config.resque = { timeout: 60, inline_jobs: :always, url: { host: 'localhost', port: 8080 } }
- config.resque.url.protocol = 'https'
- config.resque.queues = { production: ['low_priority'] }
- RUBY
- require_environment
-
- assert_equal(:always, Rails.configuration.resque.inline_jobs)
- assert_equal(60, Rails.configuration.resque.timeout)
- assert_equal({ host: 'localhost', port: 8080, protocol: 'https' }, Rails.configuration.resque.url)
- assert_equal('localhost', Rails.configuration.resque.url.host)
- assert_equal(8080, Rails.configuration.resque.url.port)
- assert_equal('https', Rails.configuration.resque.url.protocol)
- assert_equal(['low_priority'], Rails.configuration.resque.queues.production)
- assert_nil(Rails.configuration.resque.nothing)
- end
-
- test 'configuration top level builds nested options from hashes with string keys' do
- add_to_config <<-RUBY
- config.resque = { 'timeout' => 60, 'inline_jobs' => :always, 'url' => { 'host' => 'localhost', 'port' => 8080 } }
- RUBY
- require_environment
-
- assert_equal(:always, Rails.configuration.resque.inline_jobs)
- assert_equal(60, Rails.configuration.resque.timeout)
- assert_equal({ host: 'localhost', port: 8080 }, Rails.configuration.resque.url)
- assert_equal('localhost', Rails.configuration.resque.url.host)
- assert_equal(8080, Rails.configuration.resque.url.port)
- assert_nil(Rails.configuration.resque.nothing)
+ 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
View
2 railties/test/railties/engine_test.rb
@@ -840,7 +840,7 @@ def index
Rails.application.load_seed
assert Rails.application.config.app_seeds_loaded
- assert_empty Bukkits::Engine.config.bukkits_seeds_loaded
+ assert_raise(NoMethodError) { Bukkits::Engine.config.bukkits_seeds_loaded }
Bukkits::Engine.load_seed
assert Bukkits::Engine.config.bukkits_seeds_loaded

2 comments on commit 43073b3

@pixeltrix
Ruby on Rails member

@rafaelfranca was the plan to bring this back aborted for some reason or just overlooked?

@rafaelfranca
Ruby on Rails member

My plan was to bring it back but after some discussion at campfire I decided to leave the x namespace since it make explicit that the configuration is a custom object of the application

Please sign in to comment.
Something went wrong with that request. Please try again.