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/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/embedded/insert.rb,
lib/mongoid/persistence/operations/embedded/remove.rb
Overview
:nodoc:
Defined Under Namespace
Modules: Atomic, ClassMethods, Deletion, Insertion, Modification, Operations
Instance Method Summary collapse
-
#destroy(options = {}) ⇒ true, false
Remove the document from the datbase with callbacks.
-
#insert(options = {}) ⇒ Document
Insert a new document into the database.
-
#remove(options = {}) ⇒ TrueClass
(also: #delete)
Remove the document from the datbase.
-
#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 datbase.
-
#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 datbase.
-
#update_attributes!(attributes = {}, options = {}) ⇒ true, false
Update the document attributes in the database and raise an error if validation failed.
-
#upsert(options = {}) ⇒ true, false
(also: #save)
Upsert the document - will perform an insert if the document is new, and update if not.
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 datbase with callbacks.
30 31 32 33 34 35 36 37 |
# File 'lib/mongoid/persistence.rb', line 30 def destroy( = {}) self.flagged_for_destroy = true run_callbacks(:destroy) do remove() end.tap do self.flagged_for_destroy = false end end |
#insert(options = {}) ⇒ Document
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 |
#remove(options = {}) ⇒ TrueClass Also known as: delete
Remove the document from the datbase.
60 61 62 |
# File 'lib/mongoid/persistence.rb', line 60 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. 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 |
#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.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/mongoid/persistence.rb', line 100 def touch(field = nil) return false if _root.new_record? current = Time.now write_attribute(:updated_at, current) if fields["updated_at"] write_attribute(field, current) if field touches = touch_atomic_updates(field) unless touches.empty? _root.collection.update(atomic_selector, touches) end touchables.each { |name| send(name).try(:touch) } move_changes and true end |
#update(options = {}) ⇒ true, false
Update the document in the datbase.
123 124 125 |
# File 'lib/mongoid/persistence.rb', line 123 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.
139 140 141 142 |
# File 'lib/mongoid/persistence.rb', line 139 def update_attribute(name, value) write_attribute(name, value) save(:validate => false) end |
#update_attributes(attributes = {}, options = {}) ⇒ true, false
Update the document attributes in the datbase.
152 153 154 |
# File 'lib/mongoid/persistence.rb', line 152 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.
167 168 169 170 171 172 173 174 |
# File 'lib/mongoid/persistence.rb', line 167 def update_attributes!(attributes = {}, = {}) update_attributes(attributes, ).tap do |result| unless result self.class.fail_validate!(self) if errors.any? self.class.fail_callback!(self, :update_attributes!) end end end |
#upsert(options = {}) ⇒ true, false Also known as: save
Upsert the document - will perform an insert if the document is new, and update if not.
185 186 187 188 189 190 191 |
# File 'lib/mongoid/persistence.rb', line 185 def upsert( = {}) if new_record? !insert().new_record? else update() end end |