lib/active_remote/persistence.rb in active_remote-5.0.0.pre vs lib/active_remote/persistence.rb in active_remote-5.0.0.rc1

- old
+ new

@@ -74,10 +74,11 @@ # record was not deleted, it will have error messages indicating what went # wrong. Returns the frozen instance. # def delete raise ReadOnlyRemoteRecord if readonly? + response = remote_call(:delete, scope_key_hash) add_errors(response.errors) if response.respond_to?(:errors) success? ? freeze : false @@ -99,10 +100,11 @@ # be persisted). If the record was not deleted, it will have error # messages indicating what went wrong. Returns the frozen instance. # def destroy raise ReadOnlyRemoteRecord if readonly? + response = remote_call(:destroy, scope_key_hash) add_errors(response.errors) if response.respond_to?(:errors) success? ? freeze : false @@ -154,10 +156,23 @@ # Returns true if the remote class or remote record is readonly; otherwise, returns false. def readonly? self.class.readonly? || @readonly end + # Executes a remote call on the current object and serializes it's attributes and + # errors from the response. + # + # Defaults request args to the scope key hash (e.g., { guid: 'ABC-123' }) when none are given. + # Returns false if the response contained errors; otherwise, returns true. + # + def remote(endpoint, request_args = scope_key_hash) + response = remote_call(endpoint, request_args) + assign_attributes_from_rpc(response) + + success? + end + # Saves the remote record. # # If it is a new record, it will be created through the service, otherwise # the existing record gets updated. # @@ -201,10 +216,11 @@ # # This method raises an ActiveRemote::ReadOnlyRemoteRecord if the # attribute is marked as readonly. def update_attribute(name, value) raise ReadOnlyRemoteRecord if readonly? + name = name.to_s send("#{name}=", value) save(:validate => false) end @@ -233,18 +249,12 @@ # Handles creating a remote object and serializing it's attributes and # errors from the response. # def remote_create run_callbacks :create do - # Use the getter here so we get the type casting. - new_attributes = attributes + remote(:create, attributes) - response = remote_call(:create, new_attributes) - - instantiate(response.to_hash) - add_errors(response.errors) if response.respond_to?(:errors) - @new_record = has_errors? success? end end @@ -252,10 +262,11 @@ # are created, existing records are updated. If the record is marked as # readonly, an ActiveRemote::ReadOnlyRemoteRecord is raised. # def create_or_update(*args) raise ReadOnlyRemoteRecord if readonly? + new_record? ? remote_create : remote_update(*args) end # Handles updating a remote object and serializing it's attributes and # errors from the response. Only attributes with the given attribute names @@ -266,15 +277,10 @@ # Use the getter here so we get the type casting. updated_attributes = attributes updated_attributes.slice!(*attribute_names) updated_attributes.merge!(scope_key_hash) - response = remote_call(:update, updated_attributes) - - instantiate(response.to_hash) - add_errors(response.errors) if response.respond_to?(:errors) - - success? + remote(:update, updated_attributes) end end end end