Class: RuboCop::Cop::Rails::SchemaComment
Overview
Enforces the use of the ‘comment` option when adding a new table or column to the database during a migration.
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
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
|
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
|
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 (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
|
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
|