Skip to content

Commit

Permalink
Only consider migration classes for MigrationClassName
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny committed Mar 15, 2022
1 parent 8813b04 commit ba2d060
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#657](https://github.com/rubocop/rubocop-rails/pull/657): Only consider migration classes for `Rails/MigrationClassName`. ([@sunny][])
3 changes: 3 additions & 0 deletions lib/rubocop/cop/rails/migration_class_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ module Rails
#
class MigrationClassName < Base
extend AutoCorrector
include MigrationsHelper

MSG = 'Replace with `%<corrected_class_name>s` that matches the file name.'

def on_class(node)
return if in_migration?(node)

snake_class_name = to_snakecase(node.identifier.source)

return if snake_class_name == basename_without_timestamp
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/rails/migration_class_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ class CreateUsers < ActiveRecord::Migration[7.0]
end
RUBY
end

context 'when defining another class' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY, filename)
class CreateUsers < ActiveRecord::Migration[7.0]
class Article < ActiveRecord::Base
end
end
RUBY
end
end
end

context 'when the class name does not match its file name' do
Expand Down

0 comments on commit ba2d060

Please sign in to comment.