Skip to content

Commit

Permalink
Fix Migration/ChangeTableReferences offense location
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhacaz committed Sep 7, 2023
1 parent 076f77e commit 6af2828
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

# main

* Fix `Migration/ChangeTableReferences` offense location (Fix [#61](https://github.com/petalmd/rubocop-petal/issues/61)) ([#X](https://github.com/petalmd/rubocop-petal/pull/X))
* Added cop `Sidekiq/NoEarlyNilReturn` ([#58](https://github.com/petalmd/rubocop-petal/pull/58))

* Added cop `Rails/EnumStartingValue` ([#57](https://github.com/petalmd/rubocop-petal/pull/57))

* Added cop `Migration/StandaloneAddReference` ([#54](https://github.com/petalmd/rubocop-petal/pull/54))
* Update `Migration/ChangeTableReferences` on send alias and message to handle removing references. ([#55](https://github.com/petalmd/rubocop-petal/pull/55))

Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/migration/change_table_references.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ChangeTableReferences < Base
'Or `t.remove_foreign_key`, `t.remove` to remove a reference.'

# @!method add_references_in_block?(node)
def_node_search :add_references_in_block?, <<~PATTERN
def_node_matcher :add_references_in_block?, <<~PATTERN
(send lvar /references|belongs_to|remove_references|remove_belongs_to/ ...)
PATTERN

Expand All @@ -37,7 +37,7 @@ class ChangeTableReferences < Base
def on_block(node)
return unless change_table?(node)

references_node = node.children.detect { |n| add_references_in_block?(n) }
references_node = node.child_nodes[2].each_node.detect { |n| add_references_in_block?(n) }
return unless references_node

arguments = references_node.child_nodes[1]
Expand Down
3 changes: 3 additions & 0 deletions spec/rubocop/cop/migration/change_table_references_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
it 'registers an offense when using references in a change_table' do
expect_offense(<<~RUBY)
change_table :subscriptions, bulk: true do |t|
t.string :name
t.string :email
t.boolean :admin, default: false
t.references :user, null: false, foreign_key: true
^^^^^^^^^^^^ Use a combination of `t.bigint`, `t.index` and `t.foreign_key` in a change_table to add a reference.Or `t.remove_foreign_key`, `t.remove` to remove a reference.
end
Expand Down

0 comments on commit 6af2828

Please sign in to comment.