From ba2d06039eb07166672973f9fe994c6a39a933c7 Mon Sep 17 00:00:00 2001 From: Sunny Ripert Date: Tue, 15 Mar 2022 09:18:09 +0100 Subject: [PATCH] Only consider migration classes for `MigrationClassName` --- ...ider_migration_classes_for_migration_class_name.md | 1 + lib/rubocop/cop/rails/migration_class_name.rb | 3 +++ spec/rubocop/cop/rails/migration_class_name_spec.rb | 11 +++++++++++ 3 files changed, 15 insertions(+) create mode 100644 changelog/fix_only_consider_migration_classes_for_migration_class_name.md diff --git a/changelog/fix_only_consider_migration_classes_for_migration_class_name.md b/changelog/fix_only_consider_migration_classes_for_migration_class_name.md new file mode 100644 index 0000000000..4b7937396f --- /dev/null +++ b/changelog/fix_only_consider_migration_classes_for_migration_class_name.md @@ -0,0 +1 @@ +* [#657](https://github.com/rubocop/rubocop-rails/pull/657): Only consider migration classes for `Rails/MigrationClassName`. ([@sunny][]) diff --git a/lib/rubocop/cop/rails/migration_class_name.rb b/lib/rubocop/cop/rails/migration_class_name.rb index 09d9011743..b99dc70bf7 100644 --- a/lib/rubocop/cop/rails/migration_class_name.rb +++ b/lib/rubocop/cop/rails/migration_class_name.rb @@ -20,10 +20,13 @@ module Rails # class MigrationClassName < Base extend AutoCorrector + include MigrationsHelper MSG = 'Replace with `%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 diff --git a/spec/rubocop/cop/rails/migration_class_name_spec.rb b/spec/rubocop/cop/rails/migration_class_name_spec.rb index feae99d044..907686eb0c 100644 --- a/spec/rubocop/cop/rails/migration_class_name_spec.rb +++ b/spec/rubocop/cop/rails/migration_class_name_spec.rb @@ -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