module MongoModel::DocumentExtensions::Persistence

Public Instance Methods

collection() click to toggle source
# File lib/mongomodel/document/persistence.rb, line 73
def collection
  self.class.collection
end
database() click to toggle source
# File lib/mongomodel/document/persistence.rb, line 77
def database
  self.class.database
end
delete() click to toggle source
# File lib/mongomodel/document/persistence.rb, line 36
def delete
  self.class.unscoped.delete(id)
  set_destroyed(true)
  freeze
end
destroy() click to toggle source

Remove the document from the database.

# File lib/mongomodel/document/persistence.rb, line 43
def destroy
  delete
end
generate_id() click to toggle source

Generate a new BSON::ObjectId for the record. Override in subclasses for custom ID generation.

# File lib/mongomodel/document/persistence.rb, line 83
def generate_id
  ::BSON::ObjectId.new.to_s
end
reload() click to toggle source

Reload the document from the database. If the document hasn't been saved, this method will raise an error.

# File lib/mongomodel/document/persistence.rb, line 13
def reload
  reloaded = self.class.unscoped.find(id)

  attributes.clear
  attributes.load!(reloaded.attributes.to_mongo)

  associations.values.each do |association|
    association.proxy.reset
  end

  self
end
save(*) click to toggle source

Save the document to the database. Returns true on success.

# File lib/mongomodel/document/persistence.rb, line 27
def save(*)
  create_or_update
end
save!(*) click to toggle source

Save the document to the database. Raises a DocumentNotSaved exception if it fails.

# File lib/mongomodel/document/persistence.rb, line 32
def save!(*)
  create_or_update || raise(DocumentNotSaved)
end
update_attribute(name, value) click to toggle source

Updates a single attribute and saves the document without going through the normal validation procedure. This is especially useful for boolean flags on existing documents.

# File lib/mongomodel/document/persistence.rb, line 68
def update_attribute(name, value)
  send("#{name}=", value)
  save(:validate => false)
end
update_attributes(attributes, options={}) click to toggle source

Updates all the attributes from the passed-in Hash and saves the document. If the object is invalid, the saving will fail and false will be returned.

When updating model attributes, mass-assignment security protection is respected. If no :as option is supplied then the :default role will be used. If you want to bypass the protection given by attr_protected and attr_accessible then you can do so using the :without_protection option.

# File lib/mongomodel/document/persistence.rb, line 54
def update_attributes(attributes, options={})
  self.assign_attributes(attributes, options)
  save
end
update_attributes!(attributes, options={}) click to toggle source

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

# File lib/mongomodel/document/persistence.rb, line 61
def update_attributes!(attributes, options={})
  self.assign_attributes(attributes, options)
  save!
end

Private Instance Methods

create() click to toggle source
# File lib/mongomodel/document/persistence.rb, line 153
def create
  save_to_collection
end
create_or_update() click to toggle source
# File lib/mongomodel/document/persistence.rb, line 148
def create_or_update
  result = new_record? ? create : update
  result != false
end
instantiate() click to toggle source
# File lib/mongomodel/document/persistence.rb, line 169
def instantiate
  set_new_record(false)
end
save_to_collection() click to toggle source
# File lib/mongomodel/document/persistence.rb, line 161
def save_to_collection
  collection.save(to_mongo, :w => self.class.save_safely? ? 1 : 0)
  set_new_record(false)
  true
rescue Mongo::OperationFailure => e
  false
end
update() click to toggle source
# File lib/mongomodel/document/persistence.rb, line 157
def update
  save_to_collection
end