Skip to content

Instantly share code, notes, and snippets.

@AquaGeek
Created May 14, 2011 02:35
Show Gist options
  • Select an option

  • Save AquaGeek/971836 to your computer and use it in GitHub Desktop.

Select an option

Save AquaGeek/971836 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #6702
From 941114ae34e01d3688f840d9497017ee91e7e21f Mon Sep 17 00:00:00 2001
From: James Prior <james@mabonus.net>
Date: Wed, 13 Apr 2011 22:17:25 -0500
Subject: [PATCH] Updating Dirty to no longer mark STI classes as changed when instantiated
---
activerecord/lib/active_record/dirty.rb | 18 +++++++++++++-----
activerecord/test/cases/dirty_test.rb | 16 ++++++++++++++++
2 files changed, 29 insertions(+), 5 deletions(-)
diff --git a/activerecord/lib/active_record/dirty.rb b/activerecord/lib/active_record/dirty.rb
index 1db8fef..d8f2ed5 100644
--- a/activerecord/lib/active_record/dirty.rb
+++ b/activerecord/lib/active_record/dirty.rb
@@ -38,11 +38,12 @@ module ActiveRecord
def self.included(base)
base.attribute_method_suffix *DIRTY_SUFFIXES
- base.alias_method_chain :write_attribute, :dirty
- 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.alias_method_chain :write_attribute, :dirty
+ 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.alias_method_chain :ensure_proper_type, :dirty
base.class_attribute :partial_updates
base.partial_updates = true
@@ -95,6 +96,13 @@ module ActiveRecord
changed_attributes.clear
record
end
+
+ # sets the inheritance column and clears changed attributes
+ def ensure_proper_type_with_dirty(*args) #:nodoc:
+ result = ensure_proper_type_without_dirty(*args)
+ changed_attributes.clear
+ result
+ end
private
# Map of change <tt>attr => original value</tt>.
diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb
index fb3d26f..4e2bf21 100644
--- a/activerecord/test/cases/dirty_test.rb
+++ b/activerecord/test/cases/dirty_test.rb
@@ -148,6 +148,22 @@ class DirtyTest < ActiveRecord::TestCase
assert_equal [], pirate.changed
assert_equal Hash.new, pirate.changes
end
+
+ def test_inherited_object_should_not_be_changed_when_instantiated
+ # Using an STI class here
+ live_parrot = LiveParrot.new
+ assert !live_parrot.changed?
+
+ live_parrot.name = 'Susan'
+ live_parrot.save!
+
+ assert !live_parrot.changed?
+ assert_equal [], live_parrot.changed
+ assert_equal Hash.new, live_parrot.changes
+
+ live_parrot = LiveParrot.find_by_name('Susan')
+ assert !live_parrot.changed?
+ end
def test_attribute_will_change!
pirate = Pirate.create!(:catchphrase => 'arr')
--
1.6.0.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment