Change `timestamp_attributes_for_{create,update}` from symbol to string
`timestamp_attributes_for_{create,update}` is defined as symbol but
always used as string with `to_s`. This allocates extra strings. To
avoid extra allocation, change the definitions from symbol to string.
```ruby
pp ObjectSpace::AllocationTracer.trace {
1_000.times { |i|
Post.create!
}
}
```
Before:
```
["~/rails/activerecord/lib/active_record/timestamp.rb", 121]=>[1002, 0, 750, 0, 1, 18528],
["~/rails/activerecord/lib/active_record/timestamp.rb", 105]=>[1002, 0, 750, 0, 1, 7720],
["~/rails/activerecord/lib/active_record/timestamp.rb", 101]=>[1002, 0, 750, 0, 1, 7720],
["~/rails/activerecord/lib/active_record/timestamp.rb", 109]=>[1002, 0, 750, 0, 1, 13896],
["~/rails/activerecord/lib/active_record/timestamp.rb", 61]=>[4008, 0, 3000, 0, 1, 30880],
```
After:
```
["~/rails/activerecord/lib/active_record/timestamp.rb", 120]=>[1000, 0, 756, 0, 1, 17184],
["~/rails/activerecord/lib/active_record/timestamp.rb", 104]=>[1000, 0, 756, 0, 1, 7160],
["~/rails/activerecord/lib/active_record/timestamp.rb", 100]=>[1000, 0, 756, 0, 1, 7160],
["~/rails/activerecord/lib/active_record/timestamp.rb", 108]=>[1000, 0, 756, 0, 1, 12888],
```
2016-12-31 16:49:46 -05:00
|
|
|
# frozen_string_literal: true
|
2004-12-15 22:01:11 -05:00
|
|
|
module ActiveRecord
|
2015-07-08 06:16:16 -04:00
|
|
|
# = Active Record \Timestamp
|
2010-08-12 11:04:16 -04:00
|
|
|
#
|
2010-06-16 13:52:35 -04:00
|
|
|
# Active Record automatically timestamps create and update operations if the
|
2010-08-12 11:04:16 -04:00
|
|
|
# table has fields named <tt>created_at/created_on</tt> or
|
2010-06-16 13:52:35 -04:00
|
|
|
# <tt>updated_at/updated_on</tt>.
|
|
|
|
#
|
|
|
|
# Timestamping can be turned off by setting:
|
2004-12-16 12:14:41 -05:00
|
|
|
#
|
2011-02-05 12:07:00 -05:00
|
|
|
# config.active_record.record_timestamps = false
|
2006-06-28 22:39:32 -04:00
|
|
|
#
|
2013-06-14 02:39:03 -04:00
|
|
|
# Timestamps are in UTC by default but you can use the local timezone by setting:
|
2010-06-16 13:52:35 -04:00
|
|
|
#
|
2013-06-14 02:39:03 -04:00
|
|
|
# config.active_record.default_timezone = :local
|
2010-07-29 10:37:19 -04:00
|
|
|
#
|
|
|
|
# == Time Zone aware attributes
|
|
|
|
#
|
2015-09-09 00:43:38 -04:00
|
|
|
# Active Record keeps all the <tt>datetime</tt> and <tt>time</tt> columns
|
2016-01-22 03:51:16 -05:00
|
|
|
# timezone aware. By default, these values are stored in the database as UTC
|
2015-07-08 06:16:16 -04:00
|
|
|
# and converted back to the current <tt>Time.zone</tt> when pulled from the database.
|
2010-07-29 10:37:19 -04:00
|
|
|
#
|
2015-09-09 00:43:38 -04:00
|
|
|
# This feature can be turned off completely by setting:
|
2010-07-29 10:37:19 -04:00
|
|
|
#
|
2015-09-09 00:43:38 -04:00
|
|
|
# config.active_record.time_zone_aware_attributes = false
|
2010-07-29 10:37:19 -04:00
|
|
|
#
|
2015-09-09 00:43:38 -04:00
|
|
|
# You can also specify that only <tt>datetime</tt> columns should be time-zone
|
|
|
|
# aware (while <tt>time</tt> should not) by setting:
|
|
|
|
#
|
|
|
|
# ActiveRecord::Base.time_zone_aware_types = [:datetime]
|
|
|
|
#
|
2016-01-22 03:51:16 -05:00
|
|
|
# You can also add database specific timezone aware types. For example, for PostgreSQL:
|
|
|
|
#
|
|
|
|
# ActiveRecord::Base.time_zone_aware_types += [:tsrange, :tstzrange]
|
|
|
|
#
|
2015-09-09 00:43:38 -04:00
|
|
|
# Finally, you can indicate specific attributes of a model for which time zone
|
|
|
|
# conversion should not applied, for instance by setting:
|
2010-07-29 10:37:19 -04:00
|
|
|
#
|
2011-02-05 12:07:00 -05:00
|
|
|
# class Topic < ActiveRecord::Base
|
|
|
|
# self.skip_time_zone_conversion_for_attributes = [:written_on]
|
|
|
|
# end
|
2006-02-09 14:47:13 -05:00
|
|
|
module Timestamp
|
2009-05-28 12:35:36 -04:00
|
|
|
extend ActiveSupport::Concern
|
2006-06-28 22:39:32 -04:00
|
|
|
|
2009-05-11 22:23:47 -04:00
|
|
|
included do
|
2017-05-29 12:01:50 -04:00
|
|
|
class_attribute :record_timestamps, default: true
|
2006-02-09 14:47:13 -05:00
|
|
|
end
|
2009-04-16 17:48:07 -04:00
|
|
|
|
2012-09-19 01:47:38 -04:00
|
|
|
def initialize_dup(other) # :nodoc:
|
2012-08-16 17:19:47 -04:00
|
|
|
super
|
2014-01-22 09:28:19 -05:00
|
|
|
clear_timestamp_attributes
|
2011-08-29 12:39:09 -04:00
|
|
|
end
|
|
|
|
|
2017-01-02 14:34:43 -05:00
|
|
|
class_methods do
|
|
|
|
private
|
|
|
|
def timestamp_attributes_for_create_in_model
|
|
|
|
timestamp_attributes_for_create.select { |c| column_names.include?(c) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def timestamp_attributes_for_update_in_model
|
|
|
|
timestamp_attributes_for_update.select { |c| column_names.include?(c) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_timestamp_attributes_in_model
|
|
|
|
timestamp_attributes_for_create_in_model + timestamp_attributes_for_update_in_model
|
|
|
|
end
|
|
|
|
|
|
|
|
def timestamp_attributes_for_create
|
|
|
|
["created_at", "created_on"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def timestamp_attributes_for_update
|
|
|
|
["updated_at", "updated_on"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_time_from_proper_timezone
|
|
|
|
default_timezone == :utc ? Time.now.utc : Time.now
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-08 19:06:05 -04:00
|
|
|
private
|
2010-07-13 02:05:09 -04:00
|
|
|
|
2013-07-28 23:52:14 -04:00
|
|
|
def _create_record
|
2016-08-07 19:05:28 -04:00
|
|
|
if record_timestamps
|
2010-05-08 19:06:05 -04:00
|
|
|
current_time = current_time_from_proper_timezone
|
2006-02-09 14:47:13 -05:00
|
|
|
|
2017-01-02 14:34:43 -05:00
|
|
|
all_timestamp_attributes_in_model.each do |column|
|
|
|
|
if !attribute_present?(column)
|
2017-06-18 10:37:06 -04:00
|
|
|
_write_attribute(column, current_time)
|
2011-09-10 16:10:01 -04:00
|
|
|
end
|
2010-07-20 23:52:24 -04:00
|
|
|
end
|
2005-05-19 15:05:12 -04:00
|
|
|
end
|
2004-12-15 22:01:11 -05:00
|
|
|
|
2010-05-08 19:06:05 -04:00
|
|
|
super
|
|
|
|
end
|
2009-04-16 17:48:07 -04:00
|
|
|
|
2014-12-27 18:17:57 -05:00
|
|
|
def _update_record(*args, touch: true, **options)
|
|
|
|
if touch && should_record_timestamps?
|
2010-08-12 11:04:16 -04:00
|
|
|
current_time = current_time_from_proper_timezone
|
|
|
|
|
|
|
|
timestamp_attributes_for_update_in_model.each do |column|
|
Deprecate the behavior of AR::Dirty inside of after_(create|update|save) callbacks
We pretty frequently get bug reports that "dirty is broken inside of
after callbacks". Intuitively they are correct. You'd expect
`Model.after_save { puts changed? }; model.save` to do the same thing as
`model.save; puts model.changed?`, but it does not.
However, changing this goes much farther than just making the behavior
more intuitive. There are a _ton_ of places inside of AR that can be
drastically simplified with this change. Specifically, autosave
associations, timestamps, touch, counter cache, and just about anything
else in AR that works with callbacks have code to try to avoid "double
save" bugs which we will be able to flat out remove with this change.
We introduce two new sets of methods, both with names that are meant to
be more explicit than dirty. The first set maintains the old behavior,
and their names are meant to center that they are about changes that
occurred during the save that just happened. They are equivalent to
`previous_changes` when called outside of after callbacks, or once the
deprecation cycle moves.
The second set is the new behavior. Their names imply that they are
talking about changes from the database representation. The fact that
this is what we really care about became clear when looking at
`BelongsTo.touch_record` when tests were failing. I'm unsure that this
set of methods should be in the public API. Outside of after callbacks,
they are equivalent to the existing methods on dirty.
Dirty itself is not deprecated, nor are the methods inside of it. They
will only emit the warning when called inside of after callbacks. The
scope of this breakage is pretty large, but the migration path is
simple. Given how much this can improve our codebase, and considering
that it makes our API more intuitive, I think it's worth doing.
2016-06-09 10:07:12 -04:00
|
|
|
next if will_save_change_to_attribute?(column)
|
2017-06-18 10:37:06 -04:00
|
|
|
_write_attribute(column, current_time)
|
2010-08-12 11:04:16 -04:00
|
|
|
end
|
|
|
|
end
|
2014-12-28 02:05:53 -05:00
|
|
|
super(*args)
|
2010-06-30 07:14:09 -04:00
|
|
|
end
|
|
|
|
|
2010-08-12 11:04:16 -04:00
|
|
|
def should_record_timestamps?
|
Deprecate the behavior of AR::Dirty inside of after_(create|update|save) callbacks
We pretty frequently get bug reports that "dirty is broken inside of
after callbacks". Intuitively they are correct. You'd expect
`Model.after_save { puts changed? }; model.save` to do the same thing as
`model.save; puts model.changed?`, but it does not.
However, changing this goes much farther than just making the behavior
more intuitive. There are a _ton_ of places inside of AR that can be
drastically simplified with this change. Specifically, autosave
associations, timestamps, touch, counter cache, and just about anything
else in AR that works with callbacks have code to try to avoid "double
save" bugs which we will be able to flat out remove with this change.
We introduce two new sets of methods, both with names that are meant to
be more explicit than dirty. The first set maintains the old behavior,
and their names are meant to center that they are about changes that
occurred during the save that just happened. They are equivalent to
`previous_changes` when called outside of after callbacks, or once the
deprecation cycle moves.
The second set is the new behavior. Their names imply that they are
talking about changes from the database representation. The fact that
this is what we really care about became clear when looking at
`BelongsTo.touch_record` when tests were failing. I'm unsure that this
set of methods should be in the public API. Outside of after callbacks,
they are equivalent to the existing methods on dirty.
Dirty itself is not deprecated, nor are the methods inside of it. They
will only emit the warning when called inside of after callbacks. The
scope of this breakage is pretty large, but the migration path is
simple. Given how much this can improve our codebase, and considering
that it makes our API more intuitive, I think it's worth doing.
2016-06-09 10:07:12 -04:00
|
|
|
record_timestamps && (!partial_writes? || has_changes_to_save?)
|
2010-06-30 07:14:09 -04:00
|
|
|
end
|
2010-05-08 19:06:05 -04:00
|
|
|
|
2011-01-25 17:27:27 -05:00
|
|
|
def timestamp_attributes_for_create_in_model
|
2017-01-02 14:34:43 -05:00
|
|
|
self.class.send(:timestamp_attributes_for_create_in_model)
|
2011-01-25 17:27:27 -05:00
|
|
|
end
|
|
|
|
|
2010-08-12 11:04:16 -04:00
|
|
|
def timestamp_attributes_for_update_in_model
|
2017-01-02 14:34:43 -05:00
|
|
|
self.class.send(:timestamp_attributes_for_update_in_model)
|
2010-07-20 23:52:24 -04:00
|
|
|
end
|
|
|
|
|
2011-01-25 17:33:55 -05:00
|
|
|
def all_timestamp_attributes_in_model
|
2017-01-02 14:34:43 -05:00
|
|
|
self.class.send(:all_timestamp_attributes_in_model)
|
2010-07-20 23:52:24 -04:00
|
|
|
end
|
|
|
|
|
2017-01-02 14:34:43 -05:00
|
|
|
def current_time_from_proper_timezone
|
|
|
|
self.class.send(:current_time_from_proper_timezone)
|
2010-07-02 14:36:13 -04:00
|
|
|
end
|
2010-08-12 11:04:16 -04:00
|
|
|
|
2017-05-13 19:37:37 -04:00
|
|
|
def max_updated_column_timestamp(timestamp_names = timestamp_attributes_for_update_in_model)
|
2014-06-08 20:00:21 -04:00
|
|
|
timestamp_names
|
|
|
|
.map { |attr| self[attr] }
|
|
|
|
.compact
|
|
|
|
.map(&:to_time)
|
|
|
|
.max
|
2013-01-23 04:47:23 -05:00
|
|
|
end
|
|
|
|
|
2011-08-29 12:39:09 -04:00
|
|
|
# Clear attributes and changed_attributes
|
|
|
|
def clear_timestamp_attributes
|
|
|
|
all_timestamp_attributes_in_model.each do |attribute_name|
|
|
|
|
self[attribute_name] = nil
|
2014-07-12 20:30:49 -04:00
|
|
|
clear_attribute_changes([attribute_name])
|
2011-08-29 12:39:09 -04:00
|
|
|
end
|
|
|
|
end
|
2006-02-09 14:47:13 -05:00
|
|
|
end
|
2010-07-02 14:36:13 -04:00
|
|
|
end
|