Module: ActiveRemote::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/active_remote/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#deleteObject

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.



78
79
80
81
82
83
84
85
# File 'lib/active_remote/persistence.rb', line 78

def delete
  raise ReadOnlyRemoteRecord if readonly?
  response = remote_call(:delete, scope_key_hash)

  add_errors(response.errors) if response.respond_to?(:errors)

  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.

Raises:



93
94
95
96
# File 'lib/active_remote/persistence.rb', line 93

def delete!
  delete
  raise ActiveRemoteError.new(errors.to_s) if has_errors?
end

#destroyObject

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.



103
104
105
106
107
108
109
110
# File 'lib/active_remote/persistence.rb', line 103

def destroy
  raise ReadOnlyRemoteRecord if readonly?
  response = remote_call(:destroy, scope_key_hash)

  add_errors(response.errors) if response.respond_to?(:errors)

  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.

Raises:



117
118
119
120
# File 'lib/active_remote/persistence.rb', line 117

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.

Returns:

  • (Boolean)


124
125
126
# File 'lib/active_remote/persistence.rb', line 124

def has_errors?
  return respond_to?(:errors) && errors.present?
end

#instantiate(new_attributes) ⇒ Object

Instantiate a record with the given remote attributes. Generally used when retrieving records that already exist, so @new_record is set to false.



131
132
133
134
# File 'lib/active_remote/persistence.rb', line 131

def instantiate(new_attributes)
  new_attributes = self.class.build_from_rpc(new_attributes)
  init_with(new_attributes)
end

#new_record?Boolean

Returns true if the remote record hasn’t been saved yet; otherwise, returns false.

Returns:

  • (Boolean)


139
140
141
# File 'lib/active_remote/persistence.rb', line 139

def new_record?
  @new_record
end

#persisted?Boolean

Returns true if the remote record has been saved; otherwise, returns false.

Returns:

  • (Boolean)


145
146
147
# File 'lib/active_remote/persistence.rb', line 145

def persisted?
  return ! new_record?
end

#readonly!Object

Sets the instance to be a readonly object



151
152
153
# File 'lib/active_remote/persistence.rb', line 151

def readonly!
  @readonly = true
end

#readonly?Boolean

Returns true if the remote class or remote record is readonly; otherwise, returns false.

Returns:

  • (Boolean)


156
157
158
# File 'lib/active_remote/persistence.rb', line 156

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.



170
171
172
173
174
# File 'lib/active_remote/persistence.rb', line 170

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.



186
187
188
# File 'lib/active_remote/persistence.rb', line 186

def save!(*args)
  save(*args) || fail(RemoteRecordNotSaved, self)
end

#success?Boolean

Returns true if the record doesn’t have errors; otherwise, returns false.

Returns:

  • (Boolean)


192
193
194
# File 'lib/active_remote/persistence.rb', line 192

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.



205
206
207
208
209
210
# File 'lib/active_remote/persistence.rb', line 205

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.



216
217
218
219
# File 'lib/active_remote/persistence.rb', line 216

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.



226
227
228
229
# File 'lib/active_remote/persistence.rb', line 226

def update_attributes!(attributes)
  assign_attributes(attributes)
  save!
end