forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reversible_migration_method_definition.rb
66 lines (60 loc) · 1.63 KB
/
reversible_migration_method_definition.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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