hashrocket-mongomapper 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. data/.gitignore +7 -0
  2. data/History +70 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +39 -0
  5. data/Rakefile +73 -0
  6. data/VERSION +1 -0
  7. data/bin/mmconsole +56 -0
  8. data/lib/mongomapper.rb +70 -0
  9. data/lib/mongomapper/associations.rb +84 -0
  10. data/lib/mongomapper/associations/base.rb +69 -0
  11. data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +34 -0
  12. data/lib/mongomapper/associations/belongs_to_proxy.rb +22 -0
  13. data/lib/mongomapper/associations/many_documents_proxy.rb +103 -0
  14. data/lib/mongomapper/associations/many_embedded_polymorphic_proxy.rb +33 -0
  15. data/lib/mongomapper/associations/many_embedded_proxy.rb +17 -0
  16. data/lib/mongomapper/associations/many_polymorphic_proxy.rb +11 -0
  17. data/lib/mongomapper/associations/many_proxy.rb +6 -0
  18. data/lib/mongomapper/associations/proxy.rb +63 -0
  19. data/lib/mongomapper/callbacks.rb +106 -0
  20. data/lib/mongomapper/document.rb +337 -0
  21. data/lib/mongomapper/dynamic_finder.rb +38 -0
  22. data/lib/mongomapper/embedded_document.rb +267 -0
  23. data/lib/mongomapper/finder_options.rb +85 -0
  24. data/lib/mongomapper/key.rb +76 -0
  25. data/lib/mongomapper/observing.rb +50 -0
  26. data/lib/mongomapper/pagination.rb +52 -0
  27. data/lib/mongomapper/rails_compatibility/document.rb +15 -0
  28. data/lib/mongomapper/rails_compatibility/embedded_document.rb +25 -0
  29. data/lib/mongomapper/save_with_validation.rb +19 -0
  30. data/lib/mongomapper/serialization.rb +55 -0
  31. data/lib/mongomapper/serializers/json_serializer.rb +92 -0
  32. data/lib/mongomapper/support.rb +30 -0
  33. data/lib/mongomapper/validations.rb +61 -0
  34. data/mongomapper.gemspec +142 -0
  35. data/test/NOTE_ON_TESTING +1 -0
  36. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +53 -0
  37. data/test/functional/associations/test_belongs_to_proxy.rb +45 -0
  38. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +131 -0
  39. data/test/functional/associations/test_many_embedded_proxy.rb +106 -0
  40. data/test/functional/associations/test_many_polymorphic_proxy.rb +261 -0
  41. data/test/functional/associations/test_many_proxy.rb +295 -0
  42. data/test/functional/test_associations.rb +47 -0
  43. data/test/functional/test_callbacks.rb +85 -0
  44. data/test/functional/test_document.rb +952 -0
  45. data/test/functional/test_pagination.rb +81 -0
  46. data/test/functional/test_rails_compatibility.rb +30 -0
  47. data/test/functional/test_validations.rb +172 -0
  48. data/test/models.rb +139 -0
  49. data/test/test_helper.rb +67 -0
  50. data/test/unit/serializers/test_json_serializer.rb +157 -0
  51. data/test/unit/test_association_base.rb +144 -0
  52. data/test/unit/test_document.rb +123 -0
  53. data/test/unit/test_embedded_document.rb +526 -0
  54. data/test/unit/test_finder_options.rb +183 -0
  55. data/test/unit/test_key.rb +247 -0
  56. data/test/unit/test_mongomapper.rb +28 -0
  57. data/test/unit/test_observing.rb +101 -0
  58. data/test/unit/test_pagination.rb +113 -0
  59. data/test/unit/test_rails_compatibility.rb +34 -0
  60. data/test/unit/test_serializations.rb +52 -0
  61. data/test/unit/test_validations.rb +500 -0
  62. metadata +189 -0
@@ -0,0 +1,34 @@
1
+ module MongoMapper
2
+ module Associations
3
+ class BelongsToPolymorphicProxy < Proxy
4
+ def replace(doc)
5
+ if doc
6
+ doc.save if doc.new?
7
+ id, type = doc.id, doc.class.name
8
+ end
9
+
10
+ @owner.send("#{@association.foreign_key}=", id)
11
+ @owner.send("#{@association.type_key_name}=", type)
12
+ reset
13
+ end
14
+
15
+ protected
16
+ def find_target
17
+ if proxy_id && proxy_class
18
+ proxy_class.find_by_id(proxy_id)
19
+ end
20
+ end
21
+
22
+ def proxy_id
23
+ @proxy_id ||= @owner.send(@association.foreign_key)
24
+ end
25
+
26
+ def proxy_class
27
+ @proxy_class ||= begin
28
+ klass = @owner.send(@association.type_key_name)
29
+ klass && klass.constantize
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,22 @@
1
+ module MongoMapper
2
+ module Associations
3
+ class BelongsToProxy < Proxy
4
+ def replace(doc)
5
+ if doc
6
+ doc.save if doc.new?
7
+ id = doc.id
8
+ end
9
+
10
+ @owner.send("#{@association.foreign_key}=", id)
11
+ reset
12
+ end
13
+
14
+ protected
15
+ def find_target
16
+ if association_id = @owner.send(@association.foreign_key)
17
+ @association.klass.find_by_id(association_id)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,103 @@
1
+ module MongoMapper
2
+ module Associations
3
+ class ManyDocumentsProxy < Proxy
4
+ delegate :klass, :to => :@association
5
+
6
+ def find(*args)
7
+ options = args.extract_options!
8
+ klass.find(*args << scoped_options(options))
9
+ end
10
+
11
+ def paginate(options)
12
+ klass.paginate(scoped_options(options))
13
+ end
14
+
15
+ def all(options={})
16
+ find(:all, scoped_options(options))
17
+ end
18
+
19
+ def first(options={})
20
+ find(:first, scoped_options(options))
21
+ end
22
+
23
+ def last(options={})
24
+ find(:last, scoped_options(options))
25
+ end
26
+
27
+ def count(conditions={})
28
+ klass.count(conditions.deep_merge(scoped_conditions))
29
+ end
30
+
31
+ def replace(docs)
32
+ @target.map(&:destroy) if load_target
33
+ docs.each { |doc| apply_scope(doc).save }
34
+ reset
35
+ end
36
+
37
+ def <<(*docs)
38
+ ensure_owner_saved
39
+ flatten_deeper(docs).each { |doc| apply_scope(doc).save }
40
+ reset
41
+ end
42
+ alias_method :push, :<<
43
+ alias_method :concat, :<<
44
+
45
+ def build(attrs={})
46
+ doc = klass.new(attrs)
47
+ apply_scope(doc)
48
+ doc
49
+ end
50
+
51
+ def create(attrs={})
52
+ doc = klass.new(attrs)
53
+ apply_scope(doc).save
54
+ doc
55
+ end
56
+
57
+ def destroy_all(conditions={})
58
+ all(:conditions => conditions).map(&:destroy)
59
+ reset
60
+ end
61
+
62
+ def delete_all(conditions={})
63
+ klass.delete_all(conditions.deep_merge(scoped_conditions))
64
+ reset
65
+ end
66
+
67
+ def nullify
68
+ criteria = FinderOptions.to_mongo_criteria(scoped_conditions)
69
+ all(criteria).each do |doc|
70
+ doc.update_attributes self.foreign_key => nil
71
+ end
72
+ reset
73
+ end
74
+
75
+ protected
76
+ def scoped_conditions
77
+ {self.foreign_key => @owner.id}
78
+ end
79
+
80
+ def scoped_options(options)
81
+ options.deep_merge({:conditions => scoped_conditions})
82
+ end
83
+
84
+ def find_target
85
+ find(:all)
86
+ end
87
+
88
+ def ensure_owner_saved
89
+ @owner.save if @owner.new?
90
+ end
91
+
92
+ def apply_scope(doc)
93
+ ensure_owner_saved
94
+ doc.send("#{self.foreign_key}=", @owner.id)
95
+ doc
96
+ end
97
+
98
+ def foreign_key
99
+ @association.options[:foreign_key] || @owner.class.name.underscore.gsub("/", "_") + "_id"
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,33 @@
1
+ module MongoMapper
2
+ module Associations
3
+ class ManyEmbeddedPolymorphicProxy < Proxy
4
+ def replace(v)
5
+ @_values = v.map do |doc_or_hash|
6
+ if doc_or_hash.kind_of?(EmbeddedDocument)
7
+ doc = doc_or_hash
8
+ {@association.type_key_name => doc.class.name}.merge(doc.attributes)
9
+ else
10
+ doc_or_hash
11
+ end
12
+ end
13
+
14
+ reset
15
+ end
16
+
17
+ protected
18
+ def find_target
19
+ (@_values || []).map do |hash|
20
+ polymorphic_class(hash).new(hash)
21
+ end
22
+ end
23
+
24
+ def polymorphic_class(doc)
25
+ if class_name = doc[@association.type_key_name]
26
+ class_name.constantize
27
+ else
28
+ @association.klass
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,17 @@
1
+ module MongoMapper
2
+ module Associations
3
+ class ManyEmbeddedProxy < Proxy
4
+ def replace(v)
5
+ @_values = v.map { |e| e.kind_of?(EmbeddedDocument) ? e.attributes : e }
6
+ reset
7
+ end
8
+
9
+ protected
10
+ def find_target
11
+ (@_values || []).map do |e|
12
+ @association.klass.new(e)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ module MongoMapper
2
+ module Associations
3
+ class ManyPolymorphicProxy < ManyDocumentsProxy
4
+ private
5
+ def apply_scope(doc)
6
+ doc.send("#{@association.type_key_name}=", doc.class.name)
7
+ super
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ module MongoMapper
2
+ module Associations
3
+ class ManyProxy < ManyDocumentsProxy
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,63 @@
1
+ module MongoMapper
2
+ module Associations
3
+ class Proxy < BasicObject
4
+ attr_reader :owner, :association
5
+
6
+ def initialize(owner, association)
7
+ @owner = owner
8
+ @association = association
9
+ reset
10
+ end
11
+
12
+ def respond_to?(*methods)
13
+ (load_target && @target.respond_to?(*methods))
14
+ end
15
+
16
+ def reset
17
+ @target = nil
18
+ end
19
+
20
+ def reload_target
21
+ reset
22
+ load_target
23
+ self
24
+ end
25
+
26
+ def send(method, *args)
27
+ load_target
28
+ @target.send(method, *args)
29
+ end
30
+
31
+ def replace(v)
32
+ raise NotImplementedError
33
+ end
34
+
35
+ protected
36
+ def method_missing(method, *args)
37
+ if load_target
38
+ if block_given?
39
+ @target.send(method, *args) { |*block_args| yield(*block_args) }
40
+ else
41
+ @target.send(method, *args)
42
+ end
43
+ end
44
+ end
45
+
46
+ def load_target
47
+ @target ||= find_target
48
+ end
49
+
50
+ def find_target
51
+ raise NotImplementedError
52
+ end
53
+
54
+ # Array#flatten has problems with recursive arrays. Going one level
55
+ # deeper solves the majority of the problems.
56
+ def flatten_deeper(array)
57
+ array.collect do |element|
58
+ (element.respond_to?(:flatten) && !element.is_a?(Hash)) ? element.flatten : element
59
+ end.flatten
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,106 @@
1
+ module MongoMapper
2
+ module Callbacks
3
+ def self.included(model) #:nodoc:
4
+ model.class_eval do
5
+ extend Observable
6
+ include ActiveSupport::Callbacks
7
+
8
+ define_callbacks *%w(
9
+ before_save after_save before_create after_create before_update after_update before_validation
10
+ after_validation before_validation_on_create after_validation_on_create before_validation_on_update
11
+ after_validation_on_update before_destroy after_destroy
12
+ )
13
+
14
+ [:create_or_update, :valid?, :create, :update, :destroy].each do |method|
15
+ alias_method_chain method, :callbacks
16
+ end
17
+ end
18
+ end
19
+
20
+ def before_save() end
21
+
22
+ def after_save() end
23
+ def create_or_update_with_callbacks #:nodoc:
24
+ return false if callback(:before_save) == false
25
+ if result = create_or_update_without_callbacks
26
+ callback(:after_save)
27
+ end
28
+ result
29
+ end
30
+ private :create_or_update_with_callbacks
31
+
32
+ def before_create() end
33
+
34
+ def after_create() end
35
+ def create_with_callbacks #:nodoc:
36
+ return false if callback(:before_create) == false
37
+ result = create_without_callbacks
38
+ callback(:after_create)
39
+ result
40
+ end
41
+ private :create_with_callbacks
42
+
43
+ def before_update() end
44
+
45
+ def after_update() end
46
+
47
+ def update_with_callbacks(*args) #:nodoc:
48
+ return false if callback(:before_update) == false
49
+ result = update_without_callbacks(*args)
50
+ callback(:after_update)
51
+ result
52
+ end
53
+ private :update_with_callbacks
54
+
55
+ def before_validation() end
56
+
57
+ def after_validation() end
58
+
59
+ def before_validation_on_create() end
60
+
61
+ def after_validation_on_create() end
62
+
63
+ def before_validation_on_update() end
64
+
65
+ def after_validation_on_update() end
66
+
67
+ def valid_with_callbacks? #:nodoc:
68
+ return false if callback(:before_validation) == false
69
+ result = new? ? callback(:before_validation_on_create) : callback(:before_validation_on_update)
70
+ return false if false == result
71
+
72
+ result = valid_without_callbacks?
73
+ callback(:after_validation)
74
+
75
+ new? ? callback(:after_validation_on_create) : callback(:after_validation_on_update)
76
+ return result
77
+ end
78
+
79
+ def before_destroy() end
80
+
81
+ def after_destroy() end
82
+ def destroy_with_callbacks #:nodoc:
83
+ return false if callback(:before_destroy) == false
84
+ result = destroy_without_callbacks
85
+ callback(:after_destroy)
86
+ result
87
+ end
88
+
89
+ private
90
+ def callback(method)
91
+ result = run_callbacks(method) { |result, object| false == result }
92
+
93
+ if result != false && respond_to?(method)
94
+ result = send(method)
95
+ end
96
+
97
+ notify(method)
98
+ return result
99
+ end
100
+
101
+ def notify(method) #:nodoc:
102
+ self.class.changed
103
+ self.class.notify_observers(method, self)
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,337 @@
1
+ require 'set'
2
+
3
+ module MongoMapper
4
+ module Document
5
+ def self.included(model)
6
+ model.class_eval do
7
+ include EmbeddedDocument
8
+ include InstanceMethods
9
+ include Observing
10
+ include Callbacks
11
+ include SaveWithValidation
12
+ include RailsCompatibility::Document
13
+ extend Validations::Macros
14
+ extend ClassMethods
15
+ end
16
+
17
+ descendants << model
18
+ end
19
+
20
+ def self.descendants
21
+ @descendants ||= Set.new
22
+ end
23
+
24
+ module ClassMethods
25
+ def find(*args)
26
+ options = args.extract_options!
27
+
28
+ case args.first
29
+ when :first then find_first(options)
30
+ when :last then find_last(options)
31
+ when :all then find_every(options)
32
+ else find_from_ids(args, options)
33
+ end
34
+ end
35
+
36
+ def paginate(options)
37
+ per_page = options.delete(:per_page)
38
+ page = options.delete(:page)
39
+ total_entries = count(options[:conditions] || {})
40
+ collection = Pagination::PaginationProxy.new(total_entries, page, per_page)
41
+
42
+ options[:limit] = collection.limit
43
+ options[:offset] = collection.offset
44
+
45
+ collection.subject = find_every(options)
46
+ collection
47
+ end
48
+
49
+ def first(options={})
50
+ find_first(options)
51
+ end
52
+
53
+ def last(options={})
54
+ find_last(options)
55
+ end
56
+
57
+ def all(options={})
58
+ find_every(options)
59
+ end
60
+
61
+ def find_by_id(id)
62
+ criteria = FinderOptions.to_mongo_criteria(:_id => id)
63
+ if doc = collection.find_first(criteria)
64
+ new(doc)
65
+ end
66
+ end
67
+
68
+ def count(conditions={})
69
+ collection.count(FinderOptions.to_mongo_criteria(conditions))
70
+ end
71
+
72
+ def create(*docs)
73
+ instances = []
74
+ docs = [{}] if docs.blank?
75
+ docs.flatten.each do |attrs|
76
+ doc = new(attrs); doc.save
77
+ instances << doc
78
+ end
79
+ instances.size == 1 ? instances[0] : instances
80
+ end
81
+
82
+ # For updating single document
83
+ # Person.update(1, {:foo => 'bar'})
84
+ #
85
+ # For updating multiple documents at once:
86
+ # Person.update({'1' => {:foo => 'bar'}, '2' => {:baz => 'wick'}})
87
+ def update(*args)
88
+ updating_multiple = args.length == 1
89
+ if updating_multiple
90
+ update_multiple(args[0])
91
+ else
92
+ id, attributes = args
93
+ update_single(id, attributes)
94
+ end
95
+ end
96
+
97
+ def delete(*ids)
98
+ criteria = FinderOptions.to_mongo_criteria(:_id => ids.flatten)
99
+ collection.remove(criteria)
100
+ end
101
+
102
+ def delete_all(conditions={})
103
+ criteria = FinderOptions.to_mongo_criteria(conditions)
104
+ collection.remove(criteria)
105
+ end
106
+
107
+ def destroy(*ids)
108
+ find_some(ids.flatten).each(&:destroy)
109
+ end
110
+
111
+ def destroy_all(conditions={})
112
+ find(:all, :conditions => conditions).each(&:destroy)
113
+ end
114
+
115
+ def connection(mongo_connection=nil)
116
+ if mongo_connection.nil?
117
+ @connection ||= MongoMapper.connection
118
+ else
119
+ @connection = mongo_connection
120
+ end
121
+ @connection
122
+ end
123
+
124
+ def database(name=nil)
125
+ if name.nil?
126
+ @database ||= MongoMapper.database
127
+ else
128
+ @database = connection.db(name)
129
+ end
130
+ @database
131
+ end
132
+
133
+ def collection(name=nil)
134
+ if name.nil?
135
+ @collection ||= database.collection(self.to_s.demodulize.tableize)
136
+ else
137
+ @collection = database.collection(name)
138
+ end
139
+ @collection
140
+ end
141
+
142
+ def timestamps!
143
+ key :created_at, Time
144
+ key :updated_at, Time
145
+
146
+ class_eval { before_save :update_timestamps }
147
+ end
148
+
149
+ protected
150
+ def method_missing(method, *args)
151
+ finder = DynamicFinder.new(self, method)
152
+
153
+ if finder.valid?
154
+ meta_def(finder.options[:method]) do |*args|
155
+ find_with_args(args, finder.options)
156
+ end
157
+
158
+ send(finder.options[:method], *args)
159
+ else
160
+ super
161
+ end
162
+ end
163
+
164
+ private
165
+ def find_every(options)
166
+ criteria, options = FinderOptions.new(options).to_a
167
+ collection.find(criteria, options).to_a.map { |doc| new(doc) }
168
+ end
169
+
170
+ def find_first(options)
171
+ options.merge!(:limit => 1)
172
+ find_every({:order => '$natural asc'}.merge(options))[0]
173
+ end
174
+
175
+ def find_last(options)
176
+ options.merge!(:limit => 1)
177
+ options[:order] = invert_order_clause(options)
178
+ find_every(options)[0]
179
+ #find_every({:order => '$natural desc'}.merge(invert_order_clause(options)))[0]
180
+ end
181
+
182
+ def invert_order_clause(options)
183
+ return '$natural desc' unless options[:order]
184
+ options[:order].split(',').map do |order_segment|
185
+ if order_segment =~ /\sasc/i
186
+ order_segment.sub /\sasc/i, ' desc'
187
+ elsif order_segment =~ /\sdesc/i
188
+ order_segment.sub /\sdesc/i, ' asc'
189
+ else
190
+ "#{order_segment.strip} desc"
191
+ end
192
+ end.join(',')
193
+ end
194
+
195
+ def find_some(ids, options={})
196
+ documents = find_every(options.deep_merge(:conditions => {'_id' => ids}))
197
+ if ids.size == documents.size
198
+ documents
199
+ else
200
+ raise DocumentNotFound, "Couldn't find all of the ids (#{ids.to_sentence}). Found #{documents.size}, but was expecting #{ids.size}"
201
+ end
202
+ end
203
+
204
+ def find_one(id, options={})
205
+ if doc = find_every(options.deep_merge(:conditions => {:_id => id})).first
206
+ doc
207
+ else
208
+ raise DocumentNotFound, "Document with id of #{id} does not exist in collection named #{collection.name}"
209
+ end
210
+ end
211
+
212
+ def find_from_ids(ids, options={})
213
+ ids = ids.flatten.compact.uniq
214
+
215
+ case ids.size
216
+ when 0
217
+ raise(DocumentNotFound, "Couldn't find without an ID")
218
+ when 1
219
+ find_one(ids[0], options)
220
+ else
221
+ find_some(ids, options)
222
+ end
223
+ end
224
+
225
+ def find_with_args(args, options)
226
+ attributes, = {}
227
+ find_options = args.extract_options!.deep_merge(:conditions => attributes)
228
+
229
+ options[:attribute_names].each_with_index do |attr, index|
230
+ attributes[attr] = args[index]
231
+ end
232
+
233
+ result = find(options[:finder], find_options)
234
+
235
+ if result.nil?
236
+ if options[:bang]
237
+ raise DocumentNotFound, "Couldn't find Document with #{attributes.inspect} in collection named #{collection.name}"
238
+ end
239
+
240
+ if options[:instantiator]
241
+ self.send(options[:instantiator], attributes)
242
+ end
243
+ else
244
+ result
245
+ end
246
+ end
247
+
248
+ def update_single(id, attrs)
249
+ if id.blank? || attrs.blank? || !attrs.is_a?(Hash)
250
+ raise ArgumentError, "Updating a single document requires an id and a hash of attributes"
251
+ end
252
+
253
+ doc = find(id)
254
+ doc.update_attributes(attrs)
255
+ doc
256
+ end
257
+
258
+ def update_multiple(docs)
259
+ unless docs.is_a?(Hash)
260
+ raise ArgumentError, "Updating multiple documents takes 1 argument and it must be hash"
261
+ end
262
+
263
+ instances = []
264
+ docs.each_pair { |id, attrs| instances << update(id, attrs) }
265
+ instances
266
+ end
267
+ end
268
+
269
+ module InstanceMethods
270
+ def collection
271
+ self.class.collection
272
+ end
273
+
274
+ def new?
275
+ read_attribute('_id').blank? || using_custom_id?
276
+ end
277
+
278
+ def save
279
+ create_or_update
280
+ end
281
+
282
+ def save!
283
+ create_or_update || raise(DocumentNotValid.new(self))
284
+ end
285
+
286
+ def update_attributes(attrs={})
287
+ self.attributes = attrs
288
+ save
289
+ end
290
+
291
+ def destroy
292
+ return false if frozen?
293
+
294
+ criteria = FinderOptions.to_mongo_criteria(:_id => id)
295
+ collection.remove(criteria) unless new?
296
+ freeze
297
+ end
298
+
299
+ private
300
+ def create_or_update
301
+ result = new? ? create : update
302
+ result != false
303
+ end
304
+
305
+ def create
306
+ assign_id
307
+ save_to_collection
308
+ end
309
+
310
+ def assign_id
311
+ if read_attribute(:_id).blank?
312
+ write_attribute(:_id, XGen::Mongo::Driver::ObjectID.new.to_s)
313
+ end
314
+ end
315
+
316
+ def update
317
+ save_to_collection
318
+ end
319
+
320
+ # collection.save returns mongoid
321
+ def save_to_collection
322
+ clear_custom_id_flag
323
+ collection.save(attributes)
324
+ end
325
+
326
+ def update_timestamps
327
+ now = Time.now.utc
328
+ write_attribute('created_at', now) if new?
329
+ write_attribute('updated_at', now)
330
+ end
331
+
332
+ def clear_custom_id_flag
333
+ @using_custom_id = nil
334
+ end
335
+ end
336
+ end # Document
337
+ end # MongoMapper