From f4f68885efd0e1135217433cafd368902b1fd58a Mon Sep 17 00:00:00 2001 From: Taryn East Date: Fri, 2 Oct 2009 10:13:01 -0500 Subject: update_attribute(s) added to Active Resource Signed-off-by: Joshua Peek --- activeresource/lib/active_resource/base.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'activeresource/lib') 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 is + # 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 false 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 false 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 respond_to? without searching the attributes (which is faster). alias_method :respond_to_without_attributes?, :respond_to? -- cgit v1.2.3 From 8377646d68b32de362fefad0d752a923f6b36da6 Mon Sep 17 00:00:00 2001 From: Taryn East Date: Fri, 2 Oct 2009 10:13:40 -0500 Subject: add indifferent access to the attributes Signed-off-by: Joshua Peek --- activeresource/lib/active_resource/base.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activeresource/lib') diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 8fc0999c70..3a07811f26 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -1,6 +1,7 @@ require 'active_support' require 'active_support/core_ext/class/attribute_accessors' require 'active_support/core_ext/class/inheritable_attributes' +require 'active_support/core_ext/hash/indifferent_access' require 'active_support/core_ext/kernel/reporting' require 'active_support/core_ext/module/attr_accessor_with_default' require 'active_support/core_ext/module/delegation' @@ -770,7 +771,7 @@ module ActiveResource # my_other_course = Course.new(:name => "Philosophy: Reason and Being", :lecturer => "Ralph Cling") # my_other_course.save def initialize(attributes = {}) - @attributes = {} + @attributes = {}.with_indifferent_access @prefix_options = {} load(attributes) end -- cgit v1.2.3 From 89630a7c2c3d0f625742e2b84ec28479ff3ecef9 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Fri, 2 Oct 2009 10:19:30 -0500 Subject: Cleanup whitespace introduced in 8377646 and f4f6888 --- activeresource/lib/active_resource/base.rb | 69 +++++++++++++++--------------- 1 file changed, 34 insertions(+), 35 deletions(-) (limited to 'activeresource/lib') diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 3a07811f26..b21d8db613 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -156,7 +156,7 @@ module ActiveResource # # 404 is just one of the HTTP error response codes that Active Resource will handle with its own exception. The # following HTTP response codes will also result in these exceptions: - # + # # * 200..399 - Valid response, no exception (other than 301, 302) # * 301, 302 - ActiveResource::Redirection # * 400 - ActiveResource::BadRequest @@ -415,7 +415,7 @@ module ActiveResource attr_accessor_with_default(:collection_name) { ActiveSupport::Inflector.pluralize(element_name) } #:nodoc: attr_accessor_with_default(:primary_key, 'id') #:nodoc: - + # Gets the \prefix for a resource's nested URL (e.g., prefix/collectionname/1.xml) # This method is regenerated at runtime based on what the \prefix is set to. def prefix(options={}) @@ -916,7 +916,7 @@ module ActiveResource def save new? ? create : update end - + # Saves the resource. # # If the resource is new, it is created via +POST+, otherwise the @@ -925,7 +925,7 @@ module ActiveResource # With save! validations always run. If any of them fail # ActiveResource::ResourceInvalid gets raised, and nothing is POSTed to # the remote system. - # See ActiveResource::Validations for more information. + # See ActiveResource::Validations for more information. # # There's a series of callbacks associated with save!. If any # of the before_* callbacks return +false+ the action is @@ -1100,36 +1100,36 @@ module ActiveResource self end - # Updates a single attribute and then saves the object. - # - # Note: Unlike ActiveRecord::Base.update_attribute, this method is - # 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 false 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 false 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 + # Updates a single attribute and then saves the object. + # + # Note: Unlike ActiveRecord::Base.update_attribute, this method is + # 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 false 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 false 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 respond_to? without searching the attributes (which is faster). alias_method :respond_to_without_attributes?, :respond_to? @@ -1150,7 +1150,6 @@ module ActiveResource super end - protected def connection(refresh = false) self.class.connection(refresh) -- cgit v1.2.3 From 32cea98c3b5bd077692f5b20756e0e3c75e0c524 Mon Sep 17 00:00:00 2001 From: Chad Woolley Date: Sun, 4 Oct 2009 01:50:47 -0700 Subject: Ruby 1.9: Fix ActiveResource::ConnectionError#to_s when @response does not respond to #code or #message Signed-off-by: Carl Lerche --- activeresource/lib/active_resource/exceptions.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'activeresource/lib') diff --git a/activeresource/lib/active_resource/exceptions.rb b/activeresource/lib/active_resource/exceptions.rb index 0631cdcf9f..0f4549fd73 100644 --- a/activeresource/lib/active_resource/exceptions.rb +++ b/activeresource/lib/active_resource/exceptions.rb @@ -8,7 +8,10 @@ module ActiveResource end def to_s - "Failed with #{response.code} #{response.message if response.respond_to?(:message)}" + message = "Failed." + message << " Response code = #{response.code}." if response.respond_to?(:code) + message << " Response message = #{response.message}." if response.respond_to?(:message) + message end end -- cgit v1.2.3