1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Rename update_attributes method to update, keep update_attributes as an alias

This commit is contained in:
Amparo Luna + Guillermo Iguaran 2013-01-02 11:46:58 -05:00
parent 08db381d15
commit 1f3a1fedf9
19 changed files with 137 additions and 95 deletions

View file

@ -23,7 +23,7 @@ module ActiveRecord
attributes = construct_join_attributes(record) attributes = construct_join_attributes(record)
if through_record if through_record
through_record.update_attributes(attributes) through_record.update(attributes)
elsif owner.new_record? elsif owner.new_record?
through_proxy.build(attributes) through_proxy.build(attributes)
else else

View file

@ -71,11 +71,11 @@ module ActiveRecord
super(attr, value) super(attr, value)
end end
def update(*) def update_record(*)
partial_writes? ? super(keys_for_partial_write) : super partial_writes? ? super(keys_for_partial_write) : super
end end
def create(*) def create_record(*)
partial_writes? ? super(keys_for_partial_write) : super partial_writes? ? super(keys_for_partial_write) : super
end end

View file

@ -299,11 +299,11 @@ module ActiveRecord
run_callbacks(:save) { super } run_callbacks(:save) { super }
end end
def create #:nodoc: def create_record #:nodoc:
run_callbacks(:create) { super } run_callbacks(:create) { super }
end end
def update(*) #:nodoc: def update_record(*) #:nodoc:
run_callbacks(:update) { super } run_callbacks(:update) { super }
end end
end end

View file

@ -66,7 +66,7 @@ module ActiveRecord
send(lock_col + '=', previous_lock_value + 1) send(lock_col + '=', previous_lock_value + 1)
end end
def update(attribute_names = @attributes.keys) #:nodoc: def update_record(attribute_names = @attributes.keys) #:nodoc:
return super unless locking_enabled? return super unless locking_enabled?
return 0 if attribute_names.empty? return 0 if attribute_names.empty?

View file

@ -212,7 +212,7 @@ module ActiveRecord
# Updates the attributes of the model from the passed-in hash and saves the # Updates the attributes of the model from the passed-in hash and saves the
# record, all wrapped in a transaction. If the object is invalid, the saving # record, all wrapped in a transaction. If the object is invalid, the saving
# will fail and false will be returned. # will fail and false will be returned.
def update_attributes(attributes) def update(attributes)
# The following transaction covers any possible database side-effects of the # The following transaction covers any possible database side-effects of the
# attributes assignment. For example, setting the IDs of a child collection. # attributes assignment. For example, setting the IDs of a child collection.
with_transaction_returning_status do with_transaction_returning_status do
@ -220,10 +220,12 @@ module ActiveRecord
save save
end end
end end
alias update_attributes update
# Updates its receiver just like +update_attributes+ but calls <tt>save!</tt> instead # Updates its receiver just like +update_attributes+ but calls <tt>save!</tt> instead
# of +save+, so an exception is raised if the record is invalid. # of +save+, so an exception is raised if the record is invalid.
def update_attributes!(attributes) def update!(attributes)
# The following transaction covers any possible database side-effects of the # The following transaction covers any possible database side-effects of the
# attributes assignment. For example, setting the IDs of a child collection. # attributes assignment. For example, setting the IDs of a child collection.
with_transaction_returning_status do with_transaction_returning_status do
@ -231,6 +233,8 @@ module ActiveRecord
save! save!
end end
end end
alias update_attributes! update!
# Updates a single attribute of an object, without having to explicitly call save on that object. # Updates a single attribute of an object, without having to explicitly call save on that object.
# #
@ -406,13 +410,13 @@ module ActiveRecord
def create_or_update def create_or_update
raise ReadOnlyRecord if readonly? raise ReadOnlyRecord if readonly?
result = new_record? ? create : update result = new_record? ? create_record : update_record
result != false result != false
end end
# Updates the associated record with values matching those of the instance attributes. # Updates the associated record with values matching those of the instance attributes.
# Returns the number of affected rows. # Returns the number of affected rows.
def update(attribute_names = @attributes.keys) def update_record(attribute_names = @attributes.keys)
attributes_with_values = arel_attributes_with_values_for_update(attribute_names) attributes_with_values = arel_attributes_with_values_for_update(attribute_names)
if attributes_with_values.empty? if attributes_with_values.empty?
@ -426,7 +430,7 @@ module ActiveRecord
# Creates a record with values matching those of the instance attributes # Creates a record with values matching those of the instance attributes
# and returns its id. # and returns its id.
def create(attribute_names = @attributes.keys) def create_record(attribute_names = @attributes.keys)
attributes_values = arel_attributes_with_values_for_create(attribute_names) attributes_values = arel_attributes_with_values_for_create(attribute_names)
new_id = self.class.unscoped.insert attributes_values new_id = self.class.unscoped.insert attributes_values

View file

@ -308,7 +308,7 @@ module ActiveRecord
id.map.with_index { |one_id, idx| update(one_id, attributes[idx]) } id.map.with_index { |one_id, idx| update(one_id, attributes[idx]) }
else else
object = find(id) object = find(id)
object.update_attributes(attributes) object.update(attributes)
object object
end end
end end

View file

@ -43,7 +43,7 @@ module ActiveRecord
private private
def create def create_record
if self.record_timestamps if self.record_timestamps
current_time = current_time_from_proper_timezone current_time = current_time_from_proper_timezone
@ -57,7 +57,7 @@ module ActiveRecord
super super
end end
def update(*args) def update_record(*args)
if should_record_timestamps? if should_record_timestamps?
current_time = current_time_from_proper_timezone current_time = current_time_from_proper_timezone

View file

@ -317,12 +317,12 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
assert_equal 1, Topic.find(topic.id)[:replies_count] assert_equal 1, Topic.find(topic.id)[:replies_count]
end end
def test_belongs_to_counter_after_update_attributes def test_belongs_to_counter_after_update
topic = Topic.create!(:title => "37s") topic = Topic.create!(title: "37s")
topic.replies.create!(:title => "re: 37s", :content => "rails") topic.replies.create!(title: "re: 37s", content: "rails")
assert_equal 1, Topic.find(topic.id)[:replies_count] assert_equal 1, Topic.find(topic.id)[:replies_count]
topic.update_attributes(:title => "37signals") topic.update(title: "37signals")
assert_equal 1, Topic.find(topic.id)[:replies_count] assert_equal 1, Topic.find(topic.id)[:replies_count]
end end

View file

@ -212,8 +212,9 @@ class EagerAssociationTest < ActiveRecord::TestCase
def test_finding_with_includes_on_null_belongs_to_association_with_same_include_includes_only_once def test_finding_with_includes_on_null_belongs_to_association_with_same_include_includes_only_once
post = posts(:welcome) post = posts(:welcome)
post.update_attributes!(:author => nil) post.update!(author: nil)
post = assert_queries(1) { Post.all.merge!(:includes => {:author_with_address => :author_address}).find(post.id) } # find the post, then find the author which is null so no query for the author or address post = assert_queries(1) { Post.all.merge!(includes: {author_with_address: :author_address}).find(post.id) }
# find the post, then find the author which is null so no query for the author or address
assert_no_queries do assert_no_queries do
assert_equal nil, post.author_with_address assert_equal nil, post.author_with_address
end end
@ -221,7 +222,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
def test_finding_with_includes_on_null_belongs_to_polymorphic_association def test_finding_with_includes_on_null_belongs_to_polymorphic_association
sponsor = sponsors(:moustache_club_sponsor_for_groucho) sponsor = sponsors(:moustache_club_sponsor_for_groucho)
sponsor.update_attributes!(:sponsorable => nil) sponsor.update!(sponsorable: nil)
sponsor = assert_queries(1) { Sponsor.all.merge!(:includes => :sponsorable).find(sponsor.id) } sponsor = assert_queries(1) { Sponsor.all.merge!(:includes => :sponsorable).find(sponsor.id) }
assert_no_queries do assert_no_queries do
assert_equal nil, sponsor.sponsorable assert_equal nil, sponsor.sponsorable

View file

@ -706,7 +706,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
def test_can_update_through_association def test_can_update_through_association
assert_nothing_raised do assert_nothing_raised do
people(:michael).posts.first.update_attributes!(:title => "Can write") people(:michael).posts.first.update!(title: "Can write")
end end
end end

View file

@ -161,16 +161,16 @@ class TestDefaultAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCas
end end
def test_callbacks_firing_order_on_update def test_callbacks_firing_order_on_update
eye = Eye.create(:iris_attributes => {:color => 'honey'}) eye = Eye.create(iris_attributes: {color: 'honey'})
eye.update_attributes(:iris_attributes => {:color => 'green'}) eye.update(iris_attributes: {color: 'green'})
assert_equal [true, false], eye.after_update_callbacks_stack assert_equal [true, false], eye.after_update_callbacks_stack
end end
def test_callbacks_firing_order_on_save def test_callbacks_firing_order_on_save
eye = Eye.create(:iris_attributes => {:color => 'honey'}) eye = Eye.create(iris_attributes: {color: 'honey'})
assert_equal [false, false], eye.after_save_callbacks_stack assert_equal [false, false], eye.after_save_callbacks_stack
eye.update_attributes(:iris_attributes => {:color => 'blue'}) eye.update(iris_attributes: {color: 'blue'})
assert_equal [false, false, false, false], eye.after_save_callbacks_stack assert_equal [false, false, false, false], eye.after_save_callbacks_stack
end end
end end

View file

@ -593,7 +593,7 @@ class BasicsTest < ActiveRecord::TestCase
post.reload post.reload
assert_equal "cannot change this", post.title assert_equal "cannot change this", post.title
post.update_attributes(:title => "try to change", :body => "changed") post.update(title: "try to change", body: "changed")
post.reload post.reload
assert_equal "cannot change this", post.title assert_equal "cannot change this", post.title
assert_equal "changed", post.body assert_equal "changed", post.body
@ -1001,7 +1001,7 @@ class BasicsTest < ActiveRecord::TestCase
def test_reload_with_exclusive_scope def test_reload_with_exclusive_scope
dev = DeveloperCalledDavid.first dev = DeveloperCalledDavid.first
dev.update_attributes!( :name => "NotDavid" ) dev.update!(name: "NotDavid" )
assert_equal dev, dev.reload assert_equal dev, dev.reload
end end

View file

@ -517,7 +517,7 @@ class DirtyTest < ActiveRecord::TestCase
assert !pirate.previous_changes.key?('created_on') assert !pirate.previous_changes.key?('created_on')
pirate = Pirate.find_by_catchphrase("Thar She Blows!") pirate = Pirate.find_by_catchphrase("Thar She Blows!")
pirate.update_attributes(:catchphrase => "Ahoy!") pirate.update(catchphrase: "Ahoy!")
assert_equal 2, pirate.previous_changes.size assert_equal 2, pirate.previous_changes.size
assert_equal ["Thar She Blows!", "Ahoy!"], pirate.previous_changes['catchphrase'] assert_equal ["Thar She Blows!", "Ahoy!"], pirate.previous_changes['catchphrase']

View file

@ -207,7 +207,7 @@ class OptimisticLockingTest < ActiveRecord::TestCase
s.reload s.reload
assert_equal "unchangeable name", s.name assert_equal "unchangeable name", s.name
s.update_attributes(:name => "changed name") s.update(name: "changed name")
s.reload s.reload
assert_equal "unchangeable name", s.name assert_equal "unchangeable name", s.name
end end

View file

@ -79,10 +79,10 @@ class TestNestedAttributesInGeneral < ActiveRecord::TestCase
def test_should_disable_allow_destroy_by_default def test_should_disable_allow_destroy_by_default
Pirate.accepts_nested_attributes_for :ship Pirate.accepts_nested_attributes_for :ship
pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?") pirate = Pirate.create!(catchphrase: "Don' botharrr talkin' like one, savvy?")
ship = pirate.create_ship(:name => 'Nights Dirty Lightning') ship = pirate.create_ship(name: 'Nights Dirty Lightning')
pirate.update_attributes(:ship_attributes => { '_destroy' => true, :id => ship.id }) pirate.update(ship_attributes: { '_destroy' => true, :id => ship.id })
assert_nothing_raised(ActiveRecord::RecordNotFound) { pirate.ship.reload } assert_nothing_raised(ActiveRecord::RecordNotFound) { pirate.ship.reload }
end end
@ -125,33 +125,33 @@ class TestNestedAttributesInGeneral < ActiveRecord::TestCase
def test_reject_if_with_a_proc_which_returns_true_always_for_has_one def test_reject_if_with_a_proc_which_returns_true_always_for_has_one
Pirate.accepts_nested_attributes_for :ship, :reject_if => proc {|attributes| true } Pirate.accepts_nested_attributes_for :ship, :reject_if => proc {|attributes| true }
pirate = Pirate.new(:catchphrase => "Stop wastin' me time") pirate = Pirate.new(catchphrase: "Stop wastin' me time")
ship = pirate.create_ship(:name => 's1') ship = pirate.create_ship(name: 's1')
pirate.update_attributes({:ship_attributes => { :name => 's2', :id => ship.id } }) pirate.update({ship_attributes: { name: 's2', id: ship.id } })
assert_equal 's1', ship.reload.name assert_equal 's1', ship.reload.name
end end
def test_reject_if_with_a_proc_which_returns_true_always_for_has_many def test_reject_if_with_a_proc_which_returns_true_always_for_has_many
Man.accepts_nested_attributes_for :interests, :reject_if => proc {|attributes| true } Man.accepts_nested_attributes_for :interests, :reject_if => proc {|attributes| true }
man = Man.create(:name => "John") man = Man.create(name: "John")
interest = man.interests.create(:topic => 'photography') interest = man.interests.create(topic: 'photography')
man.update_attributes({:interests_attributes => { :topic => 'gardening', :id => interest.id } }) man.update({interests_attributes: { topic: 'gardening', id: interest.id } })
assert_equal 'photography', interest.reload.topic assert_equal 'photography', interest.reload.topic
end end
def test_destroy_works_independent_of_reject_if def test_destroy_works_independent_of_reject_if
Man.accepts_nested_attributes_for :interests, :reject_if => proc {|attributes| true }, :allow_destroy => true Man.accepts_nested_attributes_for :interests, :reject_if => proc {|attributes| true }, :allow_destroy => true
man = Man.create(:name => "Jon") man = Man.create(name: "Jon")
interest = man.interests.create(:topic => 'the ladies') interest = man.interests.create(topic: 'the ladies')
man.update_attributes({:interests_attributes => { :_destroy => "1", :id => interest.id } }) man.update({interests_attributes: { _destroy: "1", id: interest.id } })
assert man.reload.interests.empty? assert man.reload.interests.empty?
end end
def test_has_many_association_updating_a_single_record def test_has_many_association_updating_a_single_record
Man.accepts_nested_attributes_for(:interests) Man.accepts_nested_attributes_for(:interests)
man = Man.create(:name => 'John') man = Man.create(name: 'John')
interest = man.interests.create(:topic => 'photography') interest = man.interests.create(topic: 'photography')
man.update_attributes({:interests_attributes => {:topic => 'gardening', :id => interest.id}}) man.update({interests_attributes: {topic: 'gardening', id: interest.id}})
assert_equal 'gardening', interest.reload.topic assert_equal 'gardening', interest.reload.topic
end end
@ -284,8 +284,8 @@ class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase
@pirate.ship.destroy @pirate.ship.destroy
[1, '1', true, 'true'].each do |truth| [1, '1', true, 'true'].each do |truth|
ship = @pirate.reload.create_ship(:name => 'Mister Pablo') ship = @pirate.reload.create_ship(name: 'Mister Pablo')
@pirate.update_attributes(:ship_attributes => { :id => ship.id, :_destroy => truth }) @pirate.update(ship_attributes: { id: ship.id, _destroy: truth })
assert_nil @pirate.reload.ship assert_nil @pirate.reload.ship
assert_raise(ActiveRecord::RecordNotFound) { Ship.find(ship.id) } assert_raise(ActiveRecord::RecordNotFound) { Ship.find(ship.id) }
@ -294,7 +294,7 @@ class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase
def test_should_not_destroy_an_existing_record_if_destroy_is_not_truthy def test_should_not_destroy_an_existing_record_if_destroy_is_not_truthy
[nil, '0', 0, 'false', false].each do |not_truth| [nil, '0', 0, 'false', false].each do |not_truth|
@pirate.update_attributes(:ship_attributes => { :id => @pirate.ship.id, :_destroy => not_truth }) @pirate.update(ship_attributes: { id: @pirate.ship.id, _destroy: not_truth })
assert_equal @ship, @pirate.reload.ship assert_equal @ship, @pirate.reload.ship
end end
@ -303,7 +303,7 @@ class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase
def test_should_not_destroy_an_existing_record_if_allow_destroy_is_false def test_should_not_destroy_an_existing_record_if_allow_destroy_is_false
Pirate.accepts_nested_attributes_for :ship, :allow_destroy => false, :reject_if => proc { |attributes| attributes.empty? } Pirate.accepts_nested_attributes_for :ship, :allow_destroy => false, :reject_if => proc { |attributes| attributes.empty? }
@pirate.update_attributes(:ship_attributes => { :id => @pirate.ship.id, :_destroy => '1' }) @pirate.update(ship_attributes: { id: @pirate.ship.id, _destroy: '1' })
assert_equal @ship, @pirate.reload.ship assert_equal @ship, @pirate.reload.ship
@ -317,8 +317,8 @@ class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase
assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
end end
def test_should_work_with_update_attributes_as_well def test_should_work_with_update_as_well
@pirate.update_attributes({ :catchphrase => 'Arr', :ship_attributes => { :id => @ship.id, :name => 'Mister Pablo' } }) @pirate.update({ catchphrase: 'Arr', ship_attributes: { id: @ship.id, name: 'Mister Pablo' } })
@pirate.reload @pirate.reload
assert_equal 'Arr', @pirate.catchphrase assert_equal 'Arr', @pirate.catchphrase
@ -342,22 +342,22 @@ class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase
end end
def test_should_accept_update_only_option def test_should_accept_update_only_option
@pirate.update_attributes(:update_only_ship_attributes => { :id => @pirate.ship.id, :name => 'Mayflower' }) @pirate.update(update_only_ship_attributes: { id: @pirate.ship.id, name: 'Mayflower' })
end end
def test_should_create_new_model_when_nothing_is_there_and_update_only_is_true def test_should_create_new_model_when_nothing_is_there_and_update_only_is_true
@ship.delete @ship.delete
@pirate.reload.update_attributes(:update_only_ship_attributes => { :name => 'Mayflower' }) @pirate.reload.update(update_only_ship_attributes: { name: 'Mayflower' })
assert_not_nil @pirate.ship assert_not_nil @pirate.ship
end end
def test_should_update_existing_when_update_only_is_true_and_no_id_is_given def test_should_update_existing_when_update_only_is_true_and_no_id_is_given
@ship.delete @ship.delete
@ship = @pirate.create_update_only_ship(:name => 'Nights Dirty Lightning') @ship = @pirate.create_update_only_ship(name: 'Nights Dirty Lightning')
@pirate.update_attributes(:update_only_ship_attributes => { :name => 'Mayflower' }) @pirate.update(update_only_ship_attributes: { name: 'Mayflower' })
assert_equal 'Mayflower', @ship.reload.name assert_equal 'Mayflower', @ship.reload.name
assert_equal @ship, @pirate.reload.ship assert_equal @ship, @pirate.reload.ship
@ -365,9 +365,9 @@ class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase
def test_should_update_existing_when_update_only_is_true_and_id_is_given def test_should_update_existing_when_update_only_is_true_and_id_is_given
@ship.delete @ship.delete
@ship = @pirate.create_update_only_ship(:name => 'Nights Dirty Lightning') @ship = @pirate.create_update_only_ship(name: 'Nights Dirty Lightning')
@pirate.update_attributes(:update_only_ship_attributes => { :name => 'Mayflower', :id => @ship.id }) @pirate.update(update_only_ship_attributes: { name: 'Mayflower', id: @ship.id })
assert_equal 'Mayflower', @ship.reload.name assert_equal 'Mayflower', @ship.reload.name
assert_equal @ship, @pirate.reload.ship assert_equal @ship, @pirate.reload.ship
@ -376,9 +376,9 @@ class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase
def test_should_destroy_existing_when_update_only_is_true_and_id_is_given_and_is_marked_for_destruction def test_should_destroy_existing_when_update_only_is_true_and_id_is_given_and_is_marked_for_destruction
Pirate.accepts_nested_attributes_for :update_only_ship, :update_only => true, :allow_destroy => true Pirate.accepts_nested_attributes_for :update_only_ship, :update_only => true, :allow_destroy => true
@ship.delete @ship.delete
@ship = @pirate.create_update_only_ship(:name => 'Nights Dirty Lightning') @ship = @pirate.create_update_only_ship(name: 'Nights Dirty Lightning')
@pirate.update_attributes(:update_only_ship_attributes => { :name => 'Mayflower', :id => @ship.id, :_destroy => true }) @pirate.update(update_only_ship_attributes: { name: 'Mayflower', id: @ship.id, _destroy: true })
assert_nil @pirate.reload.ship assert_nil @pirate.reload.ship
assert_raise(ActiveRecord::RecordNotFound) { Ship.find(@ship.id) } assert_raise(ActiveRecord::RecordNotFound) { Ship.find(@ship.id) }
@ -468,15 +468,15 @@ class TestNestedAttributesOnABelongsToAssociation < ActiveRecord::TestCase
def test_should_destroy_an_existing_record_if_there_is_a_matching_id_and_destroy_is_truthy def test_should_destroy_an_existing_record_if_there_is_a_matching_id_and_destroy_is_truthy
@ship.pirate.destroy @ship.pirate.destroy
[1, '1', true, 'true'].each do |truth| [1, '1', true, 'true'].each do |truth|
pirate = @ship.reload.create_pirate(:catchphrase => 'Arr') pirate = @ship.reload.create_pirate(catchphrase: 'Arr')
@ship.update_attributes(:pirate_attributes => { :id => pirate.id, :_destroy => truth }) @ship.update(pirate_attributes: { id: pirate.id, _destroy: truth })
assert_raise(ActiveRecord::RecordNotFound) { pirate.reload } assert_raise(ActiveRecord::RecordNotFound) { pirate.reload }
end end
end end
def test_should_unset_association_when_an_existing_record_is_destroyed def test_should_unset_association_when_an_existing_record_is_destroyed
original_pirate_id = @ship.pirate.id original_pirate_id = @ship.pirate.id
@ship.update_attributes! pirate_attributes: { id: @ship.pirate.id, _destroy: true } @ship.update! pirate_attributes: { id: @ship.pirate.id, _destroy: true }
assert_empty Pirate.where(id: original_pirate_id) assert_empty Pirate.where(id: original_pirate_id)
assert_nil @ship.pirate_id assert_nil @ship.pirate_id
@ -490,7 +490,7 @@ class TestNestedAttributesOnABelongsToAssociation < ActiveRecord::TestCase
def test_should_not_destroy_an_existing_record_if_destroy_is_not_truthy def test_should_not_destroy_an_existing_record_if_destroy_is_not_truthy
[nil, '0', 0, 'false', false].each do |not_truth| [nil, '0', 0, 'false', false].each do |not_truth|
@ship.update_attributes(:pirate_attributes => { :id => @ship.pirate.id, :_destroy => not_truth }) @ship.update(pirate_attributes: { id: @ship.pirate.id, _destroy: not_truth })
assert_nothing_raised(ActiveRecord::RecordNotFound) { @ship.pirate.reload } assert_nothing_raised(ActiveRecord::RecordNotFound) { @ship.pirate.reload }
end end
end end
@ -498,14 +498,14 @@ class TestNestedAttributesOnABelongsToAssociation < ActiveRecord::TestCase
def test_should_not_destroy_an_existing_record_if_allow_destroy_is_false def test_should_not_destroy_an_existing_record_if_allow_destroy_is_false
Ship.accepts_nested_attributes_for :pirate, :allow_destroy => false, :reject_if => proc { |attributes| attributes.empty? } Ship.accepts_nested_attributes_for :pirate, :allow_destroy => false, :reject_if => proc { |attributes| attributes.empty? }
@ship.update_attributes(:pirate_attributes => { :id => @ship.pirate.id, :_destroy => '1' }) @ship.update(pirate_attributes: { id: @ship.pirate.id, _destroy: '1' })
assert_nothing_raised(ActiveRecord::RecordNotFound) { @ship.pirate.reload } assert_nothing_raised(ActiveRecord::RecordNotFound) { @ship.pirate.reload }
ensure ensure
Ship.accepts_nested_attributes_for :pirate, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? } Ship.accepts_nested_attributes_for :pirate, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
end end
def test_should_work_with_update_attributes_as_well def test_should_work_with_update_as_well
@ship.update_attributes({ :name => 'Mister Pablo', :pirate_attributes => { :catchphrase => 'Arr' } }) @ship.update({ name: 'Mister Pablo', pirate_attributes: { catchphrase: 'Arr' } })
@ship.reload @ship.reload
assert_equal 'Mister Pablo', @ship.name assert_equal 'Mister Pablo', @ship.name
@ -534,18 +534,18 @@ class TestNestedAttributesOnABelongsToAssociation < ActiveRecord::TestCase
def test_should_update_existing_when_update_only_is_true_and_no_id_is_given def test_should_update_existing_when_update_only_is_true_and_no_id_is_given
@pirate.delete @pirate.delete
@pirate = @ship.create_update_only_pirate(:catchphrase => 'Aye') @pirate = @ship.create_update_only_pirate(catchphrase: 'Aye')
@ship.update_attributes(:update_only_pirate_attributes => { :catchphrase => 'Arr' }) @ship.update(update_only_pirate_attributes: { catchphrase: 'Arr' })
assert_equal 'Arr', @pirate.reload.catchphrase assert_equal 'Arr', @pirate.reload.catchphrase
assert_equal @pirate, @ship.reload.update_only_pirate assert_equal @pirate, @ship.reload.update_only_pirate
end end
def test_should_update_existing_when_update_only_is_true_and_id_is_given def test_should_update_existing_when_update_only_is_true_and_id_is_given
@pirate.delete @pirate.delete
@pirate = @ship.create_update_only_pirate(:catchphrase => 'Aye') @pirate = @ship.create_update_only_pirate(catchphrase: 'Aye')
@ship.update_attributes(:update_only_pirate_attributes => { :catchphrase => 'Arr', :id => @pirate.id }) @ship.update(update_only_pirate_attributes: { catchphrase: 'Arr', id: @pirate.id })
assert_equal 'Arr', @pirate.reload.catchphrase assert_equal 'Arr', @pirate.reload.catchphrase
assert_equal @pirate, @ship.reload.update_only_pirate assert_equal @pirate, @ship.reload.update_only_pirate
@ -554,9 +554,9 @@ class TestNestedAttributesOnABelongsToAssociation < ActiveRecord::TestCase
def test_should_destroy_existing_when_update_only_is_true_and_id_is_given_and_is_marked_for_destruction def test_should_destroy_existing_when_update_only_is_true_and_id_is_given_and_is_marked_for_destruction
Ship.accepts_nested_attributes_for :update_only_pirate, :update_only => true, :allow_destroy => true Ship.accepts_nested_attributes_for :update_only_pirate, :update_only => true, :allow_destroy => true
@pirate.delete @pirate.delete
@pirate = @ship.create_update_only_pirate(:catchphrase => 'Aye') @pirate = @ship.create_update_only_pirate(catchphrase: 'Aye')
@ship.update_attributes(:update_only_pirate_attributes => { :catchphrase => 'Arr', :id => @pirate.id, :_destroy => true }) @ship.update(update_only_pirate_attributes: { catchphrase: 'Arr', id: @pirate.id, _destroy: true })
assert_raise(ActiveRecord::RecordNotFound) { @pirate.reload } assert_raise(ActiveRecord::RecordNotFound) { @pirate.reload }
@ -582,7 +582,7 @@ module NestedAttributesOnACollectionAssociationTests
def test_should_take_a_hash_with_string_keys_and_assign_the_attributes_to_the_associated_models def test_should_take_a_hash_with_string_keys_and_assign_the_attributes_to_the_associated_models
@alternate_params[association_getter].stringify_keys! @alternate_params[association_getter].stringify_keys!
@pirate.update_attributes @alternate_params @pirate.update @alternate_params
assert_equal ['Grace OMalley', 'Privateers Greed'], [@child_1.reload.name, @child_2.reload.name] assert_equal ['Grace OMalley', 'Privateers Greed'], [@child_1.reload.name, @child_2.reload.name]
end end
@ -628,10 +628,10 @@ module NestedAttributesOnACollectionAssociationTests
def test_should_refresh_saved_records_when_not_overwriting_unsaved_updates def test_should_refresh_saved_records_when_not_overwriting_unsaved_updates
@pirate.reload @pirate.reload
record = @pirate.class.reflect_on_association(@association_name).klass.new(:name => 'Grace OMalley') record = @pirate.class.reflect_on_association(@association_name).klass.new(name: 'Grace OMalley')
@pirate.send(@association_name) << record @pirate.send(@association_name) << record
record.save! record.save!
@pirate.send(@association_name).last.update_attributes!(:name => 'Polly') @pirate.send(@association_name).last.update!(name: 'Polly')
assert_equal 'Polly', @pirate.send(@association_name).send(:load_target).last.name assert_equal 'Polly', @pirate.send(@association_name).send(:load_target).last.name
end end
@ -718,17 +718,17 @@ module NestedAttributesOnACollectionAssociationTests
end end
end end
def test_should_work_with_update_attributes_as_well def test_should_work_with_update_as_well
@pirate.update_attributes(:catchphrase => 'Arr', @pirate.update(catchphrase: 'Arr',
association_getter => { 'foo' => { :id => @child_1.id, :name => 'Grace OMalley' }}) association_getter => { 'foo' => { :id => @child_1.id, :name => 'Grace OMalley' }})
assert_equal 'Grace OMalley', @child_1.reload.name assert_equal 'Grace OMalley', @child_1.reload.name
end end
def test_should_update_existing_records_and_add_new_ones_that_have_no_id def test_should_update_existing_records_and_add_new_ones_that_have_no_id
@alternate_params[association_getter]['baz'] = { :name => 'Buccaneers Servant' } @alternate_params[association_getter]['baz'] = { name: 'Buccaneers Servant' }
assert_difference('@pirate.send(@association_name).count', +1) do assert_difference('@pirate.send(@association_name).count', +1) do
@pirate.update_attributes @alternate_params @pirate.update @alternate_params
end end
assert_equal ['Grace OMalley', 'Privateers Greed', 'Buccaneers Servant'].to_set, @pirate.reload.send(@association_name).map(&:name).to_set assert_equal ['Grace OMalley', 'Privateers Greed', 'Buccaneers Servant'].to_set, @pirate.reload.send(@association_name).map(&:name).to_set
end end
@ -750,7 +750,7 @@ module NestedAttributesOnACollectionAssociationTests
[nil, '', '0', 0, 'false', false].each do |false_variable| [nil, '', '0', 0, 'false', false].each do |false_variable|
@alternate_params[association_getter]['foo']['_destroy'] = false_variable @alternate_params[association_getter]['foo']['_destroy'] = false_variable
assert_no_difference('@pirate.send(@association_name).count') do assert_no_difference('@pirate.send(@association_name).count') do
@pirate.update_attributes(@alternate_params) @pirate.update(@alternate_params)
end end
end end
end end
@ -814,7 +814,7 @@ module NestedAttributesOnACollectionAssociationTests
man = Man.create(name: 'John') man = Man.create(name: 'John')
interest = man.interests.create(topic: 'bar', zine_id: 0) interest = man.interests.create(topic: 'bar', zine_id: 0)
assert interest.save assert interest.save
assert !man.update_attributes({interests_attributes: { id: interest.id, zine_id: 'foo' }}) assert !man.update({interests_attributes: { id: interest.id, zine_id: 'foo' }})
end end
end end
@ -945,18 +945,18 @@ class TestNestedAttributesWithNonStandardPrimaryKeys < ActiveRecord::TestCase
end end
def test_should_update_existing_records_with_non_standard_primary_key def test_should_update_existing_records_with_non_standard_primary_key
@owner.update_attributes(@params) @owner.update(@params)
assert_equal ['Foo', 'Bar'], @owner.pets.map(&:name) assert_equal ['Foo', 'Bar'], @owner.pets.map(&:name)
end end
def test_attr_accessor_of_child_should_be_value_provided_during_update_attributes def test_attr_accessor_of_child_should_be_value_provided_during_update
@owner = owners(:ashley) @owner = owners(:ashley)
@pet1 = pets(:chew) @pet1 = pets(:chew)
attributes = {:pets_attributes => { "1"=> { :id => @pet1.id, attributes = {:pets_attributes => { "1"=> { :id => @pet1.id,
:name => "Foo2", :name => "Foo2",
:current_user => "John", :current_user => "John",
:_destroy=>true }}} :_destroy=>true }}}
@owner.update_attributes(attributes) @owner.update(attributes)
assert_equal 'John', Pet.after_destroy_output assert_equal 'John', Pet.after_destroy_output
end end

View file

@ -391,7 +391,7 @@ class PersistencesTest < ActiveRecord::TestCase
end end
def test_update_attribute_does_not_choke_on_nil def test_update_attribute_does_not_choke_on_nil
assert Topic.find(1).update_attributes(nil) assert Topic.find(1).update(nil)
end end
def test_update_attribute_for_readonly_attribute def test_update_attribute_for_readonly_attribute
@ -547,7 +547,7 @@ class PersistencesTest < ActiveRecord::TestCase
def test_update_columns_should_not_leave_the_object_dirty def test_update_columns_should_not_leave_the_object_dirty
topic = Topic.find(1) topic = Topic.find(1)
topic.update_attributes({ "content" => "Have a nice day", :author_name => "Jose" }) topic.update({ "content" => "Have a nice day", :author_name => "Jose" })
topic.reload topic.reload
topic.update_columns({ content: "You too", "author_name" => "Sebastian" }) topic.update_columns({ content: "You too", "author_name" => "Sebastian" })
@ -631,6 +631,22 @@ class PersistencesTest < ActiveRecord::TestCase
assert developer.update_columns(name: 'Will'), 'did not update record due to default scope' assert developer.update_columns(name: 'Will'), 'did not update record due to default scope'
end end
def test_update
topic = Topic.find(1)
assert !topic.approved?
assert_equal "The First Topic", topic.title
topic.update("approved" => true, "title" => "The First Topic Updated")
topic.reload
assert topic.approved?
assert_equal "The First Topic Updated", topic.title
topic.update(approved: false, title: "The First Topic")
topic.reload
assert !topic.approved?
assert_equal "The First Topic", topic.title
end
def test_update_attributes def test_update_attributes
topic = Topic.find(1) topic = Topic.find(1)
assert !topic.approved? assert !topic.approved?
@ -641,12 +657,33 @@ class PersistencesTest < ActiveRecord::TestCase
assert topic.approved? assert topic.approved?
assert_equal "The First Topic Updated", topic.title assert_equal "The First Topic Updated", topic.title
topic.update_attributes(:approved => false, :title => "The First Topic") topic.update_attributes(approved: false, title: "The First Topic")
topic.reload topic.reload
assert !topic.approved? assert !topic.approved?
assert_equal "The First Topic", topic.title assert_equal "The First Topic", topic.title
end end
def test_update!
Reply.validates_presence_of(:title)
reply = Reply.find(2)
assert_equal "The Second Topic of the day", reply.title
assert_equal "Have a nice day", reply.content
reply.update!("title" => "The Second Topic of the day updated", "content" => "Have a nice evening")
reply.reload
assert_equal "The Second Topic of the day updated", reply.title
assert_equal "Have a nice evening", reply.content
reply.update!(title: "The Second Topic of the day", content: "Have a nice day")
reply.reload
assert_equal "The Second Topic of the day", reply.title
assert_equal "Have a nice day", reply.content
assert_raise(ActiveRecord::RecordInvalid) { reply.update!(title: nil, content: "Have a nice evening") }
ensure
Reply.reset_callbacks(:validate)
end
def test_update_attributes! def test_update_attributes!
Reply.validates_presence_of(:title) Reply.validates_presence_of(:title)
reply = Reply.find(2) reply = Reply.find(2)
@ -658,12 +695,12 @@ class PersistencesTest < ActiveRecord::TestCase
assert_equal "The Second Topic of the day updated", reply.title assert_equal "The Second Topic of the day updated", reply.title
assert_equal "Have a nice evening", reply.content assert_equal "Have a nice evening", reply.content
reply.update_attributes!(:title => "The Second Topic of the day", :content => "Have a nice day") reply.update_attributes!(title: "The Second Topic of the day", content: "Have a nice day")
reply.reload reply.reload
assert_equal "The Second Topic of the day", reply.title assert_equal "The Second Topic of the day", reply.title
assert_equal "Have a nice day", reply.content assert_equal "Have a nice day", reply.content
assert_raise(ActiveRecord::RecordInvalid) { reply.update_attributes!(:title => nil, :content => "Have a nice evening") } assert_raise(ActiveRecord::RecordInvalid) { reply.update_attributes!(title: nil, content: "Have a nice evening") }
ensure ensure
Reply.reset_callbacks(:validate) Reply.reset_callbacks(:validate)
end end

View file

@ -77,7 +77,7 @@ class TransactionIsolationTest < ActiveRecord::TestCase
Tag.transaction(isolation: :repeatable_read) do Tag.transaction(isolation: :repeatable_read) do
tag.reload tag.reload
Tag2.find(tag.id).update_attributes(name: 'emily') Tag2.find(tag.id).update(name: 'emily')
tag.reload tag.reload
assert_equal 'jon', tag.name assert_equal 'jon', tag.name

View file

@ -117,21 +117,21 @@ class TransactionTest < ActiveRecord::TestCase
assert !Topic.find(1).approved? assert !Topic.find(1).approved?
end end
def test_update_attributes_should_rollback_on_failure def test_update_should_rollback_on_failure
author = Author.find(1) author = Author.find(1)
posts_count = author.posts.size posts_count = author.posts.size
assert posts_count > 0 assert posts_count > 0
status = author.update_attributes(:name => nil, :post_ids => []) status = author.update(name: nil, post_ids: [])
assert !status assert !status
assert_equal posts_count, author.posts(true).size assert_equal posts_count, author.posts(true).size
end end
def test_update_attributes_should_rollback_on_failure! def test_update_should_rollback_on_failure!
author = Author.find(1) author = Author.find(1)
posts_count = author.posts.size posts_count = author.posts.size
assert posts_count > 0 assert posts_count > 0
assert_raise(ActiveRecord::RecordInvalid) do assert_raise(ActiveRecord::RecordInvalid) do
author.update_attributes!(:name => nil, :post_ids => []) author.update!(name: nil, post_ids: [])
end end
assert_equal posts_count, author.posts(true).size assert_equal posts_count, author.posts(true).size
end end

View file

@ -11,7 +11,7 @@ class Reference < ActiveRecord::Base
def make_comments def make_comments
if self.class.make_comments if self.class.make_comments
person.update_attributes :comments => "Reference destroyed" person.update comments: "Reference destroyed"
end end
end end
end end