Class: GLRubocop::GLCops::LimitFlashOptions

Inherits:
RuboCop::Cop::Base
  • Object
show all
Defined in:
lib/gl_rubocop/gl_cops/limit_flash_options.rb

Constant Summary collapse

MSG =

Bad:

flash[:error] = "Not allowed"
flash.now[:anything_else] = "Not allowed"
Alert::Component.new(type: :notice, message: "Error")
Notifications::Dismissible::Component.new(variant: :blue_box, message: "Error")
'This cop checks for the use of flash options not in the allowlist. ' \
'Please limit flash options to those defined in the application configuration.'
ALLOWED_FLASH_KEYS =
%i[success info warning danger].freeze

Instance Method Summary collapse

Instance Method Details

#check_key(node, key) ⇒ Object



73
74
75
76
77
78
# File 'lib/gl_rubocop/gl_cops/limit_flash_options.rb', line 73

def check_key(node, key)
  return false unless key
  return false if ALLOWED_FLASH_KEYS.include?(key)

  add_offense(node, message: MSG)
end

#on_send(node) ⇒ Object

Checks for usage of flash or flash.now with keys not in the allowlist



67
68
69
70
71
# File 'lib/gl_rubocop/gl_cops/limit_flash_options.rb', line 67

def on_send(node)
  check_key(node, rails_flash?(node))
  check_key(node, alert_component_new?(node))
  check_key(node, notifications_dismissible_component_new?(node))
end