Partial updates include only unsaved attributes. Off by default; set YourClass.partial_updates = true to enable.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9157 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper
2008-03-31 01:10:04 +00:00
parent 2cf72ad250
commit 6b9448cdd2
13 changed files with 120 additions and 42 deletions

View File

@@ -449,11 +449,12 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
def test_not_resaved_when_unchanged
firm = Firm.find(:first, :include => :account)
firm.name += '-changed'
assert_queries(1) { firm.save! }
firm = Firm.find(:first)
firm.account = Account.find(:first)
assert_queries(1) { firm.save! }
assert_queries(Firm.partial_updates? ? 0 : 1) { firm.save! }
firm = Firm.find(:first).clone
firm.account = Account.find(:first)

View File

@@ -153,6 +153,7 @@ class BasicsTest < ActiveRecord::TestCase
assert_equal 2, Topic.find(topic.id).content["two"]
topic.content_will_change!
topic.content["three"] = 3
topic.save

View File

@@ -1,6 +1,7 @@
require 'cases/helper'
require 'models/topic' # For booleans
require 'models/pirate' # For timestamps
require 'models/parrot'
class Pirate # Just reopening it, not defining it
attr_accessor :detected_changes_in_after_update # Boolean for if changes are detected
@@ -19,7 +20,7 @@ private
end
end
class DirtyTest < Test::Unit::TestCase
class DirtyTest < ActiveRecord::TestCase
def test_attribute_changes
# New record - no changes.
pirate = Pirate.new
@@ -43,7 +44,6 @@ class DirtyTest < Test::Unit::TestCase
assert_nil pirate.catchphrase_change
end
# Rewritten from original tests to use AR
def test_object_should_be_changed_if_any_attribute_is_changed
pirate = Pirate.new
assert !pirate.changed?
@@ -62,6 +62,28 @@ class DirtyTest < Test::Unit::TestCase
assert_equal Hash.new, pirate.changes
end
def test_attribute_will_change!
pirate = Pirate.create!(:catchphrase => 'arr')
pirate.catchphrase << ' matey'
assert !pirate.catchphrase_changed?
assert pirate.catchphrase_will_change!
assert pirate.catchphrase_changed?
assert_equal ['arr matey', 'arr matey'], pirate.catchphrase_change
pirate.catchphrase << '!'
assert pirate.catchphrase_changed?
assert_equal ['arr matey', 'arr matey!'], pirate.catchphrase_change
end
def test_association_assignment_changes_foreign_key
pirate = Pirate.create!
pirate.parrot = Parrot.create!
assert pirate.changed?
assert_equal %w(parrot_id), pirate.changed
end
def test_attribute_should_be_compared_with_type_cast
topic = Topic.new
assert topic.approved?
@@ -74,4 +96,25 @@ class DirtyTest < Test::Unit::TestCase
assert topic.approved?
assert !topic.approved_changed?
end
def test_partial_update
pirate = Pirate.new(:catchphrase => 'foo')
with_partial_updates Pirate, false do
assert_queries(2) { 2.times { pirate.save! } }
end
with_partial_updates Pirate, true do
assert_queries(0) { 2.times { pirate.save! } }
end
end
private
def with_partial_updates(klass, on = true)
old = klass.partial_updates?
klass.partial_updates = on
yield
ensure
klass.partial_updates = old
end
end

View File

@@ -82,7 +82,9 @@ class QueryCacheExpiryTest < ActiveRecord::TestCase
Task.connection.expects(:clear_query_cache).times(2)
Task.cache do
Task.find(1).save!
task = Task.find(1)
task.starting = Time.now.utc
task.save!
end
end