diff options
author | Taryn East <git@taryneast.org> | 2009-10-02 10:13:01 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-10-02 10:13:01 -0500 |
commit | f4f68885efd0e1135217433cafd368902b1fd58a (patch) | |
tree | cceb86b215f9df4dd5cc8ae5e75dd0d395596294 | |
parent | 420004e030e96f2ace6e27fd622c90ee9e986677 (diff) | |
download | rails-f4f68885efd0e1135217433cafd368902b1fd58a.tar.gz rails-f4f68885efd0e1135217433cafd368902b1fd58a.tar.bz2 rails-f4f68885efd0e1135217433cafd368902b1fd58a.zip |
update_attribute(s) added to Active Resource
Signed-off-by: Joshua Peek <josh@joshpeek.com>
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 30 | ||||
-rw-r--r-- | activeresource/test/cases/base_test.rb | 53 |
2 files changed, 81 insertions, 2 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index e5b8589fb3..8fc0999c70 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -1099,6 +1099,36 @@ module ActiveResource self end + # Updates a single attribute and then saves the object. + # + # Note: Unlike ActiveRecord::Base.update_attribute, this method <b>is</b> + # subject to normal validation routines as an update sends the whole body + # of the resource in the request. (See Validations). + # + # As such, this method is equivalent to calling update_attributes with a single attribute/value pair. + # + # If the saving fails because of a connection or remote service error, an + # exception will be raised. If saving fails because the resource is + # invalid then <tt>false</tt> will be returned. + def update_attribute(name, value) + self.send("#{name}=".to_sym, value) + self.save + end + + + # Updates this resource with all the attributes from the passed-in Hash + # and requests that the record be saved. + # + # If the saving fails because of a connection or remote service error, an + # exception will be raised. If saving fails because the resource is + # invalid then <tt>false</tt> will be returned. + # + # Note: Though this request can be made with a partial set of the + # resource's attributes, the full body of the request will still be sent + # in the save request to the remote service. + def update_attributes(attributes) + load(attributes) && save + end # For checking <tt>respond_to?</tt> without searching the attributes (which is faster). alias_method :respond_to_without_attributes?, :respond_to? diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb index 8c0217aad6..96fc6fc2fe 100644 --- a/activeresource/test/cases/base_test.rb +++ b/activeresource/test/cases/base_test.rb @@ -11,12 +11,12 @@ class BaseTest < Test::Unit::TestCase @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person') @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person') @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person') - @addy = { :id => 1, :street => '12345 Street' }.to_xml(:root => 'address') + @addy = { :id => 1, :street => '12345 Street', :country => 'Australia' }.to_xml(:root => 'address') @default_request_headers = { 'Content-Type' => 'application/xml' } @rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person") @people = [{ :id => 1, :name => 'Matz' }, { :id => 2, :name => 'David' }].to_xml(:root => 'people') @people_david = [{ :id => 2, :name => 'David' }].to_xml(:root => 'people') - @addresses = [{ :id => 1, :street => '12345 Street' }].to_xml(:root => 'addresses') + @addresses = [{ :id => 1, :street => '12345 Street', :country => 'Australia' }].to_xml(:root => 'addresses') # - deep nested resource - # - Luis (Customer) @@ -813,6 +813,55 @@ class BaseTest < Test::Unit::TestCase assert_raise(ActiveResource::ResourceConflict) { Person.find(2).save } end + + ###### + # update_attribute(s)(!) + + def test_update_attribute_as_symbol + matz = Person.first + matz.expects(:save).returns(true) + + assert_equal "Matz", matz.name + assert matz.update_attribute(:name, "David") + assert_equal "David", matz.name + end + + def test_update_attribute_as_string + matz = Person.first + matz.expects(:save).returns(true) + + assert_equal "Matz", matz.name + assert matz.update_attribute('name', "David") + assert_equal "David", matz.name + end + + + def test_update_attributes_as_symbols + addy = StreetAddress.first(:params => {:person_id => 1}) + addy.expects(:save).returns(true) + + assert_equal "12345 Street", addy.street + assert_equal "Australia", addy.country + assert addy.update_attributes(:street => '54321 Street', :country => 'USA') + assert_equal "54321 Street", addy.street + assert_equal "USA", addy.country + end + + def test_update_attributes_as_strings + addy = StreetAddress.first(:params => {:person_id => 1}) + addy.expects(:save).returns(true) + + assert_equal "12345 Street", addy.street + assert_equal "Australia", addy.country + assert addy.update_attributes('street' => '54321 Street', 'country' => 'USA') + assert_equal "54321 Street", addy.street + assert_equal "USA", addy.country + end + + + ##### + # Mayhem and destruction + def test_destroy assert Person.find(1).destroy ActiveResource::HttpMock.respond_to do |mock| |