Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2017-04-24 20:14:09 +0300
committerRobert Speicher <rspeicher@gmail.com>2017-04-28 23:55:53 +0300
commitbbdaf982e6e9b4f674a309df9b16fb9c85498b50 (patch)
treeb9c375e45e5d841a44f616c9ad5b2b9a5f2358d2 /rubocop
parentc3c465ace034d21764a11374902132eeed7a5f5b (diff)
Refactor the AddColumnWithDefault cop to use node matchers
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/migration/add_column_with_default.rb19
1 files changed, 10 insertions, 9 deletions
diff --git a/rubocop/cop/migration/add_column_with_default.rb b/rubocop/cop/migration/add_column_with_default.rb
index 54a920d4b49..e04af8fc096 100644
--- a/rubocop/cop/migration/add_column_with_default.rb
+++ b/rubocop/cop/migration/add_column_with_default.rb
@@ -8,26 +8,27 @@ module RuboCop
class AddColumnWithDefault < 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)
-
- name = node.children[1]
-
- return unless name == :add_column_with_default
+ return unless add_column_with_default?(node)
node.each_ancestor(:def) do |def_node|
- next unless method_name(def_node) == :change
+ next unless defines_change?(def_node)
add_offense(def_node, :name)
end
end
-
- def method_name(node)
- node.children.first
- end
end
end
end