diff options
author | Vladimir Dementyev <dementiev.vm@gmail.com> | 2019-03-25 18:50:27 -0400 |
---|---|---|
committer | Vladimir Dementyev <dementiev.vm@gmail.com> | 2019-03-25 18:53:07 -0400 |
commit | b574d283e57930105d505c2d34f6d4777dc21069 (patch) | |
tree | 3880e94c61719d355a3aa509451414b8ab6b91b3 /activerecord/lib | |
parent | 61a39ffcc6614d4369f524b5687309d9f12f279f (diff) | |
download | rails-b574d283e57930105d505c2d34f6d4777dc21069.tar.gz rails-b574d283e57930105d505c2d34f6d4777dc21069.tar.bz2 rails-b574d283e57930105d505c2d34f6d4777dc21069.zip |
Add saved changes helpers for store accessors
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/store.rb | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/store.rb b/activerecord/lib/active_record/store.rb index 03225668da..6fecb06897 100644 --- a/activerecord/lib/active_record/store.rb +++ b/activerecord/lib/active_record/store.rb @@ -11,7 +11,9 @@ module ActiveRecord # of the model. This is very helpful for easily exposing store keys to a form or elsewhere that's # already built around just accessing attributes on the model. # - # Every accessor comes with dirty tracking methods (+key_changed?+, +key_was+ and +key_change+). + # Every accessor comes with dirty tracking methods (+key_changed?+, +key_was+ and +key_change+) and + # methods to access the changes made during the last save (+saved_change_to_key?+, +saved_change_to_key+ and + # +key_before_last_save+). # # NOTE: There is no +key_will_change!+ method for accessors, use +store_will_change!+ instead. # @@ -155,6 +157,24 @@ module ActiveRecord prev_store, _new_store = changes[store_attribute] prev_store&.dig(key) end + + define_method("saved_change_to_#{accessor_key}?") do + return false unless saved_change_to_attribute?(store_attribute) + prev_store, new_store = saved_change_to_attribute(store_attribute) + prev_store&.dig(key) != new_store&.dig(key) + end + + define_method("saved_change_to_#{accessor_key}") do + return unless saved_change_to_attribute?(store_attribute) + prev_store, new_store = saved_change_to_attribute(store_attribute) + [prev_store&.dig(key), new_store&.dig(key)] + end + + define_method("#{accessor_key}_before_last_save") do + return unless saved_change_to_attribute?(store_attribute) + prev_store, _new_store = saved_change_to_attribute(store_attribute) + prev_store&.dig(key) + end end end |