aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/timestamp.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-06-09 17:30:00 +0900
committerRyuta Kamizono <kamipo@gmail.com>2018-06-18 19:08:41 +0900
commitcad0b7d91dbc5abf4b0d7fdbcf2d0620557a3b0f (patch)
tree9931a8eb4a452c9de635661cad42353750452c43 /activerecord/lib/active_record/timestamp.rb
parenta865f621ee934e8741001bbed1b487698e44d914 (diff)
downloadrails-cad0b7d91dbc5abf4b0d7fdbcf2d0620557a3b0f.tar.gz
rails-cad0b7d91dbc5abf4b0d7fdbcf2d0620557a3b0f.tar.bz2
rails-cad0b7d91dbc5abf4b0d7fdbcf2d0620557a3b0f.zip
Fix `touch` option to behave consistently with `Persistence#touch` method
`touch` option was added to `increment!` (#27660) and `update_counters` (#26995). But that option behaves inconsistently with `Persistence#touch` method. If `touch` option is passed attribute names, it won't update update_at/on attributes unlike `Persistence#touch` method. Due to changed from `Persistence#touch` to `increment!` with `touch` option, #31405 has a regression that `counter_cache` with `touch` option which is passed attribute names won't update update_at/on attributes. I think that the inconsistency is not intended. To get back consistency, ensure that `touch` option updates update_at/on attributes.
Diffstat (limited to 'activerecord/lib/active_record/timestamp.rb')
-rw-r--r--activerecord/lib/active_record/timestamp.rb22
1 files changed, 14 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb
index e47f06bf3a..d32f971ad1 100644
--- a/activerecord/lib/active_record/timestamp.rb
+++ b/activerecord/lib/active_record/timestamp.rb
@@ -53,12 +53,10 @@ module ActiveRecord
end
module ClassMethods # :nodoc:
- def timestamp_attributes_for_update_in_model
- timestamp_attributes_for_update.select { |c| column_names.include?(c) }
- end
-
- def current_time_from_proper_timezone
- default_timezone == :utc ? Time.now.utc : Time.now
+ def touch_attributes_with_time(*names, time: nil)
+ attribute_names = timestamp_attributes_for_update_in_model
+ attribute_names |= names.map(&:to_s)
+ attribute_names.index_with(time ||= current_time_from_proper_timezone)
end
private
@@ -66,6 +64,10 @@ module ActiveRecord
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
@@ -77,6 +79,10 @@ module ActiveRecord
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
private
@@ -116,7 +122,7 @@ module ActiveRecord
end
def timestamp_attributes_for_update_in_model
- self.class.timestamp_attributes_for_update_in_model
+ self.class.send(:timestamp_attributes_for_update_in_model)
end
def all_timestamp_attributes_in_model
@@ -124,7 +130,7 @@ module ActiveRecord
end
def current_time_from_proper_timezone
- self.class.current_time_from_proper_timezone
+ self.class.send(:current_time_from_proper_timezone)
end
def max_updated_column_timestamp(timestamp_names = timestamp_attributes_for_update_in_model)