48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/mongo_db/object/object_serializer.rb', line 48
def update opts, collection
opts, validate, callbacks = parse_object_options opts
created_objects, updated_objects, destroyed_objects = [], [], []
if callbacks
original_ids = original_objects.collect{|obj| obj.object_id}.to_set
objects.each do |obj|
(original_ids.include?(obj.object_id) ? updated_objects : created_objects) << obj
end
objects_ids = objects.collect{|obj| obj.object_id}.to_set
destroyed_objects = original_objects.select{|obj| !objects_ids.include?(obj.object_id)}
all_successfull = [
run_callbacks(created_objects, [:before, :validate], [:before, :save], [:before, :create]),
run_callbacks(updated_objects, [:before, :validate], [:before, :save], [:before, :update]),
run_callbacks(destroyed_objects, [:before, :validate], [:before, :destroy])
].reduce(:&)
return false unless all_successfull
end
return false if validate and !valid?
doc = to_document
id = _id || raise("can't update document without id (#{doc})!")
collection.update_without_object({_id: id}, doc, opts)
update_internal_state!
if callbacks
run_callbacks(created_objects, [:after, :create], [:after, :save], [:after, :validate])
run_callbacks(updated_objects, [:after, :update], [:after, :save], [:after, :validate])
run_callbacks(destroyed_objects, [:after, :destroy], [:after, :validate])
end
true
end
|