Class: RuboCop::Cop::DevDoc::Migration::AvoidVagueColumnNames

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/dev_doc/migration/avoid_vague_column_names.rb

Overview

Avoid vague column names like status, group, or kind.

Rationale

Use more specific names that describe the domain context. A column named status reveals nothing about what status it tracks; a column named processing_status makes the intent obvious at the call site. kind is the same shape — a content-free "what variety" label that reads no better than type (which Rails reserves for STI).


t.string :status
add_column :orders, :group, :integer
t.integer :kind

✔️
t.string :processing_status
add_column :orders, :user_group, :integer
t.integer :membership_kind

Note about type

type is reserved by Rails for Single Table Inheritance (STI). Even if STI is not in use, naming a column type is misleading and should be avoided. This cop does not flag type by default — that is left to the user's discretion via the VagueNames config.

Configure the list of vague names via VagueNames in .rubocop.yml.

Examples:

# bad
t.string :status
add_column :orders, :group, :integer
t.integer :kind

# good
t.string :processing_status
add_column :orders, :user_group, :integer
t.integer :membership_kind

Constant Summary collapse

MSG =
'Avoid vague column name `%<name>s`. Use a more specific name that includes context.'.freeze
COLUMN_METHODS =
%i[
  string integer float boolean datetime date text binary decimal
  json jsonb bigint primary_key
].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rubocop/cop/dev_doc/migration/avoid_vague_column_names.rb', line 50

def on_send(node)
  col_name_node = if node.method?(:add_column)
                    node.arguments[1]
                  elsif COLUMN_METHODS.include?(node.method_name)
                    node.first_argument
                  end

  return unless col_name_node&.sym_type?
  return unless vague_names.include?(col_name_node.value.to_s)

  add_offense(col_name_node, message: format(MSG, name: col_name_node.value))
end