2017-06-13 07:44:13 -04:00
|
|
|
require_relative '../../migration_helpers'
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Migration
|
|
|
|
# Cop that checks if datetime data type is added with timezone information.
|
|
|
|
class Datetime < RuboCop::Cop::Cop
|
|
|
|
include MigrationHelpers
|
|
|
|
|
2017-10-10 05:36:10 -04:00
|
|
|
MSG = 'Do not use the `%s` data type, use `datetime_with_timezone` instead'.freeze
|
2017-06-13 07:44:13 -04:00
|
|
|
|
|
|
|
# Check methods in table creation.
|
|
|
|
def on_def(node)
|
|
|
|
return unless in_migration?(node)
|
|
|
|
|
|
|
|
node.each_descendant(:send) do |send_node|
|
2017-10-10 05:36:10 -04:00
|
|
|
method_name = node.children[1]
|
|
|
|
|
|
|
|
if method_name == :datetime || method_name == :timestamp
|
2017-09-19 11:25:42 -04:00
|
|
|
add_offense(send_node, location: :selector, message: format(MSG, method_name))
|
2017-10-10 05:36:10 -04:00
|
|
|
end
|
2017-06-13 07:44:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check methods.
|
|
|
|
def on_send(node)
|
|
|
|
return unless in_migration?(node)
|
|
|
|
|
|
|
|
node.each_descendant do |descendant|
|
2017-10-10 05:36:10 -04:00
|
|
|
next unless descendant.type == :sym
|
2017-06-13 07:44:13 -04:00
|
|
|
|
2017-10-10 05:36:10 -04:00
|
|
|
last_argument = descendant.children.last
|
|
|
|
|
|
|
|
if last_argument == :datetime || last_argument == :timestamp
|
2017-09-19 11:25:42 -04:00
|
|
|
add_offense(node, location: :expression, message: format(MSG, last_argument))
|
2017-10-10 05:36:10 -04:00
|
|
|
end
|
|
|
|
end
|
2017-06-13 07:44:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|