From 02cb196e421cafecffdc6991b61cfe9d7908efae Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Wed, 21 May 2008 08:27:20 -0700 Subject: [PATCH] Calling reload on a model now clears the changed attributes --- activerecord/lib/active_record/dirty.rb | 8 ++++++++ activerecord/test/cases/dirty_test.rb | 8 ++++++++ 2 files changed, 16 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/dirty.rb b/activerecord/lib/active_record/dirty.rb index 49f1204..4421aca 100644 --- a/activerecord/lib/active_record/dirty.rb +++ b/activerecord/lib/active_record/dirty.rb @@ -40,6 +40,7 @@ module ActiveRecord base.alias_method_chain :save, :dirty base.alias_method_chain :save!, :dirty base.alias_method_chain :update, :dirty + base.alias_method_chain :reload, :dirty base.superclass_delegating_accessor :partial_updates base.partial_updates = false @@ -84,6 +85,13 @@ module ActiveRecord status end + # reload the record and clears changed attributes. + def reload_with_dirty(*args) #:nodoc: + returning reload_without_dirty(*args) do + changed_attributes.clear + end + end + private # Map of change attr => original value. def changed_attributes diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index 6217897..db7daa9 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -136,6 +136,14 @@ class DirtyTest < ActiveRecord::TestCase assert_raises(ActiveRecord::RecordInvalid) { pirate.save! } check_pirate_after_save_failure(pirate) end + + def test_reload_should_clear_changed_attributes + pirate = Pirate.create!(:catchphrase => "shiver me timbers") + pirate.catchphrase = "*hic*" + assert pirate.changed? + pirate.reload + assert !pirate.changed? + end private def with_partial_updates(klass, on = true) -- 1.5.5.1