class RuboCop::Cop::Rails::UnknownEnv
Rails.env == ‘production’
Rails.env.production?
# good
Rails.env == ‘proudction’
Rails.env.proudction?
# bad
@example
More can be added to the ‘Environments` config parameter.
`development`, `test`, and `production`.
By default the cop allows three environments which Rails ships with:
exist.
This cop checks that environments called with `Rails.env` predicates
def collect_variable_like_names(_scope)
def collect_variable_like_names(_scope) environments end
def environments
def environments cop_config['Environments'] end
def message(name)
def message(name) name = name.to_s.chomp('?') # DidYouMean::SpellChecker is not available in all versions of Ruby, # and even on versions where it *is* available (>= 2.3), it is not # always required correctly. So we do a feature check first. See: # https://github.com/rubocop/rubocop/issues/7979 similar_names = if defined?(DidYouMean::SpellChecker) spell_checker = DidYouMean::SpellChecker.new(dictionary: environments) spell_checker.correct(name) else [] end if similar_names.empty? format(MSG, name: name) else format(MSG_SIMILAR, name: name, similar: similar_names.join(', ')) end end
def on_send(node)
def on_send(node) unknown_environment_predicate?(node) do |name| add_offense(node.loc.selector, message: message(name)) end unknown_environment_equal?(node) do |str_node| name = str_node.value add_offense(str_node, message: message(name)) end end
def unknown_env_name?(name)
def unknown_env_name?(name) !environments.include?(name) end
def unknown_env_predicate?(name)
def unknown_env_predicate?(name) name = name.to_s name.end_with?('?') && !environments.include?(name[0..-2]) end