mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
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:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user