Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only consider migration classes for MigrationClassName #657

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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