diff options
author | akihiro17 <coolwizard11@gmail.com> | 2017-01-13 01:55:29 +0900 |
---|---|---|
committer | akihiro17 <coolwizard11@gmail.com> | 2017-01-14 01:00:33 +0900 |
commit | bad9bfbea6d6af9dc28583e08a49492668087393 (patch) | |
tree | c7004fc8d7149ff0cda205e3d67a9f831a992ea7 /activerecord/lib/active_record | |
parent | aa0a3d26e338dd293a632e99e815e5ca6fad0d58 (diff) | |
download | rails-bad9bfbea6d6af9dc28583e08a49492668087393.tar.gz rails-bad9bfbea6d6af9dc28583e08a49492668087393.tar.bz2 rails-bad9bfbea6d6af9dc28583e08a49492668087393.zip |
Add the touch option to ActiveRecord#increment! and decrement!
Supports the `touch` option from update_counters.
The default behavior is not to update timestamp columns.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/counter_cache.rb | 3 | ||||
-rw-r--r-- | activerecord/lib/active_record/persistence.rb | 16 |
2 files changed, 12 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb index 93b9371206..cbd71a3779 100644 --- a/activerecord/lib/active_record/counter_cache.rb +++ b/activerecord/lib/active_record/counter_cache.rb @@ -105,7 +105,8 @@ module ActiveRecord end 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 unscoped.where(primary_key => id).update_all updates.join(", ") diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 19fe9632ca..4cd867faae 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -341,11 +341,13 @@ module ActiveRecord # Wrapper around #increment that writes the update to the database. # Only +attribute+ is updated; the record itself is not saved. # This means that any other modified attributes will still be dirty. - # Validations and callbacks are skipped. Returns +self+. - def increment!(attribute, by = 1) + # Validations and callbacks are skipped. Supports the `touch` option from + # +update_counters+, see that for more. + # Returns +self+. + def increment!(attribute, by = 1, touch: nil) increment(attribute, by) 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 self end @@ -360,9 +362,11 @@ module ActiveRecord # Wrapper around #decrement that writes the update to the database. # Only +attribute+ is updated; the record itself is not saved. # This means that any other modified attributes will still be dirty. - # Validations and callbacks are skipped. Returns +self+. - def decrement!(attribute, by = 1) - increment!(attribute, -by) + # Validations and callbacks are skipped. Supports the `touch` option from + # +update_counters+, see that for more. + # Returns +self+. + def decrement!(attribute, by = 1, touch: nil) + increment!(attribute, -by, touch: touch) end # Assigns to +attribute+ the boolean opposite of <tt>attribute?</tt>. So |