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/atomic/inc.rb,
lib/mongoid/persistence/atomic/bit.rb,
lib/mongoid/persistence/atomic/pop.rb,
lib/mongoid/persistence/operations.rb,
lib/mongoid/persistence/atomic/push.rb,
lib/mongoid/persistence/atomic/sets.rb,
lib/mongoid/persistence/atomic/pull.rb,
lib/mongoid/persistence/modification.rb,
lib/mongoid/persistence/atomic/unset.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/operations/remove.rb,
lib/mongoid/persistence/atomic/add_to_set.rb,
lib/mongoid/persistence/operations/update.rb,
lib/mongoid/persistence/operations/insert.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
Constant Summary
Constant Summary
Constants included from Atomic
Instance Method Summary (collapse)
-
- (true, false) destroy(options = {})
Remove the document from the database with callbacks.
-
- (Document) insert(options = {})
Insert a new document into the database.
-
- (TrueClass) remove(options = {})
(also: #delete)
Remove the document from the database.
-
- (true, false) save!(options = {})
Save the document - will perform an insert if the document is new, and update if not.
-
- (true) touch(field = nil)
Touch the document, in effect updating it's updated_at timestamp and optionally the provided field to the current time.
-
- (true, false) update(options = {})
Update the document in the database.
-
- (true, false) update_attribute(name, value)
Update a single attribute and persist the entire document.
-
- (true, false) update_attributes(attributes = {}, options = {})
Update the document attributes in the database.
-
- (true, false) update_attributes!(attributes = {}, options = {})
Update the document attributes in the database and raise an error if validation failed.
-
- (true, false) upsert(options = {})
(also: #save)
Upsert the document - will perform an insert if the document is new, and update if not.
Methods included from Atomic
#add_atomic_pull, #atomic_array_add_to_sets, #atomic_array_pulls, #atomic_array_pushes, #atomic_attribute_name, #atomic_delete_modifier, #atomic_insert_modifier, #atomic_path, #atomic_position, #atomic_pulls, #atomic_pushes, #atomic_selector, #atomic_sets, #atomic_unsets, #atomic_updates, #delayed_atomic_pulls, #delayed_atomic_sets
Instance Method Details
- (true, false) destroy(options = {})
Remove the document from the database with callbacks.
30 31 32 33 34 35 36 37 |
# File 'lib/mongoid/persistence.rb', line 30 def destroy( = {}) self.flagged_for_destroy = true result = run_callbacks(:destroy) do remove() end self.flagged_for_destroy = false result end |
- (Document) insert(options = {})
Insert a new document into the database. Will return the document itself whether or not the save was successful.
48 49 50 |
# File 'lib/mongoid/persistence.rb', line 48 def insert( = {}) Operations.insert(self, ).persist end |
- (TrueClass) remove(options = {}) Also known as: delete
Remove the document from the database.
60 61 62 |
# File 'lib/mongoid/persistence.rb', line 60 def remove( = {}) Operations.remove(self, ).persist end |
- (true, false) save!(options = {})
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.
74 75 76 77 78 79 80 |
# File 'lib/mongoid/persistence.rb', line 74 def save!( = {}) unless upsert() self.class.fail_validate!(self) if errors.any? self.class.fail_callback!(self, :save!) end return true end |
- (true) touch(field = nil)
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.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/mongoid/persistence.rb', line 99 def touch(field = nil) current = Time.now write_attribute(:updated_at, current) if fields["updated_at"] write_attribute(field, current) if field collection.find(atomic_selector).update(atomic_updates) without_autobuild do touchables.each { |name| send(name).try(:touch) } end move_changes and true end |
- (true, false) update(options = {})
Update the document in the database.
118 119 120 |
# File 'lib/mongoid/persistence.rb', line 118 def update( = {}) Operations.update(self, ).persist end |
- (true, false) update_attribute(name, value)
Update a single attribute and persist the entire document. This skips validation but fires the callbacks.
137 138 139 140 141 142 143 |
# File 'lib/mongoid/persistence.rb', line 137 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 |
- (true, false) update_attributes(attributes = {}, options = {})
Update the document attributes in the database.
153 154 155 |
# File 'lib/mongoid/persistence.rb', line 153 def update_attributes(attributes = {}, = {}) assign_attributes(attributes, ); save end |
- (true, false) update_attributes!(attributes = {}, options = {})
Update the document attributes in the database and raise an error if validation failed.
168 169 170 171 172 173 174 175 |
# File 'lib/mongoid/persistence.rb', line 168 def update_attributes!(attributes = {}, = {}) result = update_attributes(attributes, ) unless result self.class.fail_validate!(self) if errors.any? self.class.fail_callback!(self, :update_attributes!) end result end |
- (true, false) upsert(options = {}) Also known as: save
Upsert the document - will perform an insert if the document is new, and update if not.
186 187 188 189 190 191 192 |
# File 'lib/mongoid/persistence.rb', line 186 def upsert( = {}) if new_record? insert().persisted? else update() end end |