Module: ActiveRemote::Persistence
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#delete ⇒ Object
Deletes the record from the service (the service determines if the record is hard or soft deleted) and freezes this instance to indicate that no changes should be made (since they can’t be persisted).
-
#delete! ⇒ Object
Deletes the record from the service (the service determines if the record is hard or soft deleted) and freezes this instance to indicate that no changes should be made (since they can’t be persisted).
-
#destroy ⇒ Object
Destroys (hard deletes) the record from the service and freezes this instance to indicate that no changes should be made (since they can’t be persisted).
-
#destroy! ⇒ Object
Destroys (hard deletes) the record from the service and freezes this instance to indicate that no changes should be made (since they can’t be persisted).
-
#has_errors? ⇒ Boolean
Returns true if the record has errors; otherwise, returns false.
-
#instantiate(record) ⇒ Object
Instantiate a record with the given remote attributes.
-
#new_record? ⇒ Boolean
Returns true if the remote record hasn’t been saved yet; otherwise, returns false.
-
#persisted? ⇒ Boolean
Returns true if the remote record has been saved; otherwise, returns false.
-
#readonly! ⇒ Object
Sets the instance to be a readonly object.
-
#readonly? ⇒ Boolean
Returns true if the remote class or remote record is readonly; otherwise, returns false.
-
#save(*args) ⇒ Object
Saves the remote record.
-
#save!(*args) ⇒ Object
Saves the remote record.
-
#success? ⇒ Boolean
Returns true if the record doesn’t have errors; otherwise, returns false.
-
#update_attribute(name, value) ⇒ Object
Updates a single attribute and saves the record.
-
#update_attributes(attributes) ⇒ Object
(also: #update)
Updates the attributes of the remote record from the passed-in hash and saves the remote record.
-
#update_attributes!(attributes) ⇒ Object
(also: #update!)
Updates the attributes of the remote record from the passed-in hash and saves the remote record.
Instance Method Details
#delete ⇒ Object
Deletes the record from the service (the service determines if the record is hard or soft deleted) and freezes this instance to indicate that no changes should be made (since they can’t be persisted). If the record was not deleted, it will have error messages indicating what went wrong. Returns the frozen instance.
77 78 79 80 81 82 83 84 |
# File 'lib/active_remote/persistence.rb', line 77 def delete raise ReadOnlyRemoteRecord if readonly? response = rpc.execute(:delete, scope_key_hash) add_errors_from_response(response) return success? ? freeze : false end |
#delete! ⇒ Object
Deletes the record from the service (the service determines if the record is hard or soft deleted) and freezes this instance to indicate that no changes should be made (since they can’t be persisted). If the record was not deleted, an exception will be raised. Returns the frozen instance.
92 93 94 95 |
# File 'lib/active_remote/persistence.rb', line 92 def delete! delete raise ActiveRemoteError.new(errors.to_s) if has_errors? end |
#destroy ⇒ Object
Destroys (hard deletes) the record from the service and freezes this instance to indicate that no changes should be made (since they can’t be persisted). If the record was not deleted, it will have error messages indicating what went wrong. Returns the frozen instance.
102 103 104 105 106 107 108 109 |
# File 'lib/active_remote/persistence.rb', line 102 def destroy raise ReadOnlyRemoteRecord if readonly? response = rpc.execute(:destroy, scope_key_hash) add_errors_from_response(response) return success? ? freeze : false end |
#destroy! ⇒ Object
Destroys (hard deletes) the record from the service and freezes this instance to indicate that no changes should be made (since they can’t be persisted). If the record was not deleted, an exception will be raised. Returns the frozen instance.
116 117 118 119 |
# File 'lib/active_remote/persistence.rb', line 116 def destroy! destroy raise ActiveRemoteError.new(errors.to_s) if has_errors? end |
#has_errors? ⇒ Boolean
Returns true if the record has errors; otherwise, returns false.
123 124 125 |
# File 'lib/active_remote/persistence.rb', line 123 def has_errors? return respond_to?(:errors) && errors.present? end |
#instantiate(record) ⇒ Object
Instantiate a record with the given remote attributes. Generally used when retrieving records that already exist, so @new_record is set to false.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/active_remote/persistence.rb', line 130 def instantiate(record) skip_dirty_tracking do assign_attributes(record, :without_protection => true) end # TODO: figure out how to safely run after_find/search callbacks here # currently, several functions use this code path, so an alternate path # may need to be added run_callbacks :initialize @new_record = false self end |
#new_record? ⇒ Boolean
Returns true if the remote record hasn’t been saved yet; otherwise, returns false.
148 149 150 |
# File 'lib/active_remote/persistence.rb', line 148 def new_record? @new_record end |
#persisted? ⇒ Boolean
Returns true if the remote record has been saved; otherwise, returns false.
154 155 156 |
# File 'lib/active_remote/persistence.rb', line 154 def persisted? return ! new_record? end |
#readonly! ⇒ Object
Sets the instance to be a readonly object
160 161 162 |
# File 'lib/active_remote/persistence.rb', line 160 def readonly! @readonly = true end |
#readonly? ⇒ Boolean
Returns true if the remote class or remote record is readonly; otherwise, returns false.
165 166 167 |
# File 'lib/active_remote/persistence.rb', line 165 def readonly? self.class.readonly? || @readonly end |
#save(*args) ⇒ Object
Saves the remote record.
If it is a new record, it will be created through the service, otherwise the existing record gets updated.
The service will run any validations and if any of them fail, will return the record with error messages indicating what went wrong.
Also runs any before/after save callbacks that are defined.
179 180 181 182 183 |
# File 'lib/active_remote/persistence.rb', line 179 def save(*args) run_callbacks :save do create_or_update(*args) end end |
#save!(*args) ⇒ Object
Saves the remote record.
If it is a new record, it will be created through the service, otherwise the existing record gets updated.
The service will run any validations. If any of them fail (e.g. error messages are returned), an ActiveRemote::RemoteRecordNotSaved is raised.
Also runs any before/after save callbacks that are defined.
195 196 197 |
# File 'lib/active_remote/persistence.rb', line 195 def save!(*args) save(*args) || raise(RemoteRecordNotSaved) end |
#success? ⇒ Boolean
Returns true if the record doesn’t have errors; otherwise, returns false.
201 202 203 |
# File 'lib/active_remote/persistence.rb', line 201 def success? return ! has_errors? end |
#update_attribute(name, value) ⇒ Object
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.
214 215 216 217 218 219 |
# File 'lib/active_remote/persistence.rb', line 214 def update_attribute(name, value) raise ReadOnlyRemoteRecord if readonly? name = name.to_s send("#{name}=", value) save(:validate => false) end |
#update_attributes(attributes) ⇒ Object Also known as: update
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.
225 226 227 228 |
# File 'lib/active_remote/persistence.rb', line 225 def update_attributes(attributes) assign_attributes(attributes) save end |
#update_attributes!(attributes) ⇒ Object Also known as: update!
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.
235 236 237 238 |
# File 'lib/active_remote/persistence.rb', line 235 def update_attributes!(attributes) assign_attributes(attributes) save! end |