class RuboCop::Cop::Rails::CreateTableWithTimestamps
end
t.datetime :updated_at, default: -> { ‘CURRENT_TIMESTAMP’ }
t.string :email
t.string :name
create_table :users do |t|
# good
end
t.datetime :created_at, default: -> { ‘CURRENT_TIMESTAMP’ }
t.string :email
t.string :name
create_table :users do |t|
# good
end
t.timestamps
t.string :email
t.string :name
create_table :users do |t|
# good
end
t.string :email
t.string :name
create_table :users do |t|
# bad
create_table :users
# bad
@example
In many cases, timestamps are useful information and should be added.
when creating a new table.
This cop checks the migration for which timestamps are not included
def on_send(node)
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
def time_columns_included?(node)
def time_columns_included?(node) timestamps_included?(node) || created_at_or_updated_at_included?(node) end