Module: DatastaxRails::Persistence

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

Overview

Handles persisting data into Datastax

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#destroy(options = {}) ⇒ Object Also known as: destroy_without_callbacks



189
190
191
192
193
# File 'lib/datastax_rails/persistence.rb', line 189

def destroy(options = {})
  self.class.remove(id_for_destroy, options)
  @destroyed = true
  freeze
end

#persisted?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/datastax_rails/persistence.rb', line 174

def persisted?
  !(new_record? || destroyed?)
end

#reload(options = nil) ⇒ Object

Reloads the attributes of this object from the database. The optional options argument is passed to find when reloading so you may do e.g. record.reload(:lock => true) to reload the same record with an exclusive row lock.



268
269
270
271
272
273
274
275
276
# File 'lib/datastax_rails/persistence.rb', line 268

def reload(options = nil)
  clear_association_cache

  fresh_object = self.class.unscoped { self.class.find(id, options) }
  @attributes.update(fresh_object.instance_variable_get('@attributes'))

  @attributes_cache = {}
  self
end

#save(options = {}) ⇒ Object

See write for allowed options



179
180
181
182
183
# File 'lib/datastax_rails/persistence.rb', line 179

def save(options = {})
  _create_or_update(options)
rescue DatastaxRails::RecordInvalid
  false
end

#save!(options = {}) ⇒ Object



185
186
187
# File 'lib/datastax_rails/persistence.rb', line 185

def save!(options = {})
  _create_or_update(options) || fail(RecordNotSaved)
end

#toggle(attribute) ⇒ Object

Assigns to attribute the boolean opposite of attribute?. So if the predicate returns true the attribute will become false. This method toggles directly the underlying value without calling any setter. Returns self.



251
252
253
254
# File 'lib/datastax_rails/persistence.rb', line 251

def toggle(attribute)
  self[attribute] = !send("#{attribute}?")
  self
end

#toggle!(attribute) ⇒ Object

Wrapper around toggle that saves the record. This method differs from its non-bang version in that it passes through the attribute setter. Saving is not subjected to validation checks. Returns true if the record could be saved.



260
261
262
# File 'lib/datastax_rails/persistence.rb', line 260

def toggle!(attribute)
  toggle(attribute).update_attribute(attribute, self[attribute])
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.

  • updated_at/updated_on column is updated if that column is available.

  • Updates all the attributes that are dirty in this object.



228
229
230
231
# File 'lib/datastax_rails/persistence.rb', line 228

def update_attribute(name, value)
  send("#{name}=", value)
  save(validate: false)
end

#update_attributes(attributes, _options = {}) ⇒ Object

Updates the attributes of the model from the passed-in hash and saves the record. If the object is invalid, the saving will fail and false will be returned.



235
236
237
238
# File 'lib/datastax_rails/persistence.rb', line 235

def update_attributes(attributes, _options = {})
  assign_attributes(attributes)
  save
end

#update_attributes!(attributes, _options = {}) ⇒ Object

Updates its receiver just like update_attributes but calls save! instead of save, so an exception is raised if the record is invalid.



242
243
244
245
# File 'lib/datastax_rails/persistence.rb', line 242

def update_attributes!(attributes, _options = {})
  assign_attributes(attributes)
  save!
end