-
-
Notifications
You must be signed in to change notification settings - Fork 266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal] A new cop to prefer fetch
for Rails.application.secrets
#42
Comments
Good idea. Let's include |
I think that a bang method should be used instead of
% bin/rails c
Loading development environment (Rails 5.2.3)
[1] pry(main)> Rails.application.credentials.some_api_key
=> nil
[2] pry(main)> Rails.application.credentials.some_api_key!
KeyError: :some_api_key is blank
from /Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/ordered_options.rb:49:in `method_missing'
[3] pry(main)> Rails.application.secrets.some_api_key
=> nil
[4] pry(main)> Rails.application.secrets.some_api_key!
KeyError: :some_api_key is blank
from /Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/ordered_options.rb:49:in `method_missing' No bang method is not a deprecation. If no bang is deprecated or a strongly recommended document (e.g. Rails Guides) with bang is accepted by Rails team (rails/rails repo), I think it is worthwhile to make cop with bang method a good case. |
Thanks @koic! I didn’t know about that. |
I think it is worthwhile regardless to make a cop that suggests some form of better way to access credentials than direct access. |
This PR resolves the following error when running `rake changelog:merge` in RuboCop Rails. So it will prevent the same error from occurring in this repository. ```conosle $ bundle exec rake changelog:merge rake aborted! NoMethodError: undefined method `[]' for nil /Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:139:in `block in contributors' /Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:138:in `each' /Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:138:in `flat_map' /Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:138:in `contributors' /Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:132:in `new_contributor_lines' /Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:126:in `merge_content' /Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:115:in `merge!' tasks/changelog.rake:21:in `block (2 levels) in <top (required)>' /Users/koic/.rbenv/versions/3.3.0-dev/bin/bundle:25:in `load' /Users/koic/.rbenv/versions/3.3.0-dev/bin/bundle:25:in `<main>' Tasks: TOP => changelog:merge (See full trace by running task with --trace) ``` This error was caused by the absence of a space after the period: ```diff -* [#42](rubocop/rubocop-rails#42): Fix an error for `Rails/RedundantActiveRecordAllMethod`.([contributor_name]) +* [#42](rubocop/rubocop-rails#42): Fix an error for `Rails/RedundantActiveRecordAllMethod`. ([contributor_name]) ``` The added test verifies that there is a single space between the period and the parentheses. And tweaks changelog entries NOTE: For compatibility with outdated formats, if there's no contributor name, it checks that the line ends with a period.
Rails.application.secrets.foo
returnsnil
if the entry does not exist. However, accessing a missing entry is likely to a typo.Fortunately, it has
fetch()
method that raises errors in a missing entry, so it's a good idea to preferfetch()
instead of the use of getter.e.g.
The text was updated successfully, but these errors were encountered: