lib/mongoid/relations/referenced/many_to_many.rb in mongoid-3.0.23 vs lib/mongoid/relations/referenced/many_to_many.rb in mongoid-3.1.0

- old
+ new

@@ -134,25 +134,36 @@ # @example Nullify the relation. # person.preferences.nullify # # @since 2.0.0.rc.1 def nullify + target.each do |doc| + execute_callback :before_remove, doc + end unless metadata.forced_nil_inverse? criteria.pull(inverse_foreign_key, base.id) end if persistable? base.set( foreign_key, base.send(foreign_key).clear ) end - target.clear do |doc| + after_remove_error = nil + many_to_many = target.clear do |doc| unbind_one(doc) unless metadata.forced_nil_inverse? doc.changed_attributes.delete(inverse_foreign_key) end + begin + execute_callback :after_remove, doc + rescue => e + after_remove_error = e + end end + raise after_remove_error if after_remove_error + many_to_many end alias :nullify_all :nullify alias :clear :nullify alias :purge :nullify @@ -201,13 +212,15 @@ # # @param [ Document ] document The document to append to the target. # # @since 2.0.0.rc.1 def append(document) + execute_callback :before_add, document target.push(document) characterize_one(document) bind_one(document) + execute_callback :after_add, document end # Instantiate the binding associated with this relation. # # @example Get the binding. @@ -298,11 +311,15 @@ # # @return [ Criteria ] The criteria. # # @since 2.1.0 def criteria(metadata, object, type = nil) - apply_ordering(metadata.klass.all_of(_id: { "$in" => object || [] }), metadata) + apply_ordering( + metadata.klass.all_of( + metadata.primary_key => { "$in" => object || [] } + ), metadata + ) end # Get the criteria that is used to eager load a relation of this # type. # @@ -443,10 +460,21 @@ # # @return [ Array<Symbol> ] The valid options. # # @since 2.1.0 def valid_options - [ :autosave, :dependent, :foreign_key, :index, :order ] + [ + :after_add, + :after_remove, + :autosave, + :before_add, + :before_remove, + :dependent, + :foreign_key, + :index, + :order, + :primary_key + ] end # Get the default validation setting for the relation. Determines if # by default a validates associated will occur. #