Skip to content

Commit

Permalink
Revert "Improve custom configuration"
Browse files Browse the repository at this point in the history
This reverts commit de48913.

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

It added regression. Will be back after the beta
  • Loading branch information
rafaelfranca committed Aug 19, 2014
1 parent 867631f commit 43073b3
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 145 deletions.
18 changes: 8 additions & 10 deletions guides/source/4_2_release_notes.md
Expand Up @@ -78,24 +78,22 @@ Please refer to the [Changelog][railties] for detailed changes.
* Introduced an `after_bundle` callback for use in Rails templates. * Introduced an `after_bundle` callback for use in Rails templates.
([Pull Request](https://github.com/rails/rails/pull/16359)) ([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 ```ruby
# config/environments/production.rb # config/environments/production.rb
config.payment_processing.schedule = :daily config.x.payment_processing.schedule = :daily
config.payment_processing.retries = 3 config.x.payment_processing.retries = 3
config.resque = { timeout: 60, inline_jobs: :always } config.x.super_debugger = true
config.super_debugger = true
``` ```


These options are then available through the configuration object: These options are then available through the configuration object:


```ruby ```ruby
Rails.configuration.payment_processing.schedule # => :daily Rails.configuration.x.payment_processing.schedule # => :daily
Rails.configuration.payment_processing.retries # => 3 Rails.configuration.x.payment_processing.retries # => 3
Rails.configuration.resque.timeout # => 60 Rails.configuration.x.super_debugger # => true
Rails.configuration.resque.inline_jobs # => :always Rails.configuration.x.super_debugger.not_set # => nil
Rails.configuration.super_debugger # => true
``` ```


([Commit](https://github.com/rails/rails/commit/611849772dd66c2e4d005dcfe153f7ce79a8a7db)) ([Commit](https://github.com/rails/rails/commit/611849772dd66c2e4d005dcfe153f7ce79a8a7db))
Expand Down
16 changes: 7 additions & 9 deletions guides/source/configuring.md
Expand Up @@ -1006,18 +1006,16 @@ Custom configuration
You can configure your own code through the Rails configuration object with custom configuration. It works like this: You can configure your own code through the Rails configuration object with custom configuration. It works like this:


```ruby ```ruby
config.payment_processing.schedule = :daily config.x.payment_processing.schedule = :daily
config.payment_processing.retries = 3 config.x.payment_processing.retries = 3
config.resque = { timeout: 60, inline_jobs: :always } config.x.super_debugger = true
config.super_debugger = true
``` ```


These configuration points are then available through the configuration object: These configuration points are then available through the configuration object:


```ruby ```ruby
Rails.configuration.payment_processing.schedule # => :daily Rails.configuration.x.payment_processing.schedule # => :daily
Rails.configuration.payment_processing.retries # => 3 Rails.configuration.x.payment_processing.retries # => 3
Rails.configuration.resque.timeout # => 60 Rails.configuration.x.super_debugger # => true
Rails.configuration.resque.inline_jobs # => :always Rails.configuration.x.super_debugger.not_set # => nil
Rails.configuration.super_debugger # => true
``` ```
16 changes: 7 additions & 9 deletions railties/CHANGELOG.md
Expand Up @@ -20,18 +20,16 @@
configure your own code through the Rails configuration object with custom configuration: configure your own code through the Rails configuration object with custom configuration:


# config/environments/production.rb # config/environments/production.rb
config.payment_processing.schedule = :daily config.x.payment_processing.schedule = :daily
config.payment_processing.retries = 3 config.x.payment_processing.retries = 3
config.resque = { timeout: 60, inline_jobs: :always } config.x.super_debugger = true
config.super_debugger = true


These configuration points are then available through the configuration object: These configuration points are then available through the configuration object:


Rails.configuration.payment_processing.schedule # => :daily Rails.configuration.x.payment_processing.schedule # => :daily
Rails.configuration.payment_processing.retries # => 3 Rails.configuration.x.payment_processing.retries # => 3
Rails.configuration.resque.timeout # => 60 Rails.configuration.x.super_debugger # => true
Rails.configuration.resque.inline_jobs # => :always Rails.configuration.x.super_debugger.not_set # => nil
Rails.configuration.super_debugger # => true


*DHH* *DHH*


Expand Down
14 changes: 13 additions & 1 deletion railties/lib/rails/application/configuration.rb
Expand Up @@ -13,7 +13,7 @@ class Configuration < ::Rails::Engine::Configuration
:railties_order, :relative_url_root, :secret_key_base, :secret_token, :railties_order, :relative_url_root, :secret_key_base, :secret_token,
:serve_static_assets, :ssl_options, :static_cache_control, :session_options, :serve_static_assets, :ssl_options, :static_cache_control, :session_options,
:time_zone, :reload_classes_only_on_change, :time_zone, :reload_classes_only_on_change,
:beginning_of_week, :filter_redirect :beginning_of_week, :filter_redirect, :x


attr_writer :log_level attr_writer :log_level
attr_reader :encoding attr_reader :encoding
Expand Down Expand Up @@ -48,6 +48,7 @@ def initialize(*)
@eager_load = nil @eager_load = nil
@secret_token = nil @secret_token = nil
@secret_key_base = nil @secret_key_base = nil
@x = Custom.new


@assets = ActiveSupport::OrderedOptions.new @assets = ActiveSupport::OrderedOptions.new
@assets.enabled = true @assets.enabled = true
Expand Down Expand Up @@ -154,6 +155,17 @@ def session_store(*args)
def annotations def annotations
SourceAnnotationExtractor::Annotation SourceAnnotationExtractor::Annotation
end 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 end
end end
38 changes: 2 additions & 36 deletions railties/lib/rails/railtie/configuration.rb
Expand Up @@ -88,45 +88,11 @@ def respond_to?(name, include_private = false)


def method_missing(name, *args, &blk) def method_missing(name, *args, &blk)
if name.to_s =~ /=$/ if name.to_s =~ /=$/
key = $`.to_sym @@options[$`.to_sym] = args.first
value = args.first

if value.is_a?(Hash)
@@options[key] = ChainedConfigurationOptions.new value
else
@@options[key] = value
end
elsif @@options.key?(name) elsif @@options.key?(name)
@@options[name] @@options[name]
else else
@@options[name] = ActiveSupport::OrderedOptions.new super
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
end end
end end
end end
Expand Down
6 changes: 2 additions & 4 deletions railties/test/application/configuration/base_test.rb
Expand Up @@ -5,8 +5,6 @@
module ApplicationTests module ApplicationTests
module ConfigurationTests module ConfigurationTests
class BaseTest < ActiveSupport::TestCase class BaseTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation

def setup def setup
build_app build_app
boot_rails boot_rails
Expand All @@ -32,8 +30,8 @@ def app
end end


def require_environment def require_environment
require "#{app_path}/config/environment" require "#{app_path}/config/environment"
end end
end end
end end
end end
81 changes: 6 additions & 75 deletions railties/test/application/configuration/custom_test.rb
@@ -1,84 +1,15 @@
require 'application/configuration/base_test' require 'application/configuration/base_test'


class ApplicationTests::ConfigurationTests::CustomTest < ApplicationTests::ConfigurationTests::BaseTest 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 add_to_config <<-RUBY
config.resque.inline_jobs = :always config.x.resque.inline_jobs = :always
config.resque.timeout = 60 config.x.resque.timeout = 60
RUBY RUBY
require_environment require_environment


assert_equal :always, Rails.configuration.resque.inline_jobs assert_equal :always, Rails.configuration.x.resque.inline_jobs
assert_equal 60, Rails.configuration.resque.timeout assert_equal 60, Rails.configuration.x.resque.timeout
assert_nil Rails.configuration.resque.nothing assert_nil Rails.configuration.x.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)
end end
end end
2 changes: 1 addition & 1 deletion railties/test/railties/engine_test.rb
Expand Up @@ -840,7 +840,7 @@ def index


Rails.application.load_seed Rails.application.load_seed
assert Rails.application.config.app_seeds_loaded 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 Bukkits::Engine.load_seed
assert Bukkits::Engine.config.bukkits_seeds_loaded assert Bukkits::Engine.config.bukkits_seeds_loaded
Expand Down

2 comments on commit 43073b3

@pixeltrix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@rafaelfranca
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.