Module: MongoModel::DocumentExtensions::Persistence

Extended by:
ActiveSupport::Concern
Included in:
MongoModel::Document
Defined in:
lib/mongomodel/document/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#collectionObject


73
74
75
# File 'lib/mongomodel/document/persistence.rb', line 73

def collection
  self.class.collection
end

#databaseObject


77
78
79
# File 'lib/mongomodel/document/persistence.rb', line 77

def database
  self.class.database
end

#deleteObject


36
37
38
39
40
# File 'lib/mongomodel/document/persistence.rb', line 36

def delete
  self.class.unscoped.delete(id)
  set_destroyed(true)
  freeze
end

#destroyObject

Remove the document from the database.


43
44
45
# File 'lib/mongomodel/document/persistence.rb', line 43

def destroy
  delete
end

#generate_idObject

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


83
84
85
# File 'lib/mongomodel/document/persistence.rb', line 83

def generate_id
  ::BSON::ObjectId.new.to_s
end

#reloadObject

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


13
14
15
16
17
18
19
20
21
22
23
24
# 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

#saveObject

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


27
28
29
# File 'lib/mongomodel/document/persistence.rb', line 27

def save(*)
  create_or_update
end

#save!Object

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


32
33
34
# File 'lib/mongomodel/document/persistence.rb', line 32

def save!(*)
  create_or_update || raise(DocumentNotSaved)
end

#update_attribute(name, value) ⇒ Object

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.


68
69
70
71
# File 'lib/mongomodel/document/persistence.rb', line 68

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

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

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.


54
55
56
57
# File 'lib/mongomodel/document/persistence.rb', line 54

def update_attributes(attributes, options={})
  self.assign_attributes(attributes, options)
  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 docuemnt is invalid.


61
62
63
64
# File 'lib/mongomodel/document/persistence.rb', line 61

def update_attributes!(attributes, options={})
  self.assign_attributes(attributes, options)
  save!
end