Class: RuboCop::Cop::Vicenzo::Rails::EnumInclusionOfValidation

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/vicenzo/rails/enum_inclusion_of_validation.rb

Overview

Ensures that enums using the new syntax include the validate: { allow_nil: true } option.

Old-style enums (keyword argument syntax) are ignored.

Examples:

# bad — missing validate option
enum :status, { active: 1, inactive: 0 }, suffix: true

# bad — validate option present but incorrect
enum :status, { active: 1, inactive: 0 }, validate: true, suffix: true

# good
enum :status, { active: 1, inactive: 0 }, validate: { allow_nil: true }, suffix: true

# ignored — old-style enum syntax
enum status: { active: 1, inactive: 0 }

Constant Summary collapse

MSG_MISSING_VALIDATE =
'Add `validate: { allow_nil: true }` to the enum.'
MSG_INVALID_VALIDATE =
'The `validate` option for the enum must be `validate: { allow_nil: true }`.'
RESTRICT_ON_SEND =
[:enum].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object Also known as: on_csend



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/vicenzo/rails/enum_inclusion_of_validation.rb', line 32

def on_send(node)
  return unless node.command?(:enum)

  # Ignore old-style enums
  first_argument = node.first_argument
  return if first_argument&.hash_type?

  validate_kwarg = find_validate_option(node)

  register_offence_for(node, validate_kwarg)
end