Class: RuboCop::Cop::Code0::ZeroTrack::Migration::Datetime

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RuboCop::Code0::ZeroTrack::FileHelpers
Defined in:
lib/rubocop/cop/code0/zero_track/migration/datetime.rb

Overview

Cop that checks if datetime data type is added with timezone information.

Constant Summary collapse

MSG =
'Do not use the `%s` data type, use `datetime_with_timezone` instead'

Instance Method Summary collapse

Methods included from RuboCop::Code0::ZeroTrack::FileHelpers

#basename, #dirname, #filepath, #in_migration?

Instance Method Details

#on_def(node) ⇒ Object

Check methods in table creation.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rubocop/cop/code0/zero_track/migration/datetime.rb', line 18

def on_def(node)
  return unless in_migration?(node)

  node.each_descendant(:send) do |send_node|
    method_name = send_node.children[1]

    next unless i[datetime timestamp].include?(method_name)

    add_offense(send_node.loc.selector, message: format(MSG, method_name)) do |corrector|
      corrector.replace(send_node.loc.selector, 'datetime_with_timezone')
    end
  end
end

#on_send(node) ⇒ Object

Check methods.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubocop/cop/code0/zero_track/migration/datetime.rb', line 33

def on_send(node)
  return unless in_migration?(node)

  node.each_descendant do |descendant|
    next unless descendant.type == :sym

    last_argument = descendant.children.last

    next unless i[datetime timestamp].include?(last_argument)

    add_offense(descendant, message: format(MSG, last_argument)) do |corrector|
      corrector.replace(descendant, ':datetime_with_timezone')
    end
  end
end