Sha256: 75a172d93a73d7a906bda2e167fc6ad413073d06b3e3b970ecfe73444e666b85

Contents?: true

Size: 1.65 KB

Versions: 8

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # Checks whether the migration implements
      # either a `change` method or both an `up` and a `down`
      # method.
      #
      # @example
      #   # bad
      #   class SomeMigration < ActiveRecord::Migration[6.0]
      #     def up
      #       # up migration
      #     end
      #
      #     # <----- missing down method
      #   end
      #
      #   class SomeMigration < ActiveRecord::Migration[6.0]
      #     # <----- missing up method
      #
      #     def down
      #       # down migration
      #     end
      #   end
      #
      #   # good
      #   class SomeMigration < ActiveRecord::Migration[6.0]
      #     def change
      #       # reversible migration
      #     end
      #   end
      #
      #   # good
      #   class SomeMigration < ActiveRecord::Migration[6.0]
      #     def up
      #       # up migration
      #     end
      #
      #     def down
      #       # down migration
      #     end
      #   end
      class ReversibleMigrationMethodDefinition < Base
        include MigrationsHelper

        MSG = 'Migrations must contain either a `change` method, or ' \
              'both an `up` and a `down` method.'

        def_node_matcher :change_method?, <<~PATTERN
          `(def :change (args) _)
        PATTERN

        def_node_matcher :up_and_down_methods?, <<~PATTERN
          [`(def :up (args) _) `(def :down (args) _)]
        PATTERN

        def on_class(node)
          return if !migration_class?(node) || change_method?(node) || up_and_down_methods?(node)

          add_offense(node)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 3 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/reversible_migration_method_definition.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/reversible_migration_method_definition.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/reversible_migration_method_definition.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/reversible_migration_method_definition.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/reversible_migration_method_definition.rb
rubocop-rails-2.15.2 lib/rubocop/cop/rails/reversible_migration_method_definition.rb
rubocop-rails-2.15.1 lib/rubocop/cop/rails/reversible_migration_method_definition.rb
rubocop-rails-2.15.0 lib/rubocop/cop/rails/reversible_migration_method_definition.rb