Class: RuboCop::Cop::Rails::EnumUniqueness

Inherits:
Base
  • Object
show all
Includes:
Duplication
Defined in:
lib/rubocop/cop/rails/enum_uniqueness.rb

Overview

Looks for duplicate values in enum declarations.

Examples:

# bad
enum :status, { active: 0, archived: 0 }

# good
enum :status, { active: 0, archived: 1 }

# bad
enum :status, [:active, :archived, :active]

# good
enum :status, [:active, :archived]

# bad
enum status: { active: 0, archived: 0 }

# good
enum status: { active: 0, archived: 1 }

# bad
enum status: [:active, :archived, :active]

# good
enum status: [:active, :archived]

Constant Summary collapse

MSG =
'Duplicate value `%<value>s` found in `%<enum>s` enum declaration.'
RESTRICT_ON_SEND =
%i[enum].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rubocop/cop/rails/enum_uniqueness.rb', line 50

def on_send(node)
  enum?(node) do |key, args|
    consecutive_duplicates(args.values).each do |item|
      add_offense(item, message: message(key, item))
    end
  end

  enum_with_old_syntax?(node) do |pairs|
    pairs.each do |pair|
      enum_values(pair) do |key, args|
        consecutive_duplicates(args.values).each do |item|
          add_offense(item, message: message(key, item))
        end
      end
    end
  end
end