Skip to content

Commit

Permalink
Merge pull request #1042 from nipe0324/fix-schema-comment-with-multi-…
Browse files Browse the repository at this point in the history
…t-column

Fix create_table with multi t columns for `Rails/SchemaComment`
  • Loading branch information
koic authored Jul 29, 2023
2 parents bef142f + 91e2e3e commit 34376e0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1042](https://github.com/rubocop/rubocop-rails/pull/1042): Fix no offences for `Rails/SchemaComment` when create_table with multi t columns. ([@nipe0324][])
26 changes: 16 additions & 10 deletions lib/rubocop/cop/rails/schema_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,25 @@ class SchemaComment < Base
def on_send(node)
if add_column_without_comment?(node)
add_offense(node, message: COLUMN_MSG)
elsif create_table?(node)
if create_table_without_comment?(node)
add_offense(node, message: TABLE_MSG)
elsif create_table_column_call_without_comment?(node)
add_offense(node.parent.body, message: COLUMN_MSG)
end
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

private

def check_column_within_create_table_block(node)
if node.begin_type?
node.child_nodes.each do |child_node|
add_offense(child_node, message: COLUMN_MSG) if t_column_without_comment?(child_node)
end
elsif t_column_without_comment?(node)
add_offense(node, message: COLUMN_MSG)
end
end

def add_column_without_comment?(node)
add_column?(node) && !add_column_with_comment?(node)
end
Expand All @@ -93,10 +101,8 @@ def create_table_without_comment?(node)
create_table?(node) && !create_table_with_comment?(node)
end

def create_table_column_call_without_comment?(node)
create_table_with_block?(node.parent) &&
t_column?(node.parent.body) &&
!t_column_with_comment?(node.parent.body)
def t_column_without_comment?(node)
t_column?(node) && !t_column_with_comment?(node)
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/rails/schema_comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@
RUBY
end

it 'registers two offenses when two `t.column` have no `comment` option' do
expect_offense(<<~RUBY)
create_table :users, comment: 'Table' do |t|
t.column :column1, :integer
^^^^^^^^^^^^^^^^^^^^^^^^^^^ New database column without `comment`.
t.column :column2, :integer
^^^^^^^^^^^^^^^^^^^^^^^^^^^ New database column without `comment`.
end
RUBY
end

it 'registers two offenses when two `t.integer` have no `comment` option' do
expect_offense(<<~RUBY)
create_table :users, comment: 'Table' do |t|
t.integer :column1
^^^^^^^^^^^^^^^^^^ New database column without `comment`.
t.integer :column2
^^^^^^^^^^^^^^^^^^ New database column without `comment`.
end
RUBY
end

it 'does not register an offense when `t.column` has `comment` option' do
expect_no_offenses(<<~RUBY)
create_table :users, comment: 'Table' do |t|
Expand Down

0 comments on commit 34376e0

Please sign in to comment.