Skip to content

Commit 9a58f7e

Browse files
committed
Raise ArgumentError when reading from config.x with arguments
The flexibility provided by `config.x` supports arbitrarily defining new configuration objects on-the-fly. Each new intermediate configuration object is constructed during chaining from its ancestor through a method invocation made without arguments. Conversely, writing to leaf configuration values occurs when the invoked method's name ends with `=`, and the new configuration value is assigned to whatever that method's arguments are. There are no cases when reading from a `config.x` value or building the intermediate values involves arguments. Prior to this commit, a read invoked with a method arguments would ignore those arguments. While this is robust and error-free, it's possible to obscure misuse. For example, consider a line like: ```ruby config.x.my_config.enabled = true config.x.my_config.enabled #=> true ``` Now consider that first line with a typo that omits the `=`: ```ruby config.x.my_config.enabled true config.x.my_config.enabled #=> nil ``` This commit aims to provide more direct feedback for scenarios like the one above. There aren't legitimate use cases for invoking `#enabled` with arguments, so raise a `ArgumentError` when encountering a read with arguments.
1 parent b1fd934 commit 9a58f7e

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

railties/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* Raise `ArgumentError` when reading `config.x.something` with arguments
2+
3+
```ruby
4+
config.x.this_works.this_raises true # raises ArgumentError
5+
```
6+
7+
*Sean Doyle*
8+
19
* Add default PWA files for manifest and service-worker that are served from `app/views/pwa` and can be dynamically rendered through erb. Mount these files explicitly at the root with default routes in the generated routes file.
210

311
*DHH*

railties/lib/rails/application/configuration.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,10 +569,14 @@ def initialize
569569
def method_missing(method, *args)
570570
if method.end_with?("=")
571571
@configurations[:"#{method[0..-2]}"] = args.first
572-
else
572+
elsif args.none?
573573
@configurations.fetch(method) {
574574
@configurations[method] = ActiveSupport::OrderedOptions.new
575575
}
576+
else
577+
arguments = args.map(&:inspect)
578+
579+
raise ArgumentError.new("unexpected arguments (%s) while reading `%s` configuration" % [arguments.join(", "), method])
576580
end
577581
end
578582

railties/test/application/configuration/custom_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ def teardown
3636
assert_respond_to x, :i_do_not_exist
3737
assert_kind_of Method, x.method(:i_do_not_exist)
3838
assert_kind_of ActiveSupport::OrderedOptions, x.i_do_not_exist
39+
40+
assert_raises ArgumentError, match: "unexpected arguments (true, false) while reading `i_do_not_exist` configuration" do
41+
x.i_do_not_exist(true, false)
42+
end
3943
end
4044

4145
private

0 commit comments

Comments
 (0)