aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-08-25 22:54:55 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-08-25 23:02:30 -0300
commit50bdb924ba26999a468ec4844917cefec39ba08c (patch)
treeb3e1ac7383b62cabe07234e911417f88033e8352 /activerecord/test/cases
parenta1d18ed9cf24a8e12066ecb07fd702673ac68e63 (diff)
downloadrails-50bdb924ba26999a468ec4844917cefec39ba08c.tar.gz
rails-50bdb924ba26999a468ec4844917cefec39ba08c.tar.bz2
rails-50bdb924ba26999a468ec4844917cefec39ba08c.zip
Revert "Remove update_attribute."
This reverts commit a7f4b0a1231bf3c65db2ad4066da78c3da5ffb01. Conflicts: activerecord/lib/active_record/associations/has_one_association.rb activerecord/lib/active_record/persistence.rb activerecord/test/cases/base_test.rb activerecord/test/cases/dirty_test.rb activerecord/test/cases/timestamp_test.rb
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r--activerecord/test/cases/dirty_test.rb12
-rw-r--r--activerecord/test/cases/persistence_test.rb40
2 files changed, 51 insertions, 1 deletions
diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb
index 92677b9926..9a2a5a4e3c 100644
--- a/activerecord/test/cases/dirty_test.rb
+++ b/activerecord/test/cases/dirty_test.rb
@@ -509,6 +509,16 @@ class DirtyTest < ActiveRecord::TestCase
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
if ActiveRecord::Base.connection.supports_migrations?
@@ -534,7 +544,7 @@ class DirtyTest < ActiveRecord::TestCase
pirate = target.create(:created_on => created_on)
pirate.reload # Here mysql truncate the usec value to 0
-
+
pirate.created_on = created_on
assert !pirate.created_on_changed?
end
diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb
index 72b8219782..4ffa4836e0 100644
--- a/activerecord/test/cases/persistence_test.rb
+++ b/activerecord/test/cases/persistence_test.rb
@@ -371,10 +371,50 @@ class PersistencesTest < ActiveRecord::TestCase
assert_raise(ActiveSupport::FrozenObjectError) { client.name = "something else" }
end
+ def test_update_attribute
+ assert !Topic.find(1).approved?
+ Topic.find(1).update_attribute("approved", true)
+ assert Topic.find(1).approved?
+
+ Topic.find(1).update_attribute(:approved, false)
+ assert !Topic.find(1).approved?
+ end
+
def test_update_attribute_does_not_choke_on_nil
assert Topic.find(1).update_attributes(nil)
end
+ def test_update_attribute_for_readonly_attribute
+ minivan = Minivan.find('m1')
+ assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_attribute(:color, 'black') }
+ end
+
+ def test_update_attribute_with_one_updated
+ t = Topic.first
+ t.update_attribute(:title, 'super_title')
+ assert_equal 'super_title', t.title
+ assert !t.changed?, "topic should not have changed"
+ assert !t.title_changed?, "title should not have changed"
+ assert_nil t.title_change, 'title change should be nil'
+
+ t.reload
+ assert_equal 'super_title', t.title
+ end
+
+ def test_update_attribute_for_updated_at_on
+ developer = Developer.find(1)
+ prev_month = Time.now.prev_month
+
+ developer.update_attribute(:updated_at, prev_month)
+ assert_equal prev_month, developer.updated_at
+
+ developer.update_attribute(:salary, 80001)
+ assert_not_equal prev_month, developer.updated_at
+
+ developer.reload
+ assert_not_equal prev_month, developer.updated_at
+ end
+
def test_update_column
topic = Topic.find(1)
topic.update_column("approved", true)