lib/active_remote/persistence.rb in active_remote-1.6.1 vs lib/active_remote/persistence.rb in active_remote-1.7.0

- old
+ new

@@ -8,11 +8,11 @@ include ::ActiveRemote::Persistence::InstanceMethods include ::ActiveRemote::RPC # Allow users to create callbacks around a `save` call. # - define_model_callbacks :save + define_model_callbacks :save, :create, :update # Before a save occurs, ensure that we # clear out the errors list. # set_callback :save, :before do |remote| @@ -73,11 +73,11 @@ # record was not deleted, it will have error messages indicating what went # wrong. Returns the frozen instance. # def delete raise ReadOnlyRemoteRecord if readonly? - execute(:delete, @attributes.slice("guid")) + execute(:delete, "guid" => read_attribute("guid")) return success? ? freeze : false end # Deletes the record from the service (the service determines if the @@ -96,11 +96,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? - execute(:destroy, @attributes.slice("guid")) + execute(:destroy, "guid" => read_attribute("guid")) return success? ? freeze : false end # Destroys (hard deletes) the record from the service and freezes this @@ -195,20 +195,22 @@ # Handles creating a remote object and serializing it's attributes and # errors from the response. # def create - # Use the getter here so we get the type casting. - new_attributes = attributes - new_attributes.delete("guid") + run_callbacks :create do + # Use the getter here so we get the type casting. + new_attributes = attributes + new_attributes.delete("guid") - execute(:create, new_attributes) + execute(:create, new_attributes) - assign_attributes(last_response.to_hash) - add_errors_from_response + assign_attributes(last_response.to_hash) + add_errors_from_response - success? + success? + end end # Deterines whether the record should be created or updated. New records # are created, existing records are updated. If the record is marked as # readonly, an ActiveRemote::ReadOnlyRemoteRecord is raised. @@ -221,20 +223,22 @@ # Handles updating a remote object and serializing it's attributes and # errors from the response. Only attributes with the given attribute names # (plus :guid) will be updated. Defaults to all attributes. # def update(attribute_names = @attributes.keys) - # Use the getter here so we get the type casting. - updated_attributes = attributes - updated_attributes.slice!(*attribute_names) - updated_attributes.merge!("guid" => self[:guid]) + run_callbacks :update do + # Use the getter here so we get the type casting. + updated_attributes = attributes + updated_attributes.slice!(*attribute_names) + updated_attributes.merge!("guid" => self[:guid]) - execute(:update, updated_attributes) + execute(:update, updated_attributes) - assign_attributes(last_response.to_hash) - add_errors_from_response + assign_attributes(last_response.to_hash) + add_errors_from_response - success? + success? + end end end end end