Sha256: fd9d10b5eb76f2907385f38b6fc73b496c979d30f4bc68703e905fc886340dc1

Contents?: true

Size: 1.79 KB

Versions: 33

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # Makes sure that each migration file defines a migration class
      # whose name matches the file name.
      # (e.g. `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 MigrationClassName < Base
        extend AutoCorrector
        include MigrationsHelper

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

        def on_class(node)
          return unless migration_class?(node)

          basename = basename_without_timestamp_and_suffix(processed_source.file_path)

          class_identifier = node.identifier.location.name
          camelized_basename = camelize(basename)
          return if class_identifier.source.casecmp(camelized_basename).zero?

          message = format(MSG, camelized_basename: camelized_basename)

          add_offense(class_identifier, message: message) do |corrector|
            corrector.replace(class_identifier, camelized_basename)
          end
        end

        private

        def basename_without_timestamp_and_suffix(filepath)
          basename = File.basename(filepath, '.rb')
          basename = remove_gem_suffix(basename)

          basename.sub(/\A\d+_/, '')
        end

        # e.g.: from `add_blobs.active_storage` to `add_blobs`.
        def remove_gem_suffix(file_name)
          file_name.sub(/\..+\z/, '')
        end

        def camelize(word)
          word.split('_').map(&:capitalize).join
        end
      end
    end
  end
end

Version data entries

33 entries across 32 versions & 6 rubygems

Version Path
rubocop-rails-2.29.0 lib/rubocop/cop/rails/migration_class_name.rb
rubocop-rails-2.27.0 lib/rubocop/cop/rails/migration_class_name.rb
rubocop-rails-2.26.2 lib/rubocop/cop/rails/migration_class_name.rb
rubocop-rails-2.26.1 lib/rubocop/cop/rails/migration_class_name.rb
rubocop-rails-2.26.0 lib/rubocop/cop/rails/migration_class_name.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.25.1/lib/rubocop/cop/rails/migration_class_name.rb
rubocop-rails-2.25.1 lib/rubocop/cop/rails/migration_class_name.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.25.0/lib/rubocop/cop/rails/migration_class_name.rb
rubocop-rails-2.24.1 lib/rubocop/cop/rails/migration_class_name.rb
rubocop-rails-2.24.0 lib/rubocop/cop/rails/migration_class_name.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.23.1/lib/rubocop/cop/rails/migration_class_name.rb
rubocop-rails-2.23.1 lib/rubocop/cop/rails/migration_class_name.rb
rubocop-rails-2.23.0 lib/rubocop/cop/rails/migration_class_name.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/migration_class_name.rb
rubocop-rails-2.22.2 lib/rubocop/cop/rails/migration_class_name.rb
rubocop-rails-2.22.1 lib/rubocop/cop/rails/migration_class_name.rb
rubocop-rails-2.22.0 lib/rubocop/cop/rails/migration_class_name.rb
rubocop-rails-2.21.2 lib/rubocop/cop/rails/migration_class_name.rb
rubocop-rails-2.21.1 lib/rubocop/cop/rails/migration_class_name.rb
rubocop-rails-2.21.0 lib/rubocop/cop/rails/migration_class_name.rb