class RuboCop::Cop::Rails::EnumHash


enum status: { active: 0, archived: 1 }
# good
enum status: [:active, :archived]
# bad
enum :status, { active: 0, archived: 1 }
# good
enum :status, [:active, :archived]
# bad
@example
value for each key prevents this from happening.
definitions to shift. Explicitly specifying the
position other than the last causes all previous
When using array syntax, adding an element in a
Looks for enums written with array syntax.

def build_hash(array)

def build_hash(array)
  hash = array.children.each_with_index.map do |elem, index|
    "#{source(elem)} => #{index}"
  end.join(', ')
  "{#{hash}}"
end

def enum_name(key)

def enum_name(key)
  case key.type
  when :sym, :str
    key.value
  else
    key.source
  end
end

def message(key)

def message(key)
  format(MSG, enum: enum_name(key))
end

def on_send(node)

def on_send(node)
  target_rails_version >= 7.0 && enum_with_array?(node) do |key, array|
    add_offense(array, message: message(key)) do |corrector|
      corrector.replace(array, build_hash(array))
    end
  end
  enum_with_old_syntax?(node) do |pairs|
    pairs.each do |pair|
      key, array = array_pair?(pair)
      next unless key
      add_offense(array, message: message(key)) do |corrector|
        corrector.replace(array, build_hash(array))
      end
    end
  end
end

def source(elem)

def source(elem)
  case elem.type
  when :str
    elem.value.dump
  when :sym
    elem.value.inspect
  else
    elem.source
  end
end