diff options
Diffstat (limited to 'activeresource/lib')
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 42 | ||||
-rw-r--r-- | activeresource/lib/active_resource/observing.rb | 13 | ||||
-rw-r--r-- | activeresource/lib/active_resource/validations.rb | 4 | ||||
-rw-r--r-- | activeresource/lib/activeresource.rb | 1 |
4 files changed, 50 insertions, 10 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index e5b8589fb3..b21d8db613 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' @@ -155,7 +156,7 @@ module ActiveResource # # <tt>404</tt> 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 @@ -414,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., <tt>prefix/collectionname/1.xml</tt>) # This method is regenerated at runtime based on what the \prefix is set to. def prefix(options={}) @@ -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 @@ -915,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 @@ -924,7 +925,7 @@ module ActiveResource # With <tt>save!</tt> 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 <tt>save!</tt>. If any # of the <tt>before_*</tt> callbacks return +false+ the action is @@ -1099,6 +1100,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? @@ -1119,7 +1150,6 @@ module ActiveResource super end - protected def connection(refresh = false) self.class.connection(refresh) diff --git a/activeresource/lib/active_resource/observing.rb b/activeresource/lib/active_resource/observing.rb index 94836f4bb1..3c74d49c80 100644 --- a/activeresource/lib/active_resource/observing.rb +++ b/activeresource/lib/active_resource/observing.rb @@ -4,7 +4,18 @@ module ActiveResource include ActiveModel::Observing included do - wrap_with_notifications :create, :save, :update, :destroy + %w( create save update destroy ).each do |method| + class_eval(<<-EOS, __FILE__, __LINE__ + 1) + def #{method}_with_notifications(*args, &block) + notify_observers(:before_#{method}) + if result = #{method}_without_notifications(*args, &block) + notify_observers(:after_#{method}) + end + result + end + EOS + alias_method_chain(method, :notifications) + end end end end diff --git a/activeresource/lib/active_resource/validations.rb b/activeresource/lib/active_resource/validations.rb index d4d282e273..67b69fa505 100644 --- a/activeresource/lib/active_resource/validations.rb +++ b/activeresource/lib/active_resource/validations.rb @@ -93,9 +93,9 @@ module ActiveResource # content-type of the error-block received def load_remote_errors(remote_errors, save_cache = false ) #:nodoc: case remote_errors.response['Content-Type'] - when 'application/xml' + when /xml/ errors.from_xml(remote_errors.response.body, save_cache) - when 'application/json' + when /json/ errors.from_json(remote_errors.response.body, save_cache) end end diff --git a/activeresource/lib/activeresource.rb b/activeresource/lib/activeresource.rb deleted file mode 100644 index e076455b16..0000000000 --- a/activeresource/lib/activeresource.rb +++ /dev/null @@ -1 +0,0 @@ -require 'active_resource' |