mongoid 0.6.1 → 0.6.2
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.
- data/README.textile +29 -2
- data/Rakefile +0 -1
- data/VERSION +1 -1
- data/lib/mongoid.rb +2 -1
- data/lib/mongoid/criteria.rb +7 -2
- data/lib/mongoid/timestamps.rb +2 -3
- data/lib/mongoid/versioning.rb +27 -0
- data/mongoid.gemspec +7 -4
- data/spec/integration/mongoid/document_spec.rb +48 -0
- data/spec/spec_helper.rb +0 -60
- data/spec/unit/mongoid/associations/belongs_to_association_spec.rb +7 -0
- data/spec/unit/mongoid/associations/decorator_spec.rb +29 -0
- data/spec/unit/mongoid/associations/factory_spec.rb +19 -0
- data/spec/unit/mongoid/associations/has_many_association_spec.rb +11 -0
- data/spec/unit/mongoid/associations/has_one_association_spec.rb +4 -0
- data/spec/unit/mongoid/associations_spec.rb +43 -0
- data/spec/unit/mongoid/attributes_spec.rb +6 -1
- data/spec/unit/mongoid/commands_spec.rb +4 -0
- data/spec/unit/mongoid/criteria_spec.rb +32 -6
- data/spec/unit/mongoid/document_spec.rb +27 -1
- data/spec/unit/mongoid/extensions/array/conversions_spec.rb +5 -0
- data/spec/unit/mongoid/extensions/object/conversions_spec.rb +5 -0
- data/spec/unit/mongoid/extensions/object/parentization_spec.rb +12 -0
- data/spec/unit/mongoid/timestamps_spec.rb +5 -1
- data/spec/unit/mongoid/versioning_spec.rb +46 -0
- metadata +5 -2
data/README.textile
CHANGED
@@ -35,8 +35,9 @@ Example of a simple domain model:
|
|
35
35
|
|
36
36
|
<pre>
|
37
37
|
class Person < Mongoid::Document
|
38
|
+
include Mongoid::Versioning
|
38
39
|
field :title
|
39
|
-
field :age, :default => 0
|
40
|
+
field :age, :type => Integer, :default => 0
|
40
41
|
has_many :addresses
|
41
42
|
has_one :name
|
42
43
|
end
|
@@ -50,9 +51,9 @@ Example of a simple domain model:
|
|
50
51
|
end
|
51
52
|
|
52
53
|
class Name < Mongoid::Document
|
54
|
+
include Mongoid::Timestamps
|
53
55
|
field :first_name
|
54
56
|
field :last_name
|
55
|
-
has_timestamps
|
56
57
|
end
|
57
58
|
</pre>
|
58
59
|
|
@@ -88,6 +89,12 @@ Deleting:
|
|
88
89
|
Person.delete_all(:title => "Sir")
|
89
90
|
</pre>
|
90
91
|
|
92
|
+
Count:
|
93
|
+
|
94
|
+
<pre>
|
95
|
+
Person.count
|
96
|
+
</pre>
|
97
|
+
|
91
98
|
Retrieval of Documents from the database can be done in the traditional ActiveRecord
|
92
99
|
manner or done using the Mongoid criteria DSL.
|
93
100
|
|
@@ -104,6 +111,7 @@ New School:
|
|
104
111
|
|
105
112
|
<pre>
|
106
113
|
Person.select(:first_name, :last_name).where(:title => "Sir").skip(10).limit(10).execute
|
114
|
+
Person.select(:title).aggregate
|
107
115
|
Criteria.translate(:conditions => { :title => "Sir" }).execute
|
108
116
|
</pre>
|
109
117
|
|
@@ -113,6 +121,25 @@ Paginate Document search results:
|
|
113
121
|
Person.paginate(:conditions => {:title => "Esquire"}, :page => 1, :per_page => 20)
|
114
122
|
</pre>
|
115
123
|
|
124
|
+
Versioning:
|
125
|
+
|
126
|
+
Versioning can be added by including Mongoid::Versioning in your document. This will give
|
127
|
+
a version field, and will update the version number with each save. Additionally each
|
128
|
+
version of the document will be stored in a versions array, embedded within the document.
|
129
|
+
|
130
|
+
<pre>
|
131
|
+
class Person < Mongoid::Document
|
132
|
+
include Mongoid::Versioning
|
133
|
+
field :title
|
134
|
+
end
|
135
|
+
|
136
|
+
@person = Person.new
|
137
|
+
@person.save # Version 1
|
138
|
+
@person.title = "Madam"
|
139
|
+
@person.save # Version 2
|
140
|
+
@person.versions.size = 1
|
141
|
+
</pre>
|
142
|
+
|
116
143
|
Validations:
|
117
144
|
|
118
145
|
Mongoid supports all validations provided by Jay Fields' <tt>Validatable</tt> gem.
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.2
|
data/lib/mongoid.rb
CHANGED
@@ -38,8 +38,9 @@ require "mongoid/commands"
|
|
38
38
|
require "mongoid/criteria"
|
39
39
|
require "mongoid/extensions"
|
40
40
|
require "mongoid/field"
|
41
|
-
require "mongoid/document"
|
42
41
|
require "mongoid/timestamps"
|
42
|
+
require "mongoid/versioning"
|
43
|
+
require "mongoid/document"
|
43
44
|
|
44
45
|
module Mongoid
|
45
46
|
|
data/lib/mongoid/criteria.rb
CHANGED
@@ -100,8 +100,13 @@ module Mongoid #:nodoc:
|
|
100
100
|
# objects of the type of class provided.
|
101
101
|
def execute(klass = nil)
|
102
102
|
@klass = klass if klass
|
103
|
-
|
104
|
-
|
103
|
+
if type == :first
|
104
|
+
attributes = klass.collection.find_one(@selector, @options)
|
105
|
+
attributes ? @klass.new(attributes) : nil
|
106
|
+
else
|
107
|
+
attributes = @klass.collection.find(@selector, @options)
|
108
|
+
attributes ? attributes.collect { |doc| @klass.new(doc) } : []
|
109
|
+
end
|
105
110
|
end
|
106
111
|
|
107
112
|
# Adds a criterion to the +Criteria+ that specifies additional options
|
data/lib/mongoid/timestamps.rb
CHANGED
@@ -6,8 +6,7 @@ module Mongoid
|
|
6
6
|
include InstanceMethods
|
7
7
|
field :created_at, :type => Time
|
8
8
|
field :modified_at, :type => Time
|
9
|
-
|
10
|
-
before_save :update_modified_at
|
9
|
+
before_save :update_created_at, :update_modified_at
|
11
10
|
end
|
12
11
|
end
|
13
12
|
|
@@ -16,7 +15,7 @@ module Mongoid
|
|
16
15
|
# Update the created_at field on the Document to the current time. This is
|
17
16
|
# only called on create.
|
18
17
|
def update_created_at
|
19
|
-
self.created_at = Time.now.utc
|
18
|
+
self.created_at = Time.now.utc if !created_at
|
20
19
|
end
|
21
20
|
|
22
21
|
# Update the last_modified field on the Document to the current time.
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Mongoid #:nodoc:
|
2
|
+
# Include this module to get automatic versioning of root level documents.
|
3
|
+
# This will add a version field to the +Document+ and a has_many association
|
4
|
+
# with all the versions contained in it.
|
5
|
+
module Versioning
|
6
|
+
def self.included(base)
|
7
|
+
base.class_eval do
|
8
|
+
field :version, :type => Integer, :default => 1
|
9
|
+
has_many :versions, :class_name => self.name
|
10
|
+
before_save :revise
|
11
|
+
include InstanceMethods
|
12
|
+
end
|
13
|
+
end
|
14
|
+
module InstanceMethods
|
15
|
+
# Create a new version of the +Document+. This will load the previous
|
16
|
+
# document from the database and set it as the next version before saving
|
17
|
+
# the current document. It then increments the version number.
|
18
|
+
def revise
|
19
|
+
last_version = self.class.find(id)
|
20
|
+
if last_version
|
21
|
+
self.versions << last_version
|
22
|
+
self.version = version + 1
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/mongoid.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid}
|
8
|
-
s.version = "0.6.
|
8
|
+
s.version = "0.6.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Durran Jordan"]
|
12
|
-
s.date = %q{2009-
|
12
|
+
s.date = %q{2009-11-01}
|
13
13
|
s.email = %q{durran@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.textile"
|
@@ -56,6 +56,7 @@ Gem::Specification.new do |s|
|
|
56
56
|
"lib/mongoid/extensions/time/conversions.rb",
|
57
57
|
"lib/mongoid/field.rb",
|
58
58
|
"lib/mongoid/timestamps.rb",
|
59
|
+
"lib/mongoid/versioning.rb",
|
59
60
|
"mongoid.gemspec",
|
60
61
|
"spec/integration/mongoid/document_spec.rb",
|
61
62
|
"spec/spec.opts",
|
@@ -91,7 +92,8 @@ Gem::Specification.new do |s|
|
|
91
92
|
"spec/unit/mongoid/extensions/symbol/inflections_spec.rb",
|
92
93
|
"spec/unit/mongoid/extensions/time/conversions_spec.rb",
|
93
94
|
"spec/unit/mongoid/field_spec.rb",
|
94
|
-
"spec/unit/mongoid/timestamps_spec.rb"
|
95
|
+
"spec/unit/mongoid/timestamps_spec.rb",
|
96
|
+
"spec/unit/mongoid/versioning_spec.rb"
|
95
97
|
]
|
96
98
|
s.homepage = %q{http://github.com/durran/mongoid}
|
97
99
|
s.rdoc_options = ["--charset=UTF-8"]
|
@@ -132,7 +134,8 @@ Gem::Specification.new do |s|
|
|
132
134
|
"spec/unit/mongoid/extensions/symbol/inflections_spec.rb",
|
133
135
|
"spec/unit/mongoid/extensions/time/conversions_spec.rb",
|
134
136
|
"spec/unit/mongoid/field_spec.rb",
|
135
|
-
"spec/unit/mongoid/timestamps_spec.rb"
|
137
|
+
"spec/unit/mongoid/timestamps_spec.rb",
|
138
|
+
"spec/unit/mongoid/versioning_spec.rb"
|
136
139
|
]
|
137
140
|
|
138
141
|
if s.respond_to? :specification_version then
|
@@ -1,5 +1,36 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
2
|
|
3
|
+
class Person < Mongoid::Document
|
4
|
+
include Mongoid::Timestamps
|
5
|
+
field :title
|
6
|
+
field :terms, :type => Boolean
|
7
|
+
field :age, :type => Integer, :default => 100
|
8
|
+
field :dob, :type => Date
|
9
|
+
has_many :addresses
|
10
|
+
has_one :name
|
11
|
+
end
|
12
|
+
|
13
|
+
class Address < Mongoid::Document
|
14
|
+
field :street
|
15
|
+
field :city
|
16
|
+
field :state
|
17
|
+
field :comment_code
|
18
|
+
key :street
|
19
|
+
belongs_to :person
|
20
|
+
end
|
21
|
+
|
22
|
+
class Name < Mongoid::Document
|
23
|
+
field :first_name
|
24
|
+
field :last_name
|
25
|
+
key :first_name, :last_name
|
26
|
+
belongs_to :person
|
27
|
+
end
|
28
|
+
|
29
|
+
class Comment < Mongoid::Document
|
30
|
+
include Mongoid::Versioning
|
31
|
+
field :text
|
32
|
+
end
|
33
|
+
|
3
34
|
describe Mongoid::Document do
|
4
35
|
|
5
36
|
before do
|
@@ -199,4 +230,21 @@ describe Mongoid::Document do
|
|
199
230
|
|
200
231
|
end
|
201
232
|
|
233
|
+
context "versioning" do
|
234
|
+
|
235
|
+
before do
|
236
|
+
@comment = Comment.new(:text => "Testing")
|
237
|
+
@comment.save
|
238
|
+
end
|
239
|
+
|
240
|
+
it "versions on save" do
|
241
|
+
@from_db = Comment.find(@comment.id)
|
242
|
+
@from_db.text = "New"
|
243
|
+
@from_db.save
|
244
|
+
@from_db.versions.size.should == 1
|
245
|
+
@from_db.version.should == 2
|
246
|
+
end
|
247
|
+
|
248
|
+
end
|
249
|
+
|
202
250
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -14,63 +14,3 @@ Spec::Runner.configure do |config|
|
|
14
14
|
config.mock_with :mocha
|
15
15
|
Mocha::Configuration.prevent(:stubbing_non_existent_method)
|
16
16
|
end
|
17
|
-
|
18
|
-
class MixedDrink < Mongoid::Document
|
19
|
-
field :name
|
20
|
-
end
|
21
|
-
|
22
|
-
class Person < Mongoid::Document
|
23
|
-
include Mongoid::Timestamps
|
24
|
-
field :title
|
25
|
-
field :terms, :type => Boolean
|
26
|
-
field :age, :type => Integer, :default => 100
|
27
|
-
field :dob, :type => Date
|
28
|
-
field :mixed_drink, :type => MixedDrink
|
29
|
-
has_many :addresses
|
30
|
-
has_many :phone_numbers, :class_name => "Phone"
|
31
|
-
has_one :name
|
32
|
-
has_one :pet, :class_name => "Animal"
|
33
|
-
end
|
34
|
-
|
35
|
-
class Address < Mongoid::Document
|
36
|
-
field :street
|
37
|
-
field :city
|
38
|
-
field :state
|
39
|
-
field :post_code
|
40
|
-
key :street
|
41
|
-
belongs_to :person
|
42
|
-
end
|
43
|
-
|
44
|
-
class Phone < Mongoid::Document
|
45
|
-
field :number
|
46
|
-
key :number
|
47
|
-
belongs_to :person
|
48
|
-
has_one :country_code
|
49
|
-
end
|
50
|
-
|
51
|
-
class CountryCode < Mongoid::Document
|
52
|
-
field :code, :type => Integer
|
53
|
-
key :code
|
54
|
-
belongs_to :phone_number
|
55
|
-
end
|
56
|
-
|
57
|
-
class Animal < Mongoid::Document
|
58
|
-
field :name
|
59
|
-
key :name
|
60
|
-
belongs_to :person
|
61
|
-
end
|
62
|
-
|
63
|
-
class Name < Mongoid::Document
|
64
|
-
field :first_name
|
65
|
-
field :last_name
|
66
|
-
key :first_name, :last_name
|
67
|
-
belongs_to :person
|
68
|
-
end
|
69
|
-
|
70
|
-
class Decorated
|
71
|
-
include Mongoid::Associations::Decorator
|
72
|
-
|
73
|
-
def initialize(doc)
|
74
|
-
@document = doc
|
75
|
-
end
|
76
|
-
end
|
@@ -1,5 +1,12 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
2
|
|
3
|
+
class Name < Mongoid::Document
|
4
|
+
field :first_name
|
5
|
+
field :last_name
|
6
|
+
key :first_name, :last_name
|
7
|
+
belongs_to :person
|
8
|
+
end
|
9
|
+
|
3
10
|
describe Mongoid::Associations::BelongsToAssociation do
|
4
11
|
|
5
12
|
before do
|
@@ -1,5 +1,34 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
2
|
|
3
|
+
class Person < Mongoid::Document
|
4
|
+
field :title
|
5
|
+
field :terms, :type => Boolean
|
6
|
+
field :age, :type => Integer, :default => 100
|
7
|
+
field :dob, :type => Date
|
8
|
+
has_many :addresses
|
9
|
+
has_one :name
|
10
|
+
end
|
11
|
+
|
12
|
+
class Address < Mongoid::Document
|
13
|
+
field :street
|
14
|
+
key :street
|
15
|
+
belongs_to :person
|
16
|
+
end
|
17
|
+
|
18
|
+
class Name < Mongoid::Document
|
19
|
+
field :first_name
|
20
|
+
field :last_name
|
21
|
+
key :first_name, :last_name
|
22
|
+
belongs_to :person
|
23
|
+
end
|
24
|
+
|
25
|
+
class Decorated
|
26
|
+
include Mongoid::Associations::Decorator
|
27
|
+
def initialize(doc)
|
28
|
+
@document = doc
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
3
32
|
describe Mongoid::Associations::Decorator do
|
4
33
|
|
5
34
|
describe "#included" do
|
@@ -1,5 +1,24 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
2
|
|
3
|
+
class Person < Mongoid::Document
|
4
|
+
field :title
|
5
|
+
has_many :addresses
|
6
|
+
has_one :name
|
7
|
+
end
|
8
|
+
|
9
|
+
class Address < Mongoid::Document
|
10
|
+
field :street
|
11
|
+
key :street
|
12
|
+
belongs_to :person
|
13
|
+
end
|
14
|
+
|
15
|
+
class Name < Mongoid::Document
|
16
|
+
field :first_name
|
17
|
+
field :last_name
|
18
|
+
key :first_name, :last_name
|
19
|
+
belongs_to :person
|
20
|
+
end
|
21
|
+
|
3
22
|
describe Mongoid::Associations::Factory do
|
4
23
|
|
5
24
|
describe "#create" do
|
@@ -1,5 +1,16 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
2
|
|
3
|
+
class Person < Mongoid::Document
|
4
|
+
field :title
|
5
|
+
has_many :addresses
|
6
|
+
end
|
7
|
+
|
8
|
+
class Address < Mongoid::Document
|
9
|
+
field :street
|
10
|
+
key :street
|
11
|
+
belongs_to :person
|
12
|
+
end
|
13
|
+
|
3
14
|
describe Mongoid::Associations::HasManyAssociation do
|
4
15
|
|
5
16
|
before do
|
@@ -1,5 +1,48 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
2
|
|
3
|
+
class Person < Mongoid::Document
|
4
|
+
field :title
|
5
|
+
has_many :addresses
|
6
|
+
has_many :phone_numbers, :class_name => "Phone"
|
7
|
+
has_one :name
|
8
|
+
has_one :pet, :class_name => "Animal"
|
9
|
+
end
|
10
|
+
|
11
|
+
class Animal < Mongoid::Document
|
12
|
+
field :name
|
13
|
+
key :name
|
14
|
+
belongs_to :person
|
15
|
+
end
|
16
|
+
|
17
|
+
class CountryCode < Mongoid::Document
|
18
|
+
field :code, :type => Integer
|
19
|
+
key :code
|
20
|
+
belongs_to :phone_number
|
21
|
+
end
|
22
|
+
|
23
|
+
class Address < Mongoid::Document
|
24
|
+
field :street
|
25
|
+
field :city
|
26
|
+
field :state
|
27
|
+
field :post_code
|
28
|
+
key :street
|
29
|
+
belongs_to :person
|
30
|
+
end
|
31
|
+
|
32
|
+
class Name < Mongoid::Document
|
33
|
+
field :first_name
|
34
|
+
field :last_name
|
35
|
+
key :first_name, :last_name
|
36
|
+
belongs_to :person
|
37
|
+
end
|
38
|
+
|
39
|
+
class Phone < Mongoid::Document
|
40
|
+
field :number
|
41
|
+
key :number
|
42
|
+
belongs_to :person
|
43
|
+
has_one :country_code
|
44
|
+
end
|
45
|
+
|
3
46
|
describe Mongoid::Associations do
|
4
47
|
|
5
48
|
before do
|
@@ -1,5 +1,10 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
2
|
|
3
|
+
class Person < Mongoid::Document
|
4
|
+
field :terms, :type => Boolean
|
5
|
+
field :age, :type => Integer, :default => 100
|
6
|
+
end
|
7
|
+
|
3
8
|
describe Mongoid::Attributes do
|
4
9
|
|
5
10
|
describe "#process" do
|
@@ -22,7 +27,7 @@ describe Mongoid::Attributes do
|
|
22
27
|
}
|
23
28
|
end
|
24
29
|
|
25
|
-
it "returns a properly cast
|
30
|
+
it "returns a properly cast the attributes" do
|
26
31
|
attrs = Person.new(@attributes).attributes
|
27
32
|
attrs[:age].should == 30
|
28
33
|
attrs[:terms].should == true
|
@@ -1,5 +1,9 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
2
|
|
3
|
+
class Person < Mongoid::Document
|
4
|
+
field :title
|
5
|
+
end
|
6
|
+
|
3
7
|
describe Mongoid::Criteria do
|
4
8
|
|
5
9
|
before do
|
@@ -90,12 +94,34 @@ describe Mongoid::Criteria do
|
|
90
94
|
|
91
95
|
context "when type is :first" do
|
92
96
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
97
|
+
context "when documents exist" do
|
98
|
+
|
99
|
+
before do
|
100
|
+
@collection = mock
|
101
|
+
Person.expects(:collection).returns(@collection)
|
102
|
+
@collection.expects(:find_one).with(@criteria.selector, @criteria.options).returns({ :title => "Sir" })
|
103
|
+
end
|
104
|
+
|
105
|
+
it "calls find on the collection with the selector and options" do
|
106
|
+
criteria = Mongoid::Criteria.new(:first)
|
107
|
+
criteria.execute(Person).should be_a_kind_of(Person)
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
context "when no documents exist" do
|
113
|
+
|
114
|
+
before do
|
115
|
+
@collection = mock
|
116
|
+
Person.expects(:collection).returns(@collection)
|
117
|
+
@collection.expects(:find_one).with(@criteria.selector, @criteria.options).returns(nil)
|
118
|
+
end
|
119
|
+
|
120
|
+
it "returns nil" do
|
121
|
+
criteria = Mongoid::Criteria.new(:first)
|
122
|
+
criteria.execute(Person).should be_nil
|
123
|
+
end
|
124
|
+
|
99
125
|
end
|
100
126
|
|
101
127
|
end
|
@@ -1,5 +1,31 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
2
|
|
3
|
+
class MixedDrink < Mongoid::Document
|
4
|
+
field :name
|
5
|
+
end
|
6
|
+
|
7
|
+
class Person < Mongoid::Document
|
8
|
+
field :title
|
9
|
+
field :terms, :type => Boolean
|
10
|
+
field :age, :type => Integer, :default => 100
|
11
|
+
field :mixed_drink, :type => MixedDrink
|
12
|
+
has_many :addresses
|
13
|
+
has_one :name
|
14
|
+
end
|
15
|
+
|
16
|
+
class Name < Mongoid::Document
|
17
|
+
field :first_name
|
18
|
+
field :last_name
|
19
|
+
key :first_name, :last_name
|
20
|
+
belongs_to :person
|
21
|
+
end
|
22
|
+
|
23
|
+
class Address < Mongoid::Document
|
24
|
+
field :street
|
25
|
+
key :street
|
26
|
+
belongs_to :person
|
27
|
+
end
|
28
|
+
|
3
29
|
describe Mongoid::Document do
|
4
30
|
|
5
31
|
before do
|
@@ -237,7 +263,7 @@ describe Mongoid::Document do
|
|
237
263
|
describe "#first" do
|
238
264
|
|
239
265
|
before do
|
240
|
-
@attributes =
|
266
|
+
@attributes = { "age" => 100, "version" => 1 }
|
241
267
|
end
|
242
268
|
|
243
269
|
context "when a selector is provided" do
|
@@ -1,5 +1,10 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb")
|
2
2
|
|
3
|
+
class Person < Mongoid::Document
|
4
|
+
field :title
|
5
|
+
field :age, :type => Integer, :default => 100
|
6
|
+
end
|
7
|
+
|
3
8
|
describe Mongoid::Extensions::Array::Conversions do
|
4
9
|
|
5
10
|
describe "#mongoidize" do
|
@@ -1,5 +1,10 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb")
|
2
2
|
|
3
|
+
class Person < Mongoid::Document
|
4
|
+
field :title
|
5
|
+
field :age, :type => Integer, :default => 100
|
6
|
+
end
|
7
|
+
|
3
8
|
describe Mongoid::Extensions::Object::Conversions do
|
4
9
|
|
5
10
|
describe "#mongoidize" do
|
@@ -1,5 +1,17 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb")
|
2
2
|
|
3
|
+
class Person < Mongoid::Document
|
4
|
+
field :title
|
5
|
+
has_one :name
|
6
|
+
end
|
7
|
+
|
8
|
+
class Name < Mongoid::Document
|
9
|
+
field :first_name
|
10
|
+
field :last_name
|
11
|
+
key :first_name, :last_name
|
12
|
+
belongs_to :person
|
13
|
+
end
|
14
|
+
|
3
15
|
describe Mongoid::Extensions::Object::Parentization do
|
4
16
|
|
5
17
|
describe "#parentize" do
|
@@ -1,5 +1,9 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
2
|
|
3
|
+
class Person < Mongoid::Document
|
4
|
+
include Mongoid::Timestamps
|
5
|
+
end
|
6
|
+
|
3
7
|
describe Mongoid::Timestamps do
|
4
8
|
|
5
9
|
describe "#included" do
|
@@ -15,7 +19,7 @@ describe Mongoid::Timestamps do
|
|
15
19
|
end
|
16
20
|
|
17
21
|
it "forces the timestamps to UTC" do
|
18
|
-
@person.run_callbacks(:
|
22
|
+
@person.run_callbacks(:before_save)
|
19
23
|
@person.created_at.should be_close(Time.now.utc, 10.seconds)
|
20
24
|
@person.modified_at.should be_close(Time.now.utc, 10.seconds)
|
21
25
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
|
+
|
3
|
+
class Post < Mongoid::Document
|
4
|
+
include Mongoid::Versioning
|
5
|
+
field :title
|
6
|
+
end
|
7
|
+
|
8
|
+
describe Mongoid::Versioning do
|
9
|
+
|
10
|
+
describe "#version" do
|
11
|
+
|
12
|
+
before do
|
13
|
+
@post = Post.new
|
14
|
+
end
|
15
|
+
|
16
|
+
it "defaults to 1" do
|
17
|
+
@post.version.should == 1
|
18
|
+
end
|
19
|
+
|
20
|
+
context "when document is saved" do
|
21
|
+
|
22
|
+
before do
|
23
|
+
@post.title = "New"
|
24
|
+
@version = Post.new(:title => "Test")
|
25
|
+
Post.expects(:find).at_least(1).with(@post.id).returns(@version)
|
26
|
+
@post.revise
|
27
|
+
end
|
28
|
+
|
29
|
+
it "increments the version" do
|
30
|
+
@post.version.should == 2
|
31
|
+
end
|
32
|
+
|
33
|
+
it "adds a snapshot of the document to the versions" do
|
34
|
+
@post.title.should == "New"
|
35
|
+
@post.version.should == 2
|
36
|
+
@post.versions.size.should == 1
|
37
|
+
version = @post.versions.first
|
38
|
+
version.title.should == "Test"
|
39
|
+
version.version.should == 1
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Durran Jordan
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-11-01 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -112,6 +112,7 @@ files:
|
|
112
112
|
- lib/mongoid/extensions/time/conversions.rb
|
113
113
|
- lib/mongoid/field.rb
|
114
114
|
- lib/mongoid/timestamps.rb
|
115
|
+
- lib/mongoid/versioning.rb
|
115
116
|
- mongoid.gemspec
|
116
117
|
- spec/integration/mongoid/document_spec.rb
|
117
118
|
- spec/spec.opts
|
@@ -148,6 +149,7 @@ files:
|
|
148
149
|
- spec/unit/mongoid/extensions/time/conversions_spec.rb
|
149
150
|
- spec/unit/mongoid/field_spec.rb
|
150
151
|
- spec/unit/mongoid/timestamps_spec.rb
|
152
|
+
- spec/unit/mongoid/versioning_spec.rb
|
151
153
|
has_rdoc: true
|
152
154
|
homepage: http://github.com/durran/mongoid
|
153
155
|
licenses: []
|
@@ -211,3 +213,4 @@ test_files:
|
|
211
213
|
- spec/unit/mongoid/extensions/time/conversions_spec.rb
|
212
214
|
- spec/unit/mongoid/field_spec.rb
|
213
215
|
- spec/unit/mongoid/timestamps_spec.rb
|
216
|
+
- spec/unit/mongoid/versioning_spec.rb
|