lib/active_remote/persistence.rb in active_remote-2.2.0 vs lib/active_remote/persistence.rb in active_remote-2.3.0
- old
+ new
@@ -200,34 +200,52 @@
#
def success?
return ! has_errors?
end
+ # Updates a single attribute and saves the record.
+ # This is especially useful for boolean flags on existing records. Also note that
+ #
+ # * Validation is skipped.
+ # * Callbacks are invoked.
+ # * Updates all the attributes that are dirty in this object.
+ #
+ # 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
+
# Updates the attributes of the remote record from the passed-in hash and
# saves the remote record. If the object is invalid, it will have error
# messages and false will be returned.
#
def update_attributes(attributes)
assign_attributes(attributes)
save
end
+ alias_method :update, :update_attributes
# Updates the attributes of the remote record from the passed-in hash and
# saves the remote record. If the object is invalid, an
# ActiveRemote::RemoteRecordNotSaved is raised.
#
def update_attributes!(attributes)
assign_attributes(attributes)
save!
end
+ alias_method :update!, :update_attributes!
private
# Handles creating a remote object and serializing it's attributes and
# errors from the response.
#
- def create
+ def remote_create
run_callbacks :create do
# Use the getter here so we get the type casting.
new_attributes = attributes
new_attributes.delete(primary_key.to_s)
@@ -245,17 +263,17 @@
# 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? ? create : update(*args)
+ 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
# (plus :guid) will be updated. Defaults to all attributes.
#
- def update(attribute_names = @attributes.keys)
+ def remote_update(attribute_names = @attributes.keys)
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!(scope_key_hash)