Module: Mongoid::Persistence
- Extended by:
- ActiveSupport::Concern
- Includes:
- Atomic, Atomic::Positionable
- 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
Constant Summary collapse
- LIST_OPERATIONS =
The atomic operations that deal with arrays or sets in the db.
[ "$addToSet", "$push", "$pull", "$pullAll" ].freeze
Constants included from Atomic
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/false
Touch the document, in effect updating its 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::Positionable
Methods included from Atomic
#add_atomic_pull, #add_atomic_unset, #atomic_array_add_to_sets, #atomic_array_pulls, #atomic_array_pushes, #atomic_attribute_name, #atomic_delete_modifier, #atomic_insert_modifier, #atomic_path, #atomic_paths, #atomic_position, #atomic_pulls, #atomic_pushes, #atomic_selector, #atomic_sets, #atomic_unsets, #atomic_updates, #delayed_atomic_pulls, #delayed_atomic_sets, #delayed_atomic_unsets, #flag_as_destroyed, #flagged_destroys, #process_flagged_destroys
Instance Method Details
#destroy(options = {}) ⇒ true, false
Remove the document from the database with callbacks.
37 38 39 40 41 42 43 44 |
# File 'lib/mongoid/persistence.rb', line 37 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.
55 56 57 |
# File 'lib/mongoid/persistence.rb', line 55 def insert( = {}) Operations.insert(self, ).persist end |
#remove(options = {}) ⇒ TrueClass Also known as: delete
Remove the document from the database.
67 68 69 |
# File 'lib/mongoid/persistence.rb', line 67 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.
83 84 85 86 87 88 89 |
# File 'lib/mongoid/persistence.rb', line 83 def save( = {}) if new_record? !insert().new_record? 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.
100 101 102 103 104 105 106 |
# File 'lib/mongoid/persistence.rb', line 100 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/false
This will not autobuild relations if those options are set.
Touch the document, in effect updating its 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.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/mongoid/persistence.rb', line 125 def touch(field = nil) return false if _root.new_record? current = Time.now field = database_field_name(field) write_attribute(:updated_at, current) if respond_to?("updated_at=") write_attribute(field, current) if field touches = touch_atomic_updates(field) unless touches.empty? selector = atomic_selector _root.collection.find(selector).update(positionally(selector, touches)) end run_callbacks(:touch, :after) true end |
#update(options = {}) ⇒ true, false
Update the document in the database.
149 150 151 |
# File 'lib/mongoid/persistence.rb', line 149 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.
168 169 170 171 172 173 174 175 |
# File 'lib/mongoid/persistence.rb', line 168 def update_attribute(name, value) normalized = name.to_s unless attribute_writable?(normalized) raise Errors::ReadonlyAttribute.new(normalized, value) end write_attribute(database_field_name(normalized), value) save(validate: false) end |
#update_attributes(attributes = {}, options = {}) ⇒ true, false
Update the document attributes in the database.
185 186 187 |
# File 'lib/mongoid/persistence.rb', line 185 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.
200 201 202 203 204 205 206 207 |
# File 'lib/mongoid/persistence.rb', line 200 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.
221 222 223 |
# File 'lib/mongoid/persistence.rb', line 221 def upsert( = {}) Operations.upsert(self, ).persist end |