Ticket #7308: reworked_activeresource_update_attributes_patch.diff
File reworked_activeresource_update_attributes_patch.diff, 4.5 kB (added by EmmanuelOga, 7 months ago) |
---|
-
test/base_test.rb
old new 383 383 assert_raises(ActiveResource::ResourceConflict) { Person.find(2).save } 384 384 end 385 385 386 def test_update_attribute 387 matz = Person.find(:first) 388 assert matz.update_attribute("name", "David") 389 assert_equal "David", matz.name 390 assert_equal 1, matz.id # make sure don't wipe out other attrs. 391 392 # Test exceptions get raised 393 ActiveResource::HttpMock.respond_to do |mock| 394 mock.put "/people/1.xml", {}, nil, 409 395 end 396 assert_raise(ActiveResource::ResourceConflict) { matz.update_attribute("name", "David") } 397 end 398 399 def test_update_attributes 400 matz = Person.find(:first) 401 matz.sex = 'male' 402 assert matz.update_attributes("name" => "David", "sex" => "female") 403 assert_equal "David", matz.name 404 assert_equal "female", matz.sex 405 assert_equal 1, matz.id # make sure don't wipe out other attrs. 406 407 # Test exceptions get raised 408 ActiveResource::HttpMock.respond_to do |mock| 409 mock.put "/people/1.xml", {}, nil, 409 410 end 411 assert_raise(ActiveResource::ResourceConflict) { matz.update_attributes("name" => "David", "sex" => "") } 412 end 413 386 414 def test_destroy 387 415 assert Person.find(1).destroy 388 416 ActiveResource::HttpMock.respond_to do |mock| -
lib/active_resource/base.rb
old new 668 668 def destroy 669 669 connection.delete(element_path, self.class.headers) 670 670 end 671 672 # Updates a single attribute and requests that the resource be saved. 673 # 674 # Note: Unlike ActiveRecord::Base.update_attribute, this method <b>is</b> subject to normal validation 675 # routines as an update sends the whole body of the resource in the request. (See Validations). 676 # As such, this method is equivalent to calling update_attributes with a single attribute/value pair. 677 # 678 # Note: Also unlike ActiveRecord::Base, ActiveResource currently uses string versions of attribute 679 # names, so use <tt>update_attribute("name", "ryan")</tt> <em>instead of</em> <tt>update_attribute(:name, "ryan")</tt>. 680 # 681 # If the saving fails because of a connection or remote service error, an exception will be raised. If saving 682 # fails because the resource is invalid then <tt>false</tt> will be returned. 683 # 684 def update_attribute(name, value); update_attributes(name => value); end 685 686 # Updates this resource withe all the attributes from the passed-in Hash and requests that 687 # the record be saved. 688 # 689 # If the saving fails because of a connection or remote service error, an exception will be raised. If saving 690 # fails because the resource is invalid then <tt>false</tt> will be returned. 691 # 692 # Note: Though this request can be made with a partial set of the resource's attributes, the full body 693 # of the request will still be sent in the save request to the remote service. Also note that 694 # ActiveResource currently uses string versions of attribute 695 # names, so use <tt>update_attributes("name" => "ryan")</tt> <em>instead of</em> <tt>update_attribute(:name => "ryan")</tt>. 696 # 697 def update_attributes(attributes) 698 load(attributes) && save 699 end 671 700 672 701 # Evaluates to <tt>true</tt> if this resource is not +new?+ and is 673 702 # found on the remote service. Using this method, you can check for -
README
old new 143 143 ryan.first = 'Rizzle' 144 144 ryan.save #=> true 145 145 146 <tt>update_attribute</tt> and <tt>update_attributes</tt> work very similarly to ActiveRecord except that both 147 are subject to validation errors and the string attribute name is expected: 148 149 # <person><first>Ryan</first></person> 150 # 151 # is submitted as the body on 152 # 153 # PUT http://api.people.com:3000/people/1.xml 154 # 155 # when save is called on an existing Person object. An empty response is 156 # is expected with code (204) 157 # 158 ryan = Person.find(1) 159 ryan.update_attribute('first', 'Ryan') #=> true 160 161 # Or 162 ryan = Person.find(1) 163 ryan.update_attribute('first', 'Rizzle') #=> true 164 146 165 ==== Delete 147 166 148 167 Destruction of a resource can be invoked as a class and instance method of the resource.