mongoid 0.2.6 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +3 -0
- data/VERSION +1 -1
- data/lib/mongoid/document.rb +15 -1
- data/mongoid.gemspec +1 -1
- data/spec/spec_helper.rb +4 -0
- data/spec/unit/mongoid/document_spec.rb +15 -0
- data/spec/unit/mongoid/finders_spec.rb +8 -0
- metadata +1 -1
data/History.txt
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.7
|
data/lib/mongoid/document.rb
CHANGED
@@ -40,7 +40,7 @@ module Mongoid #:nodoc:
|
|
40
40
|
# For each field that is defined, a getter and setter will be
|
41
41
|
# added as an instance method to the Document.
|
42
42
|
def fields(*names)
|
43
|
-
@fields
|
43
|
+
@fields ||= []
|
44
44
|
names.flatten.each do |name|
|
45
45
|
@fields << name
|
46
46
|
define_method(name) { read_attribute(name) }
|
@@ -58,6 +58,15 @@ module Mongoid #:nodoc:
|
|
58
58
|
add_association(:has_one, association_name.to_s.titleize, association_name)
|
59
59
|
end
|
60
60
|
|
61
|
+
# Adds timestamps on the Document in the form of the fields 'created_on'
|
62
|
+
# and 'last_modified'
|
63
|
+
def has_timestamps
|
64
|
+
fields :created_on, :last_modified
|
65
|
+
class_eval do
|
66
|
+
before_save :update_timestamps
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
61
70
|
# Adds an index on the field specified. Options can be :unique => true or
|
62
71
|
# :unique => false. It will default to the latter.
|
63
72
|
def index(name, options = { :unique => false })
|
@@ -126,6 +135,11 @@ module Mongoid #:nodoc:
|
|
126
135
|
@attributes = attributes.symbolize_keys!; save; true
|
127
136
|
end
|
128
137
|
|
138
|
+
def update_timestamps
|
139
|
+
self.created_on = Time.now
|
140
|
+
self.last_modified = Time.now
|
141
|
+
end
|
142
|
+
|
129
143
|
private
|
130
144
|
|
131
145
|
class << self
|
data/mongoid.gemspec
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -179,6 +179,21 @@ describe Mongoid::Document do
|
|
179
179
|
|
180
180
|
end
|
181
181
|
|
182
|
+
describe "#has_timestamps" do
|
183
|
+
|
184
|
+
before do
|
185
|
+
@tester = Tester.new
|
186
|
+
end
|
187
|
+
|
188
|
+
it "adds created_on and last_modified to the document" do
|
189
|
+
@collection.expects(:save).with(@tester.attributes)
|
190
|
+
@tester.save
|
191
|
+
@tester.created_on.should_not be_nil
|
192
|
+
@tester.last_modified.should_not be_nil
|
193
|
+
end
|
194
|
+
|
195
|
+
end
|
196
|
+
|
182
197
|
describe "#index" do
|
183
198
|
|
184
199
|
context "when unique options are not provided" do
|