Skip to content

Commit b909f24

Browse files
authored
Merge pull request #665 from koic/fix_a_false_positive_for_rails_migration_class_name_cop
[Fix #664] Fix a false positive for `Rails/MigrationClassName` cop
2 parents b0f4bfd + 0e5a962 commit b909f24

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#664](https://github.com/rubocop/rubocop-rails/issues/664): Fix a false positive for `Rails/MigrationClassName` when `ActiveSupport::Inflector` is applied to the class name and the case is different. ([@koic][])

lib/rubocop/cop/rails/migration_class_name.rb

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,30 @@ class MigrationClassName < Base
2222
extend AutoCorrector
2323
include MigrationsHelper
2424

25-
MSG = 'Replace with `%<corrected_class_name>s` that matches the file name.'
25+
MSG = 'Replace with `%<camelized_basename>s` that matches the file name.'
2626

2727
def on_class(node)
2828
return unless migration_class?(node)
2929

30-
snake_class_name = to_snakecase(node.identifier.source)
30+
basename = basename_without_timestamp_and_suffix(processed_source.file_path)
3131

32-
basename = basename_without_timestamp_and_suffix
33-
return if snake_class_name == basename
32+
class_identifier = node.identifier
33+
camelized_basename = camelize(basename)
34+
return if class_identifier.source.casecmp(camelized_basename).zero?
3435

35-
corrected_class_name = to_camelcase(basename)
36-
message = format(MSG, corrected_class_name: corrected_class_name)
36+
message = format(MSG, camelized_basename: camelized_basename)
3737

38-
add_offense(node.identifier, message: message) do |corrector|
39-
corrector.replace(node.identifier, corrected_class_name)
38+
add_offense(class_identifier, message: message) do |corrector|
39+
corrector.replace(class_identifier, camelized_basename)
4040
end
4141
end
4242

4343
private
4444

45-
def basename_without_timestamp_and_suffix
46-
filepath = processed_source.file_path
45+
def basename_without_timestamp_and_suffix(filepath)
4746
basename = File.basename(filepath, '.rb')
4847
basename = remove_gem_suffix(basename)
48+
4949
basename.sub(/\A\d+_/, '')
5050
end
5151

@@ -54,17 +54,9 @@ def remove_gem_suffix(file_name)
5454
file_name.sub(/\..+\z/, '')
5555
end
5656

57-
def to_camelcase(word)
57+
def camelize(word)
5858
word.split('_').map(&:capitalize).join
5959
end
60-
61-
def to_snakecase(word)
62-
word
63-
.gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
64-
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
65-
.tr('-', '_')
66-
.downcase
67-
end
6860
end
6961
end
7062
end

spec/rubocop/cop/rails/migration_class_name_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,23 @@ class AddBlobs < ActiveRecord::Migration[7.0]
6060
RUBY
6161
end
6262
end
63+
64+
#
65+
# When `OAuth` is applied instead of `Oauth` for `oauth`.
66+
#
67+
# # config/initializers/inflections.rb
68+
# ActiveSupport::Inflector.inflections(:en) do |inflect|
69+
# inflect.acronym 'OAuth'
70+
# end
71+
#
72+
context 'when `ActiveSupport::Inflector` is applied to the class name and the case is different' do
73+
let(:filename) { 'db/migrate/20210623095243_remove_unused_oauth_scope_grants.rb' }
74+
75+
it 'does not register an offense' do
76+
expect_no_offenses(<<~RUBY, filename)
77+
class RemoveUnusedOAuthScopeGrants < ActiveRecord::Migration[7.0]
78+
end
79+
RUBY
80+
end
81+
end
6382
end

0 commit comments

Comments
 (0)