-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#644](https://github.com/rubocop/rubocop-rails/pull/644): Add new `MigrationFileName` cop. ([@johnny-miyake][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop makes sure that each migration file defines a migration class | ||
# whose name matches the file name. | ||
# (i.e. `20220224111111_create_users.rb` should define `CreateUsers` class.) | ||
# | ||
# @example | ||
# # db/migrate/20220224111111_create_users.rb | ||
# | ||
# # bad | ||
# | ||
# class SellBooks < ActiveRecord::Migration[7.0] | ||
# end | ||
# | ||
# # good | ||
# | ||
# class CreateUsers < ActiveRecord::Migration[7.0] | ||
# end | ||
# | ||
class MigrationFileName < Base | ||
MSG = 'The class name does not match with the file name.' | ||
|
||
def on_new_investigation | ||
filepath = processed_source.file_path | ||
@basename = File.basename(filepath, '.rb') | ||
end | ||
|
||
def on_class(node) | ||
class_name = node.loc.name.source | ||
snake_class_name = to_snakecase(class_name) | ||
basename_without_timestamp = @basename.sub(/^[0-9]+_/, '') | ||
add_offense(node.loc.name) if snake_class_name != basename_without_timestamp | ||
end | ||
|
||
private | ||
|
||
def to_snakecase(word) | ||
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') | ||
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2') | ||
word.tr!('-', '_') | ||
word.downcase! | ||
word | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::MigrationFileName, :config do | ||
let(:filename) { 'db/migrate/20220101050505_create_users.rb' } | ||
|
||
context 'when the file name matches its class' do | ||
context 'with a super class' do | ||
it do | ||
expect_no_offenses(<<~RUBY, filename) | ||
class CreateUsers < ActiveRecord::Migration[7.0] | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'without any super class' do | ||
it do | ||
expect_no_offenses(<<~RUBY, filename) | ||
class CreateUsers | ||
end | ||
RUBY | ||
end | ||
end | ||
end | ||
|
||
context 'when the file name does not match its class' do | ||
it do | ||
expect_offense(<<~RUBY, filename) | ||
class SellBooks < ActiveRecord::Migration[7.0] | ||
^^^^^^^^^ The class name does not match with the file name. | ||
end | ||
RUBY | ||
end | ||
end | ||
end |