Add AllowedPredicates configuration option to Rails/Env - #1617
Conversation
Replace the hard-coded `ALLOWED_LIST` constant with a configurable
`AllowedPredicates` option. The default value preserves the previous
behavior — `String` / `StringInquirer` predicates such as `empty?`,
`match?`, and `between?` are still exempt out of the box — but users
can now customize the list, for example to allow `Rails.env.local?`
or a custom predicate monkey-patched onto the environment inquirer.
The configured value fully replaces the default; users who want to
inherit the defaults and add to them can opt in via RuboCop's
`inherit_mode`.
Rails/Env:
AllowedPredicates:
- empty?
- match?
- local?
A new spec block uses `let(:cop_config)` to verify that overriding
`AllowedPredicates` both allows the configured predicate and continues
to flag predicates that are no longer in the list.
6dc5bea to
b7b27ad
Compare
commented
May 11, 2026
|
Great fix! Thanks for the added versatility. And we need this now as well, because we do allow |
commented
Jun 17, 2026
|
@Earlopain can you help us get this across the line? @fxn had asked us to support The approach here allows everyone to adjust the cop accordingly so they can keep Rails/EnvLocal and also extend the behavior as needed |
commented
Jun 17, 2026
|
It would be really convenient to have |
commented
Jun 18, 2026
Sorry, you would be better to get the attention of a maintainer. I haven't done any rubocop this year. |
commented
Jun 18, 2026
|
I'm not sure a public option like |
Thanks @koic, I put together that approach here: |
commented
Jun 22, 2026
I could live with that, though I would like to be able to drop the other allowed predicates (it’s slightly silly to ban |
Summary
Replace
Rails/Env's hard-codedALLOWED_LISTconstant with a configurableAllowedPredicatesoption. The default value is the previousALLOWED_LISTcontent —String/StringInquirerpredicates likeempty?,match?,between?— so out-of-the-box behavior is unchanged.The motivation is to let users exempt additional predicates from the cop without forking or disabling it. Two concrete cases this addresses:
Rails.env.local?— the built-in alias for "development or test", introduced in Rails 7.1. Some users treat it as a legitimate guard rail for code that should only ever execute in dev or test (sanity checks, devtools, seed data) rather than as an environment-rollout mechanism, and want it exempt from the cop.Rails.env.beta_user?/Rails.env.internal?style helpers can now opt those in.The configured value fully replaces the default — it is not merged. Users who want to inherit the defaults and add to them can opt in via RuboCop's
inherit_mode:Compatibility with
Rails/EnvLocalRails/EnvLocalautocorrectsRails.env.development? || Rails.env.test?→Rails.env.local?. Under the defaultAllowedPredicates,Rails/Envwill still flag the autocorrectedRails.env.local?. Users who run both cops can now resolve that conflict by includinglocal?in theirAllowedPredicatesrather than disabling either cop.Test coverage
A new context block uses
let(:cop_config)to overrideAllowedPredicatesand verifies (a) a predicate in the override is not flagged, and (b) a predicate omitted from the override is flagged — confirming the override is exhaustive (no implicit merge with defaults).Before submitting the PR make sure the following are checked:
[Fix #issue-number](no related issue).master.bundle exec rake default.