Skip to content

Instantly share code, notes, and snippets.

@louishuyng
Last active March 27, 2024 15:46
Show Gist options
  • Save louishuyng/3985a98807404bcee0c0cd87570a41b5 to your computer and use it in GitHub Desktop.
Save louishuyng/3985a98807404bcee0c0cd87570a41b5 to your computer and use it in GitHub Desktop.
Custom Rubocop
require:
- ./lib/rubocop/cop/lint_rails_env
Lint/RailsEnv:
Enabled: true
Exclude:
- 'config/**/*.rb'
module RuboCop
module Cop
module Lint
class RailsEnv < RuboCop::Cop::Cop
MSG = "Avoid Rails.env in application code, " \
"use configuration parameters instead"
def_node_matcher :rails_env?, <<~PATTERN
(send {(const nil? :Rails) (const (cbase)
:Rails)} :env)
PATTERN
def on_send(node)
return unless rails_env?(node)
add_offense(
node.parent.type == :send ? node.parent : node,
location: :selector, message: MSG
)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment