aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2008-03-29 00:04:27 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2008-03-29 00:04:27 +0000
commitbbf738f269bac83bcc8a0100455c69cba638d877 (patch)
tree8195a8e4c6e7045bfa5fa4d9b6458bf39df0a10d /activerecord/test
parent3704f4ba2e937e32677d461d54fdaa3172a1df8b (diff)
downloadrails-bbf738f269bac83bcc8a0100455c69cba638d877.tar.gz
rails-bbf738f269bac83bcc8a0100455c69cba638d877.tar.bz2
rails-bbf738f269bac83bcc8a0100455c69cba638d877.zip
Track changes to unsaved attributes
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9127 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/dirty_test.rb80
1 files changed, 80 insertions, 0 deletions
diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb
new file mode 100644
index 0000000000..c6777f6bfd
--- /dev/null
+++ b/activerecord/test/cases/dirty_test.rb
@@ -0,0 +1,80 @@
+require 'cases/helper'
+
+# Stub out an AR-alike.
+class DirtyTestSubject
+ def self.table_name; 'people' end
+ def self.primary_key; 'id' end
+ def self.attribute_method_suffix(*suffixes) suffixes end
+
+ def initialize(attrs = {}) @attributes = attrs end
+
+ def save
+ changed_attributes.clear
+ end
+
+ alias_method :save!, :save
+
+ def name; read_attribute('name') end
+ def name=(value); write_attribute('name', value) end
+ def name_was; attribute_was('name') end
+ def name_change; attribute_change('name') end
+ def name_changed?; attribute_changed?('name') end
+
+ private
+ def define_read_methods; nil end
+
+ def read_attribute(attr)
+ @attributes[attr]
+ end
+
+ def write_attribute(attr, value)
+ @attributes[attr] = value
+ end
+end
+
+# Include the module after the class is all set up.
+DirtyTestSubject.module_eval { include ActiveRecord::Dirty }
+
+
+class DirtyTest < Test::Unit::TestCase
+ def test_attribute_changes
+ # New record - no changes.
+ person = DirtyTestSubject.new
+ assert !person.name_changed?
+ assert_nil person.name_change
+
+ # Change name.
+ person.name = 'a'
+ assert person.name_changed?
+ assert_nil person.name_was
+ assert_equal [nil, 'a'], person.name_change
+
+ # Saved - no changes.
+ person.save!
+ assert !person.name_changed?
+ assert_nil person.name_change
+
+ # Same value - no changes.
+ person.name = 'a'
+ assert !person.name_changed?
+ assert_nil person.name_change
+ end
+
+ def test_object_should_be_changed_if_any_attribute_is_changed
+ person = DirtyTestSubject.new
+ assert !person.changed?
+ assert_equal [], person.changed
+ assert_equal Hash.new, person.changes
+
+ person.name = 'a'
+ assert person.changed?
+ assert_nil person.name_was
+ assert_equal %w(name), person.changed
+ assert_equal({'name' => [nil, 'a']}, person.changes)
+
+ person.save
+ assert !person.changed?
+ assert_equal [], person.changed
+ assert_equal({}, person.changes)
+ end
+end