diff options
author | Robert Speicher <rspeicher@gmail.com> | 2017-04-24 20:16:33 +0300 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2017-04-28 23:55:53 +0300 |
commit | 9c27c90b4a8a9ea179af9588a7dbd2037829905f (patch) | |
tree | 4c7b3691cfabab8f2aa7c138795c5f473238b710 /rubocop/cop/migration/reversible_add_column_with_default.rb | |
parent | bbdaf982e6e9b4f674a309df9b16fb9c85498b50 (diff) |
Rename AddColumnWithDefault to ReversibleAddColumnWithDefault
We're going to add another cop that deals with another aspect of
`add_column_with_default`, so we need to separate them.
Diffstat (limited to 'rubocop/cop/migration/reversible_add_column_with_default.rb')
-rw-r--r-- | rubocop/cop/migration/reversible_add_column_with_default.rb | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/rubocop/cop/migration/reversible_add_column_with_default.rb b/rubocop/cop/migration/reversible_add_column_with_default.rb new file mode 100644 index 00000000000..f413f06f39b --- /dev/null +++ b/rubocop/cop/migration/reversible_add_column_with_default.rb @@ -0,0 +1,35 @@ +require_relative '../../migration_helpers' + +module RuboCop + module Cop + module Migration + # Cop that checks if `add_column_with_default` is used with `up`/`down` methods + # and not `change`. + class ReversibleAddColumnWithDefault < RuboCop::Cop::Cop + include MigrationHelpers + + def_node_matcher :add_column_with_default?, <<~PATTERN + (send nil :add_column_with_default $...) + PATTERN + + def_node_matcher :defines_change?, <<~PATTERN + (def :change ...) + PATTERN + + MSG = '`add_column_with_default` is not reversible so you must manually define ' \ + 'the `up` and `down` methods in your migration class, using `remove_column` in `down`'.freeze + + def on_send(node) + return unless in_migration?(node) + return unless add_column_with_default?(node) + + node.each_ancestor(:def) do |def_node| + next unless defines_change?(def_node) + + add_offense(def_node, :name) + end + end + end + end + end +end |