Module: Mongoid::Persistable::Pushable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Mongoid::Persistable
- Defined in:
- build/mongoid-6.1/lib/mongoid/persistable/pushable.rb
Overview
Defines behaviour for $push and $addToSet operations.
Instance Method Summary collapse
-
#add_to_set(adds) ⇒ Document
Add the single values to the arrays only if the value does not already exist in the array.
-
#push(pushes) ⇒ Document
Push a single value or multiple values onto arrays.
Instance Method Details
#add_to_set(adds) ⇒ Document
Add the single values to the arrays only if the value does not already exist in the array.
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'build/mongoid-6.1/lib/mongoid/persistable/pushable.rb', line 22 def add_to_set(adds) prepare_atomic_operation do |ops| process_atomic_operations(adds) do |field, value| existing = send(field) || (attributes[field] ||= []) values = [ value ].flatten(1) values.each do |val| existing.push(val) unless existing.include?(val) end ops[atomic_attribute_name(field)] = { "$each" => values } end { "$addToSet" => ops } end end |
#push(pushes) ⇒ Document
Push a single value or multiple values onto arrays.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'build/mongoid-6.1/lib/mongoid/persistable/pushable.rb', line 49 def push(pushes) prepare_atomic_operation do |ops| process_atomic_operations(pushes) do |field, value| existing = send(field) || (attributes[field] ||= []) values = [ value ].flatten(1) values.each{ |val| existing.push(val) } ops[atomic_attribute_name(field)] = { "$each" => values } end { "$push" => ops } end end |