mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #27660 from akihiro17/updates-timestamp
Add the touch option to ActiveRecord#increment! and decrement!
This commit is contained in:
commit
31a95eda42
4 changed files with 32 additions and 7 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
* Add the touch option to ActiveRecord#increment! and decrement!
|
||||||
|
|
||||||
|
*Hiroaki Izu*
|
||||||
|
|
||||||
* Deprecate passing a class to the `class_name` because it eagerloads more classes than
|
* Deprecate passing a class to the `class_name` because it eagerloads more classes than
|
||||||
necessary and potentially creates circular dependencies.
|
necessary and potentially creates circular dependencies.
|
||||||
|
|
||||||
|
|
|
@ -105,7 +105,8 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
if touch
|
if touch
|
||||||
updates << sanitize_sql_for_assignment(touch_updates(touch))
|
touch_updates = touch_updates(touch)
|
||||||
|
updates << sanitize_sql_for_assignment(touch_updates) unless touch_updates.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
unscoped.where(primary_key => id).update_all updates.join(", ")
|
unscoped.where(primary_key => id).update_all updates.join(", ")
|
||||||
|
|
|
@ -341,11 +341,13 @@ module ActiveRecord
|
||||||
# Wrapper around #increment that writes the update to the database.
|
# Wrapper around #increment that writes the update to the database.
|
||||||
# Only +attribute+ is updated; the record itself is not saved.
|
# Only +attribute+ is updated; the record itself is not saved.
|
||||||
# This means that any other modified attributes will still be dirty.
|
# This means that any other modified attributes will still be dirty.
|
||||||
# Validations and callbacks are skipped. Returns +self+.
|
# Validations and callbacks are skipped. Supports the `touch` option from
|
||||||
def increment!(attribute, by = 1)
|
# +update_counters+, see that for more.
|
||||||
|
# Returns +self+.
|
||||||
|
def increment!(attribute, by = 1, touch: nil)
|
||||||
increment(attribute, by)
|
increment(attribute, by)
|
||||||
change = public_send(attribute) - (attribute_in_database(attribute.to_s) || 0)
|
change = public_send(attribute) - (attribute_in_database(attribute.to_s) || 0)
|
||||||
self.class.update_counters(id, attribute => change)
|
self.class.update_counters(id, attribute => change, touch: touch)
|
||||||
clear_attribute_change(attribute) # eww
|
clear_attribute_change(attribute) # eww
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
@ -360,9 +362,11 @@ module ActiveRecord
|
||||||
# Wrapper around #decrement that writes the update to the database.
|
# Wrapper around #decrement that writes the update to the database.
|
||||||
# Only +attribute+ is updated; the record itself is not saved.
|
# Only +attribute+ is updated; the record itself is not saved.
|
||||||
# This means that any other modified attributes will still be dirty.
|
# This means that any other modified attributes will still be dirty.
|
||||||
# Validations and callbacks are skipped. Returns +self+.
|
# Validations and callbacks are skipped. Supports the `touch` option from
|
||||||
def decrement!(attribute, by = 1)
|
# +update_counters+, see that for more.
|
||||||
increment!(attribute, -by)
|
# Returns +self+.
|
||||||
|
def decrement!(attribute, by = 1, touch: nil)
|
||||||
|
increment!(attribute, -by, touch: touch)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Assigns to +attribute+ the boolean opposite of <tt>attribute?</tt>. So
|
# Assigns to +attribute+ the boolean opposite of <tt>attribute?</tt>. So
|
||||||
|
|
|
@ -139,6 +139,14 @@ class PersistenceTest < ActiveRecord::TestCase
|
||||||
assert_equal initial_credit + 2, a1.reload.credit_limit
|
assert_equal initial_credit + 2, a1.reload.credit_limit
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_increment_updates_timestamps
|
||||||
|
topic = topics(:first)
|
||||||
|
topic.update_columns(updated_at: 5.minutes.ago)
|
||||||
|
previous_updated_at = topic.updated_at
|
||||||
|
topic.increment!(:replies_count, touch: true)
|
||||||
|
assert_operator previous_updated_at, :<, topic.reload.updated_at
|
||||||
|
end
|
||||||
|
|
||||||
def test_destroy_all
|
def test_destroy_all
|
||||||
conditions = "author_name = 'Mary'"
|
conditions = "author_name = 'Mary'"
|
||||||
topics_by_mary = Topic.all.merge!(where: conditions, order: "id").to_a
|
topics_by_mary = Topic.all.merge!(where: conditions, order: "id").to_a
|
||||||
|
@ -230,6 +238,14 @@ class PersistenceTest < ActiveRecord::TestCase
|
||||||
assert_equal 41, accounts(:signals37, :reload).credit_limit
|
assert_equal 41, accounts(:signals37, :reload).credit_limit
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_decrement_updates_timestamps
|
||||||
|
topic = topics(:first)
|
||||||
|
topic.update_columns(updated_at: 5.minutes.ago)
|
||||||
|
previous_updated_at = topic.updated_at
|
||||||
|
topic.decrement!(:replies_count, touch: true)
|
||||||
|
assert_operator previous_updated_at, :<, topic.reload.updated_at
|
||||||
|
end
|
||||||
|
|
||||||
def test_create
|
def test_create
|
||||||
topic = Topic.new
|
topic = Topic.new
|
||||||
topic.title = "New Topic"
|
topic.title = "New Topic"
|
||||||
|
|
Loading…
Reference in a new issue