Permalink
Browse files

ActiveRecord::Base#reload should clear dirty attributes. [#231 state:…

…resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
1 parent 6e3521e commit 262d23d763c05bbe5f433a99cb9f02e48a8cdd4a @ryanb ryanb committed with lifo May 21, 2008
Showing with 16 additions and 0 deletions.
  1. +8 −0 activerecord/lib/active_record/dirty.rb
  2. +8 −0 activerecord/test/cases/dirty_test.rb
@@ -40,6 +40,7 @@ def self.included(base)
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 @@ def save_with_dirty!(*args) #:nodoc:
status
end
+ # <tt>reload</tt> the record and clears changed attributes.
+ def reload_with_dirty(*args) #:nodoc:
+ record = reload_without_dirty(*args)
+ changed_attributes.clear
+ record
+ end
+
private
# Map of change attr => original value.
def changed_attributes
@@ -137,6 +137,14 @@ def test_changed_attributes_should_be_preserved_if_save_failure
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)
old = klass.partial_updates?

0 comments on commit 262d23d

Please sign in to comment.