class RuboCop::Cop::Rails::CreateTableWithTimestamps

This cop checks the migration for which timestamps are not included when creating a new table. In many cases, timestamps are useful information and should be added.

@example

# bad
create_table :users

# bad
create_table :users do |t|
  t.string :name
  t.string :email
end

# good
create_table :users do |t|
  t.string :name
  t.string :email

  t.timestamps
end

# good
create_table :users do |t|
  t.string :name
  t.string :email

  t.datetime :created_at, default: -> { 'CURRENT_TIMESTAMP' }
end

# good
create_table :users do |t|
  t.string :name
  t.string :email

  t.datetime :updated_at, default: -> { 'CURRENT_TIMESTAMP' }
end

Constants

MSG

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/rails/create_table_with_timestamps.rb, line 67
def on_send(node)
  return unless node.command?(:create_table)

  parent = node.parent

  if create_table_with_block?(parent)
    if parent.body.nil? || !time_columns_included?(parent.body)
      add_offense(parent)
    end
  elsif create_table_with_timestamps_proc?(node)
    # nothing to do
  else
    add_offense(node)
  end
end

Private Instance Methods

time_columns_included?(node) click to toggle source
# File lib/rubocop/cop/rails/create_table_with_timestamps.rb, line 85
def time_columns_included?(node)
  timestamps_included?(node) || created_at_or_updated_at_included?(node)
end