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

Inherits:
Base
  • Object
show all
Includes:
ActiveRecordMigrationsHelper, MigrationsHelper
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

Methods included from MigrationsHelper

#in_migration?

Instance Method Details

#add_column?(node) ⇒ Object



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

def_node_matcher :add_column?, "(send nil? :add_column _table _column _type _?)\n"

#add_column_with_comment?(node) ⇒ Object



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

def_node_matcher :add_column_with_comment?, "(send nil? :add_column _table _column _type #comment_present?)\n"

#comment_present?(node) ⇒ Object



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

def_node_matcher :comment_present?, "(hash <(pair {(sym :comment) (str \"comment\")} !{nil (str blank?)}) ...>)\n"

#create_table?(node) ⇒ Object



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

def_node_matcher :create_table?, "(send nil? :create_table _table _?)\n"

#on_send(node) ⇒ Object



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

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



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

def_node_matcher :t_column?, "(send _var CREATE_TABLE_COLUMN_METHODS ...)\n"

#t_column_with_comment?(node) ⇒ Object



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

def_node_matcher :t_column_with_comment?, "(send _var CREATE_TABLE_COLUMN_METHODS _column _type? #comment_present?)\n"