Class: RuboCop::Cop::Betterment::FetchBoolean

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/betterment/fetch_boolean.rb

Constant Summary collapse

MSG =
<<~MSG
  A boolean fetched from query params or ENV will never be false when
  explicitly specified on the request or env var. Please use a model
  with a boolean attribute, or cast the value.
MSG

Instance Method Summary collapse

Instance Method Details

#action_controller?(node) ⇒ Object



34
35
36
37
38
39
# File 'lib/rubocop/cop/betterment/fetch_boolean.rb', line 34

def_node_search :action_controller?, <<~PATTERN
  {
    (const {nil? cbase} :ApplicationController)
    (const (const {nil? cbase} :ActionController) :Base)
  }
PATTERN

#boolean_cast?(node) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/rubocop/cop/betterment/fetch_boolean.rb', line 24

def_node_search :boolean_cast?, "(send\n(send\n(const\n(const\n(const nil? :ActiveModel) :Type) :Boolean) :new) :cast\n...)\n"

#fetch_boolean?(node) ⇒ Object



14
15
16
# File 'lib/rubocop/cop/betterment/fetch_boolean.rb', line 14

def_node_matcher :fetch_boolean?, "(send _ :fetch _ (boolean))\n"

#fetch_env_boolean?(node) ⇒ Object



19
20
21
# File 'lib/rubocop/cop/betterment/fetch_boolean.rb', line 19

def_node_matcher :fetch_env_boolean?, "(send (const nil? :ENV) :fetch _ (boolean))\n"

#on_send(node) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/rubocop/cop/betterment/fetch_boolean.rb', line 41

def on_send(node)
  return unless fetch_env_boolean?(node) ||
    (fetch_boolean?(node) && inherit_action_controller_base?(node))

  return if node.each_ancestor(:send).any? { |ancestor| boolean_cast?(ancestor) }

  add_offense(node)
end