Class: RuboCop::Cop::Rails::SchemaComment

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

Overview

Enforces the use of the ‘comment` option when adding a new table or column to the database during a migration.

Examples:

# bad (no comment for a new column or table)
add_column :table, :column, :integer

create_table :table do |t|
  t.type :column
end

# good
add_column :table, :column, :integer, comment: 'Number of offenses'

create_table :table, comment: 'Table of offenses data' do |t|
  t.type :column, comment: 'Number of offenses'
end

Constant Summary collapse

COLUMN_MSG =
'New database column without `comment`.'
TABLE_MSG =
'New database table without `comment`.'
RESTRICT_ON_SEND =
%i[add_column create_table].freeze
CREATE_TABLE_COLUMN_METHODS =
Set[
  *(
    RAILS_ABSTRACT_SCHEMA_DEFINITIONS |
    RAILS_ABSTRACT_SCHEMA_DEFINITIONS_HELPERS |
    POSTGRES_SCHEMA_DEFINITIONS |
    MYSQL_SCHEMA_DEFINITIONS
  )
].freeze

Constants included from ActiveRecordMigrationsHelper

ActiveRecordMigrationsHelper::MYSQL_SCHEMA_DEFINITIONS, ActiveRecordMigrationsHelper::POSTGRES_SCHEMA_DEFINITIONS, ActiveRecordMigrationsHelper::RAILS_ABSTRACT_SCHEMA_DEFINITIONS, ActiveRecordMigrationsHelper::RAILS_ABSTRACT_SCHEMA_DEFINITIONS_HELPERS

Instance Method Summary collapse

Instance Method Details

#add_column?(node) ⇒ Object



45
46
47
# File 'lib/rubocop/cop/rails/schema_comment.rb', line 45

def_node_matcher :add_column?, <<~PATTERN
  (send nil? :add_column _table _column _type _?)
PATTERN

#add_column_with_comment?(node) ⇒ Object



50
51
52
# File 'lib/rubocop/cop/rails/schema_comment.rb', line 50

def_node_matcher :add_column_with_comment?, <<~PATTERN
  (send nil? :add_column _table _column _type #comment_present?)
PATTERN

#comment_present?(node) ⇒ Object



40
41
42
# File 'lib/rubocop/cop/rails/schema_comment.rb', line 40

def_node_matcher :comment_present?, <<~PATTERN
  (hash <(pair {(sym :comment) (str "comment")} (_ [present?])) ...>)
PATTERN

#create_table?(node) ⇒ Object



55
56
57
# File 'lib/rubocop/cop/rails/schema_comment.rb', line 55

def_node_matcher :create_table?, <<~PATTERN
  (send nil? :create_table _table _?)
PATTERN

#on_send(node) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/rubocop/cop/rails/schema_comment.rb', line 74

def on_send(node)
  if add_column_without_comment?(node)
    add_offense(node, message: COLUMN_MSG)
  elsif create_table_without_comment?(node)
    add_offense(node, message: TABLE_MSG)
  elsif create_table_with_block?(node.parent)
    check_column_within_create_table_block(node.parent.body)
  end
end

#t_column?(node) ⇒ Object



65
66
67
# File 'lib/rubocop/cop/rails/schema_comment.rb', line 65

def_node_matcher :t_column?, <<~PATTERN
  (send _var CREATE_TABLE_COLUMN_METHODS ...)
PATTERN

#t_column_with_comment?(node) ⇒ Object



70
71
72
# File 'lib/rubocop/cop/rails/schema_comment.rb', line 70

def_node_matcher :t_column_with_comment?, <<~PATTERN
  (send _var CREATE_TABLE_COLUMN_METHODS _column _type? #comment_present?)
PATTERN