Module: Mongoid::Persistence
- Extended by:
- ActiveSupport::Concern
- Includes:
- Atomic
- Included in:
- Components
- Defined in:
- lib/mongoid/persistence.rb,
lib/mongoid/persistence/atomic.rb,
lib/mongoid/persistence/deletion.rb,
lib/mongoid/persistence/insertion.rb,
lib/mongoid/persistence/upsertion.rb,
lib/mongoid/persistence/atomic/bit.rb,
lib/mongoid/persistence/atomic/inc.rb,
lib/mongoid/persistence/atomic/pop.rb,
lib/mongoid/persistence/operations.rb,
lib/mongoid/persistence/atomic/pull.rb,
lib/mongoid/persistence/atomic/push.rb,
lib/mongoid/persistence/atomic/sets.rb,
lib/mongoid/persistence/atomic/unset.rb,
lib/mongoid/persistence/modification.rb,
lib/mongoid/persistence/atomic/rename.rb,
lib/mongoid/persistence/atomic/pull_all.rb,
lib/mongoid/persistence/atomic/push_all.rb,
lib/mongoid/persistence/atomic/operation.rb,
lib/mongoid/persistence/atomic/add_to_set.rb,
lib/mongoid/persistence/operations/insert.rb,
lib/mongoid/persistence/operations/remove.rb,
lib/mongoid/persistence/operations/update.rb,
lib/mongoid/persistence/operations/upsert.rb,
lib/mongoid/persistence/operations/embedded/insert.rb,
lib/mongoid/persistence/operations/embedded/remove.rb
Overview
The persistence module is a mixin to provide database accessor methods for the document. These correspond to the appropriate accessors on a mongo collection and retain the same DSL.
Defined Under Namespace
Modules: Atomic, ClassMethods, Deletion, Insertion, Modification, Operations, Upsertion
Instance Method Summary collapse
-
#destroy(options = {}) ⇒ true, false
Remove the document from the database with callbacks.
-
#insert(options = {}) ⇒ Document
Insert a new document into the database.
-
#remove(options = {}) ⇒ TrueClass
(also: #delete)
Remove the document from the database.
-
#save(options = {}) ⇒ true, false
Save the document - will perform an insert if the document is new, and update if not.
-
#save!(options = {}) ⇒ true, false
Save the document - will perform an insert if the document is new, and update if not.
-
#touch(field = nil) ⇒ true
Touch the document, in effect updating it’s updated_at timestamp and optionally the provided field to the current time.
-
#update(options = {}) ⇒ true, false
Update the document in the database.
-
#update_attribute(name, value) ⇒ true, false
Update a single attribute and persist the entire document.
-
#update_attributes(attributes = {}, options = {}) ⇒ true, false
Update the document attributes in the database.
-
#update_attributes!(attributes = {}, options = {}) ⇒ true, false
Update the document attributes in the database and raise an error if validation failed.
-
#upsert(options = {}) ⇒ true
Perform an upsert of the document.
Methods included from Atomic
#add_to_set, #bit, #inc, #pop, #pull, #pull_all, #push, #push_all, #rename, #set, #unset
Instance Method Details
#destroy(options = {}) ⇒ true, false
Remove the document from the database with callbacks.
31 32 33 34 35 36 37 38 |
# File 'lib/mongoid/persistence.rb', line 31 def destroy( = {}) self.flagged_for_destroy = true result = run_callbacks(:destroy) do remove() end self.flagged_for_destroy = false result end |
#insert(options = {}) ⇒ Document
Insert a new document into the database. Will return the document itself whether or not the save was successful.
49 50 51 |
# File 'lib/mongoid/persistence.rb', line 49 def insert( = {}) Operations.insert(self, ).persist end |
#remove(options = {}) ⇒ TrueClass Also known as: delete
Remove the document from the database.
61 62 63 |
# File 'lib/mongoid/persistence.rb', line 61 def remove( = {}) Operations.remove(self, ).persist end |
#save(options = {}) ⇒ true, false
Save the document - will perform an insert if the document is new, and update if not.
77 78 79 80 81 82 83 |
# File 'lib/mongoid/persistence.rb', line 77 def save( = {}) if new_record? insert().persisted? else update() end end |
#save!(options = {}) ⇒ true, false
Save the document - will perform an insert if the document is new, and update if not. If a validation error occurs an error will get raised.
94 95 96 97 98 99 100 |
# File 'lib/mongoid/persistence.rb', line 94 def save!( = {}) unless save() self.class.fail_validate!(self) unless errors.empty? self.class.fail_callback!(self, :save!) end return true end |
#touch(field = nil) ⇒ true
This will not autobuild relations if those options are set.
Touch the document, in effect updating it’s updated_at timestamp and optionally the provided field to the current time. If any belongs_to relations exist with a touch option, they will be updated as well.
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/mongoid/persistence.rb', line 119 def touch(field = nil) current = Time.now write_attribute(:updated_at, current) if fields["updated_at"] write_attribute(field, current) if field _root.collection.find(atomic_selector).update(atomic_updates) without_autobuild do touchables.each { |name| send(name).try(:touch) } end move_changes and true end |
#update(options = {}) ⇒ true, false
Update the document in the database.
138 139 140 |
# File 'lib/mongoid/persistence.rb', line 138 def update( = {}) Operations.update(self, ).persist end |
#update_attribute(name, value) ⇒ true, false
Update a single attribute and persist the entire document. This skips validation but fires the callbacks.
157 158 159 160 161 162 163 |
# File 'lib/mongoid/persistence.rb', line 157 def update_attribute(name, value) unless attribute_writable?(name.to_s) raise Errors::ReadonlyAttribute.new(name, value) end write_attribute(name, value) save(validate: false) end |
#update_attributes(attributes = {}, options = {}) ⇒ true, false
Update the document attributes in the database.
173 174 175 |
# File 'lib/mongoid/persistence.rb', line 173 def update_attributes(attributes = {}, = {}) assign_attributes(attributes, ); save end |
#update_attributes!(attributes = {}, options = {}) ⇒ true, false
Update the document attributes in the database and raise an error if validation failed.
188 189 190 191 192 193 194 195 |
# File 'lib/mongoid/persistence.rb', line 188 def update_attributes!(attributes = {}, = {}) result = update_attributes(attributes, ) unless result self.class.fail_validate!(self) unless errors.empty? self.class.fail_callback!(self, :update_attributes!) end result end |
#upsert(options = {}) ⇒ true
Perform an upsert of the document. If the document does not exist in the database, then Mongo will insert a new one, otherwise the fields will get overwritten with new values on the existing document.
209 210 211 |
# File 'lib/mongoid/persistence.rb', line 209 def upsert( = {}) Operations.upsert(self, ).persist end |