Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7308: activeresource_update_attributes_patch.diff

File activeresource_update_attributes_patch.diff, 4.5 kB (added by rwdaigle, 2 years ago)

Adds update_attribute(s) to ActiveRecord::Base

  • test/base_test.rb

    old new  
    220220    assert_raises(ActiveResource::ResourceConflict) { Person.find(2).save } 
    221221  end 
    222222 
     223  def test_update_attribute 
     224    matz = Person.find(:first) 
     225    assert matz.update_attribute("name", "David") 
     226    assert_equal "David", matz.name 
     227    assert_equal 1, matz.id   # make sure don't wipe out other attrs. 
     228     
     229    # Test exceptions get raised 
     230    ActiveResource::HttpMock.respond_to do |mock| 
     231      mock.put    "/people/1.xml",             {}, nil, 409 
     232    end 
     233    assert_raise(ActiveResource::ResourceConflict) { matz.update_attribute("name", "David") } 
     234  end 
     235 
     236  def test_update_attributes 
     237    matz = Person.find(:first) 
     238    matz.sex = 'male' 
     239    assert matz.update_attributes("name" => "David", "sex" => "female") 
     240    assert_equal "David", matz.name 
     241    assert_equal "female", matz.sex 
     242    assert_equal 1, matz.id   # make sure don't wipe out other attrs. 
     243     
     244    # Test exceptions get raised 
     245    ActiveResource::HttpMock.respond_to do |mock| 
     246      mock.put    "/people/1.xml",             {}, nil, 409 
     247    end 
     248    assert_raise(ActiveResource::ResourceConflict) { matz.update_attributes("name" => "David", "sex" => "") } 
     249  end 
     250 
    223251  def test_destroy 
    224252    assert Person.find(1).destroy 
    225253    ActiveResource::HttpMock.respond_to do |mock| 
  • lib/active_resource/base.rb

    old new  
    186186      new? ? create : update 
    187187    end 
    188188 
     189    # Updates a single attribute and requests that the resource be saved. 
     190    # 
     191    # Note: Unlike ActiveRecord::Base.update_attribute, this method <b>is</b> subject to normal validation 
     192    # routines as an update sends the whole body of the resource in the request.  (See Validations). 
     193    # As such, this method is equivalent to calling update_attributes with a single attribute/value pair. 
     194    # 
     195    # Note: Also unlike ActiveRecord::Base, ActiveResource currently uses string versions of attribute 
     196    # names, so use <tt>update_attribute("name", "ryan")</tt> <em>instead of</em> <tt>update_attribute(:name, "ryan")</tt>. 
     197    # 
     198    # If the saving fails because of a connection or remote service error, an exception will be raised.  If saving 
     199    # fails because the resource is invalid then <tt>false</tt> will be returned. 
     200    #     
     201    def update_attribute(name, value); update_attributes(name => value); end 
     202 
     203    # Updates this resource withe all the attributes from the passed-in Hash and requests that 
     204    # the record be saved. 
     205    # 
     206    # If the saving fails because of a connection or remote service error, an exception will be raised.  If saving 
     207    # fails because the resource is invalid then <tt>false</tt> will be returned. 
     208    # 
     209    # Note: Though this request can be made with a partial set of the resource's attributes, the full body 
     210    # of the request will still be sent in the save request to the remote service.  Also note that 
     211    # ActiveResource currently uses string versions of attribute 
     212    # names, so use <tt>update_attributes("name" => "ryan")</tt> <em>instead of</em> <tt>update_attribute(:name => "ryan")</tt>. 
     213    #     
     214    def update_attributes(attributes) 
     215      load(attributes) && save 
     216    end 
     217 
    189218    # Delete the resource. 
    190219    def destroy 
    191220      connection.delete(element_path) 
  • README

    old new  
    4343   ryan.first = 'Rizzle' 
    4444   ryan.save  #=> true 
    4545 
     46   # Or 
     47   ryan = Person.find(1) 
     48   ryan.update_attribute('first', 'Rizzle')  #=> true 
     49 
    4650   # And destruction 
    4751   # DELETE http://api.people.com:3000/people/1.xml 
    4852   # 
     
    146150  ryan.first = 'Rizzle' 
    147151  ryan.save  #=> true 
    148152 
     153<tt>update_attribute</tt> and <tt>update_attributes</tt> work very similarly to ActiveRecord except that both 
     154are subject to validation errors and the string attribute name is expected: 
     155 
     156  # <person><first>Ryan</first></person> 
     157  # 
     158  # is submitted as the body on 
     159  # 
     160  # PUT http://api.people.com:3000/people/1.xml 
     161  # 
     162  # when save is called on an existing Person object.  An empty response is 
     163  # is expected with code (204) 
     164  # 
     165  ryan = Person.find(1) 
     166  ryan.update_attribute('first', 'Ryan')  #=> true 
     167 
    149168==== Delete 
    150169 
    151170Destruction of a resource can be invoked as a class and instance method of the resource.