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` or `group`.

## 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.


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

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

## 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

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

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



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubocop/cop/dev_doc/migration/avoid_vague_column_names.rb', line 44

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