Skip to content

Commit

Permalink
Merge pull request #1456 from Earlopain/timestamp-numblock
Browse files Browse the repository at this point in the history
Fix a false positive for `Rails/CreateTableWithTimestamps` with numblocks
  • Loading branch information
koic authored Feb 27, 2025
2 parents c67ec6f + f226d20 commit 9254ef6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog/fix_false_positive_table_timestamp_numblock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1456](https://github.com/rubocop/rubocop-rails/pull/1456): Fix a false positive for `Rails/CreateTableWithTimestamps` with numblocks. ([@earlopain][])
4 changes: 2 additions & 2 deletions lib/rubocop/cop/mixin/active_record_migrations_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ module ActiveRecordMigrationsHelper
].freeze

def_node_matcher :create_table_with_block?, <<~PATTERN
(block
(any_block
(send nil? :create_table ...)
(args (arg _var))
{ _ | (args (arg _var)) }
_)
PATTERN
end
Expand Down
17 changes: 14 additions & 3 deletions spec/rubocop/cop/rails/create_table_with_timestamps_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
RUBY
end

it 'registers an offense when not including timestampswith `to_proc` syntax' do
it 'registers an offense when not including timestamps with `to_proc` syntax' do
expect_offense <<~RUBY
create_table :users, &:extension_columns
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add timestamps when creating a new table.
Expand All @@ -53,13 +53,24 @@
RUBY
end

it 'does not register an offense when including timestampswith `to_proc` syntax' do
it 'does not register an offense when including timestamps in numblock' do
expect_no_offenses <<~RUBY
create_table :users do
_1.string :name
_1.string :email
_1.timestamps
end
RUBY
end

it 'does not register an offense when including timestamps with `to_proc` syntax' do
expect_no_offenses <<~RUBY
create_table :users, &:timestamps
RUBY
end

it 'does not register an offense when including timestampswith options and `to_proc` syntax' do
it 'does not register an offense when including timestamps with options and `to_proc` syntax' do
expect_no_offenses <<~RUBY
create_table :users, id: :string, limit: 42, &:timestamps
RUBY
Expand Down

0 comments on commit 9254ef6

Please sign in to comment.