Skip to content

Commit

Permalink
Pull in the custom configuration concept from dhh/custom_configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
dhh committed Aug 3, 2014
1 parent e5632f3 commit 6118497
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 1 deletion.
21 changes: 21 additions & 0 deletions guides/source/configuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
17 changes: 17 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
14 changes: 13 additions & 1 deletion railties/lib/rails/application/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

This comment has been minimized.

Copy link
@adrianpacala

adrianpacala Aug 5, 2014

Contributor

Why x and not app or something more descriptive?

This comment has been minimized.

Copy link
@dgm

dgm Jan 24, 2015

Contributor

yeah, x is not very well named.

This comment has been minimized.

Copy link
@eveevans

eveevans Jun 3, 2015

You can use something like:

 config.whatever.foo.bar = "wow"

no?


attr_writer :log_level
attr_reader :encoding
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
37 changes: 37 additions & 0 deletions railties/test/application/configuration/base_test.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions railties/test/application/configuration/custom_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6118497

Please sign in to comment.