aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosh Sharpe <josh.m.sharpe@gmail.com>2009-08-31 12:50:27 -0500
committerJoshua Peek <josh@joshpeek.com>2009-08-31 12:50:27 -0500
commit80989437dc1502f9194b0600941b6d70a3efa3b2 (patch)
treea899f747b89ed6b912ee7218d4f9e01f95fe2d25
parent57f7308da4dff57639f8e67a830baab358aaa5df (diff)
downloadrails-80989437dc1502f9194b0600941b6d70a3efa3b2.tar.gz
rails-80989437dc1502f9194b0600941b6d70a3efa3b2.tar.bz2
rails-80989437dc1502f9194b0600941b6d70a3efa3b2.zip
I added this feature so that a Map of changed fields could be retrieved
after a model had been saved. This is useful in the after_save callback when you need to know what fields changed. At present there is no way to do this other than have code in the before_save callback that takes a copy of the changes Map, which I thought was a bit messy. Example. person = Person.find_by_name('bob') person.name = 'robert' person.changes # => {'name' => ['bob, 'robert']} person.save person.changes # => {} person.previous_changes # => {'name' => ['bob, 'robert']} person.reload person.previous_changes # => {} Signed-off-by: Joshua Peek <josh@joshpeek.com>
-rw-r--r--activemodel/lib/active_model/dirty.rb14
-rw-r--r--activerecord/lib/active_record/attribute_methods/dirty.rb11
-rw-r--r--activerecord/test/cases/dirty_test.rb78
3 files changed, 101 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb
index 624c3647ca..735c61df74 100644
--- a/activemodel/lib/active_model/dirty.rb
+++ b/activemodel/lib/active_model/dirty.rb
@@ -72,12 +72,26 @@ module ActiveModel
changed.inject({}) { |h, attr| h[attr] = attribute_change(attr); h }
end
+ # Map of attributes that were changed when the model was saved.
+ # person.name # => 'bob'
+ # person.name = 'robert'
+ # person.save
+ # person.previous_changes # => {'name' => ['bob, 'robert']}
+ def previous_changes
+ previously_changed_attributes
+ end
+
private
# Map of change <tt>attr => original value</tt>.
def changed_attributes
@changed_attributes ||= {}
end
+ # Map of fields that were changed when the model was saved
+ def previously_changed_attributes
+ @previously_changed || {}
+ end
+
# Handle <tt>*_changed?</tt> for +method_missing+.
def attribute_changed?(attr)
changed_attributes.include?(attr)
diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb
index b6c4df2a49..4df0f1af69 100644
--- a/activerecord/lib/active_record/attribute_methods/dirty.rb
+++ b/activerecord/lib/active_record/attribute_methods/dirty.rb
@@ -19,6 +19,7 @@ module ActiveRecord
# Attempts to +save+ the record and clears changed attributes if successful.
def save_with_dirty(*args) #:nodoc:
if status = save_without_dirty(*args)
+ @previously_changed = changes
changed_attributes.clear
end
status
@@ -26,12 +27,18 @@ module ActiveRecord
# Attempts to <tt>save!</tt> the record and clears changed attributes if successful.
def save_with_dirty!(*args) #:nodoc:
- save_without_dirty!(*args).tap { changed_attributes.clear }
+ save_without_dirty!(*args).tap do
+ @previously_changed = changes
+ changed_attributes.clear
+ end
end
# <tt>reload</tt> the record and clears changed attributes.
def reload_with_dirty(*args) #:nodoc:
- reload_without_dirty(*args).tap { changed_attributes.clear }
+ reload_without_dirty(*args).tap do
+ previously_changed_attributes.clear
+ changed_attributes.clear
+ end
end
private
diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb
index 74571d923a..f456d273fe 100644
--- a/activerecord/test/cases/dirty_test.rb
+++ b/activerecord/test/cases/dirty_test.rb
@@ -308,6 +308,84 @@ class DirtyTest < ActiveRecord::TestCase
end
end
+ def test_previous_changes
+ # original values should be in previous_changes
+ pirate = Pirate.new
+
+ assert_equal Hash.new, pirate.previous_changes
+ pirate.catchphrase = "arrr"
+ pirate.save!
+
+ assert_equal 4, pirate.previous_changes.size
+ assert_equal [nil, "arrr"], pirate.previous_changes['catchphrase']
+ assert_equal [nil, pirate.id], pirate.previous_changes['id']
+ assert_nil pirate.previous_changes['updated_on'][0]
+ assert_not_nil pirate.previous_changes['updated_on'][1]
+ assert_nil pirate.previous_changes['created_on'][0]
+ assert_not_nil pirate.previous_changes['created_on'][1]
+ assert !pirate.previous_changes.key?('parrot_id')
+
+ # original values should be in previous_changes
+ pirate = Pirate.new
+
+ assert_equal Hash.new, pirate.previous_changes
+ pirate.catchphrase = "arrr"
+ pirate.save
+
+ assert_equal 4, pirate.previous_changes.size
+ assert_equal [nil, "arrr"], pirate.previous_changes['catchphrase']
+ assert_equal [nil, pirate.id], pirate.previous_changes['id']
+ assert pirate.previous_changes.include?('updated_on')
+ assert pirate.previous_changes.include?('created_on')
+ assert !pirate.previous_changes.key?('parrot_id')
+
+ pirate.catchphrase = "Yar!!"
+ pirate.reload
+ assert_equal Hash.new, pirate.previous_changes
+
+ pirate = Pirate.find_by_catchphrase("arrr")
+ pirate.catchphrase = "Me Maties!"
+ pirate.save!
+
+ assert_equal 2, pirate.previous_changes.size
+ assert_equal ["arrr", "Me Maties!"], pirate.previous_changes['catchphrase']
+ assert_not_nil pirate.previous_changes['updated_on'][0]
+ assert_not_nil pirate.previous_changes['updated_on'][1]
+ assert !pirate.previous_changes.key?('parrot_id')
+ assert !pirate.previous_changes.key?('created_on')
+
+ pirate = Pirate.find_by_catchphrase("Me Maties!")
+ pirate.catchphrase = "Thar She Blows!"
+ pirate.save
+
+ assert_equal 2, pirate.previous_changes.size
+ assert_equal ["Me Maties!", "Thar She Blows!"], pirate.previous_changes['catchphrase']
+ assert_not_nil pirate.previous_changes['updated_on'][0]
+ assert_not_nil pirate.previous_changes['updated_on'][1]
+ assert !pirate.previous_changes.key?('parrot_id')
+ assert !pirate.previous_changes.key?('created_on')
+
+ pirate = Pirate.find_by_catchphrase("Thar She Blows!")
+ pirate.update_attributes(:catchphrase => "Ahoy!")
+
+ assert_equal 2, pirate.previous_changes.size
+ assert_equal ["Thar She Blows!", "Ahoy!"], pirate.previous_changes['catchphrase']
+ assert_not_nil pirate.previous_changes['updated_on'][0]
+ assert_not_nil pirate.previous_changes['updated_on'][1]
+ assert !pirate.previous_changes.key?('parrot_id')
+ assert !pirate.previous_changes.key?('created_on')
+
+ pirate = Pirate.find_by_catchphrase("Ahoy!")
+ pirate.update_attribute(:catchphrase, "Ninjas suck!")
+
+ assert_equal 2, pirate.previous_changes.size
+ assert_equal ["Ahoy!", "Ninjas suck!"], pirate.previous_changes['catchphrase']
+ assert_not_nil pirate.previous_changes['updated_on'][0]
+ assert_not_nil pirate.previous_changes['updated_on'][1]
+ assert !pirate.previous_changes.key?('parrot_id')
+ assert !pirate.previous_changes.key?('created_on')
+ end
+
private
def with_partial_updates(klass, on = true)
old = klass.partial_updates?