Skip to content

Commit 34376e0

Browse files
authored
Merge pull request #1042 from nipe0324/fix-schema-comment-with-multi-t-column
Fix create_table with multi t columns for `Rails/SchemaComment`
2 parents bef142f + 91e2e3e commit 34376e0

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1042](https://github.com/rubocop/rubocop-rails/pull/1042): Fix no offences for `Rails/SchemaComment` when create_table with multi t columns. ([@nipe0324][])

lib/rubocop/cop/rails/schema_comment.rb

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,25 @@ class SchemaComment < Base
7474
def on_send(node)
7575
if add_column_without_comment?(node)
7676
add_offense(node, message: COLUMN_MSG)
77-
elsif create_table?(node)
78-
if create_table_without_comment?(node)
79-
add_offense(node, message: TABLE_MSG)
80-
elsif create_table_column_call_without_comment?(node)
81-
add_offense(node.parent.body, message: COLUMN_MSG)
82-
end
77+
elsif create_table_without_comment?(node)
78+
add_offense(node, message: TABLE_MSG)
79+
elsif create_table_with_block?(node.parent)
80+
check_column_within_create_table_block(node.parent.body)
8381
end
8482
end
8583

8684
private
8785

86+
def check_column_within_create_table_block(node)
87+
if node.begin_type?
88+
node.child_nodes.each do |child_node|
89+
add_offense(child_node, message: COLUMN_MSG) if t_column_without_comment?(child_node)
90+
end
91+
elsif t_column_without_comment?(node)
92+
add_offense(node, message: COLUMN_MSG)
93+
end
94+
end
95+
8896
def add_column_without_comment?(node)
8997
add_column?(node) && !add_column_with_comment?(node)
9098
end
@@ -93,10 +101,8 @@ def create_table_without_comment?(node)
93101
create_table?(node) && !create_table_with_comment?(node)
94102
end
95103

96-
def create_table_column_call_without_comment?(node)
97-
create_table_with_block?(node.parent) &&
98-
t_column?(node.parent.body) &&
99-
!t_column_with_comment?(node.parent.body)
104+
def t_column_without_comment?(node)
105+
t_column?(node) && !t_column_with_comment?(node)
100106
end
101107
end
102108
end

spec/rubocop/cop/rails/schema_comment_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,28 @@
8787
RUBY
8888
end
8989

90+
it 'registers two offenses when two `t.column` have no `comment` option' do
91+
expect_offense(<<~RUBY)
92+
create_table :users, comment: 'Table' do |t|
93+
t.column :column1, :integer
94+
^^^^^^^^^^^^^^^^^^^^^^^^^^^ New database column without `comment`.
95+
t.column :column2, :integer
96+
^^^^^^^^^^^^^^^^^^^^^^^^^^^ New database column without `comment`.
97+
end
98+
RUBY
99+
end
100+
101+
it 'registers two offenses when two `t.integer` have no `comment` option' do
102+
expect_offense(<<~RUBY)
103+
create_table :users, comment: 'Table' do |t|
104+
t.integer :column1
105+
^^^^^^^^^^^^^^^^^^ New database column without `comment`.
106+
t.integer :column2
107+
^^^^^^^^^^^^^^^^^^ New database column without `comment`.
108+
end
109+
RUBY
110+
end
111+
90112
it 'does not register an offense when `t.column` has `comment` option' do
91113
expect_no_offenses(<<~RUBY)
92114
create_table :users, comment: 'Table' do |t|

0 commit comments

Comments
 (0)