djsun-mongomapper 0.3.5.5 → 0.4.1.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.rdoc +38 -38
- data/Rakefile +87 -73
- data/VERSION +1 -1
- data/lib/mongomapper.rb +67 -71
- data/lib/mongomapper/associations.rb +86 -84
- data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +34 -34
- data/lib/mongomapper/associations/many_embedded_proxy.rb +67 -17
- data/lib/mongomapper/associations/proxy.rb +74 -73
- data/lib/mongomapper/document.rb +342 -348
- data/lib/mongomapper/embedded_document.rb +354 -274
- data/lib/mongomapper/finder_options.rb +84 -84
- data/lib/mongomapper/key.rb +32 -76
- data/lib/mongomapper/rails_compatibility/document.rb +14 -14
- data/lib/mongomapper/rails_compatibility/embedded_document.rb +26 -24
- data/lib/mongomapper/support.rb +156 -29
- data/lib/mongomapper/validations.rb +69 -47
- data/test/custom_matchers.rb +48 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +53 -56
- data/test/functional/associations/test_belongs_to_proxy.rb +48 -49
- data/test/functional/associations/test_many_documents_as_proxy.rb +208 -253
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +130 -130
- data/test/functional/associations/test_many_embedded_proxy.rb +168 -106
- data/test/functional/associations/test_many_polymorphic_proxy.rb +261 -262
- data/test/functional/test_binary.rb +21 -0
- data/test/functional/test_document.rb +946 -952
- data/test/functional/test_embedded_document.rb +98 -0
- data/test/functional/test_pagination.rb +87 -80
- data/test/functional/test_rails_compatibility.rb +29 -29
- data/test/functional/test_validations.rb +262 -172
- data/test/models.rb +169 -169
- data/test/test_helper.rb +28 -66
- data/test/unit/serializers/test_json_serializer.rb +193 -193
- data/test/unit/test_document.rb +161 -123
- data/test/unit/test_embedded_document.rb +643 -547
- data/test/unit/test_finder_options.rb +183 -183
- data/test/unit/test_key.rb +175 -247
- data/test/unit/test_rails_compatibility.rb +38 -33
- data/test/unit/test_serializations.rb +52 -52
- data/test/unit/test_support.rb +268 -0
- data/test/unit/test_time_zones.rb +40 -0
- data/test/unit/test_validations.rb +499 -258
- metadata +22 -12
- data/History +0 -76
- data/mongomapper.gemspec +0 -145
@@ -1,47 +1,69 @@
|
|
1
|
-
module MongoMapper
|
2
|
-
module Validations
|
3
|
-
|
4
|
-
def
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
1
|
+
module MongoMapper
|
2
|
+
module Validations
|
3
|
+
module Macros
|
4
|
+
def validates_uniqueness_of(*args)
|
5
|
+
add_validations(args, MongoMapper::Validations::ValidatesUniquenessOf)
|
6
|
+
end
|
7
|
+
|
8
|
+
def validates_exclusion_of(*args)
|
9
|
+
add_validations(args, MongoMapper::Validations::ValidatesExclusionOf)
|
10
|
+
end
|
11
|
+
|
12
|
+
def validates_inclusion_of(*args)
|
13
|
+
add_validations(args, MongoMapper::Validations::ValidatesInclusionOf)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class ValidatesUniquenessOf < Validatable::ValidationBase
|
18
|
+
option :scope
|
19
|
+
|
20
|
+
def valid?(instance)
|
21
|
+
doc = instance.class.find(:first, :conditions => {self.attribute => instance[attribute]}.merge(scope_conditions(instance)), :limit => 1)
|
22
|
+
doc.nil? || instance.id == doc.id
|
23
|
+
end
|
24
|
+
|
25
|
+
def message(instance)
|
26
|
+
super || "has already been taken"
|
27
|
+
end
|
28
|
+
|
29
|
+
def scope_conditions(instance)
|
30
|
+
return {} unless scope
|
31
|
+
Array(scope).inject({}) do |conditions, key|
|
32
|
+
conditions.merge(key => instance[key])
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class ValidatesExclusionOf < Validatable::ValidationBase
|
38
|
+
required_option :within
|
39
|
+
|
40
|
+
def valid?(instance)
|
41
|
+
value = instance[attribute]
|
42
|
+
return true if allow_nil && value.nil?
|
43
|
+
return true if allow_blank && value.blank?
|
44
|
+
|
45
|
+
!within.include?(instance[attribute])
|
46
|
+
end
|
47
|
+
|
48
|
+
def message(instance)
|
49
|
+
super || "is reserved"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class ValidatesInclusionOf < Validatable::ValidationBase
|
54
|
+
required_option :within
|
55
|
+
|
56
|
+
def valid?(instance)
|
57
|
+
value = instance[attribute]
|
58
|
+
return true if allow_nil && value.nil?
|
59
|
+
return true if allow_blank && value.blank?
|
60
|
+
|
61
|
+
within.include?(value)
|
62
|
+
end
|
63
|
+
|
64
|
+
def message(instance)
|
65
|
+
super || "is not in the list"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module CustomMatchers
|
2
|
+
custom_matcher :be_nil do |receiver, matcher, args|
|
3
|
+
matcher.positive_failure_message = "Expected #{receiver} to be nil but it wasn't"
|
4
|
+
matcher.negative_failure_message = "Expected #{receiver} not to be nil but it was"
|
5
|
+
receiver.nil?
|
6
|
+
end
|
7
|
+
|
8
|
+
custom_matcher :be_blank do |receiver, matcher, args|
|
9
|
+
matcher.positive_failure_message = "Expected #{receiver} to be blank but it wasn't"
|
10
|
+
matcher.negative_failure_message = "Expected #{receiver} not to be blank but it was"
|
11
|
+
receiver.blank?
|
12
|
+
end
|
13
|
+
|
14
|
+
custom_matcher :be_true do |receiver, matcher, args|
|
15
|
+
matcher.positive_failure_message = "Expected #{receiver} to be true but it wasn't"
|
16
|
+
matcher.negative_failure_message = "Expected #{receiver} not to be true but it was"
|
17
|
+
receiver.eql?(true)
|
18
|
+
end
|
19
|
+
|
20
|
+
custom_matcher :be_false do |receiver, matcher, args|
|
21
|
+
matcher.positive_failure_message = "Expected #{receiver} to be false but it wasn't"
|
22
|
+
matcher.negative_failure_message = "Expected #{receiver} not to be false but it was"
|
23
|
+
receiver.eql?(false)
|
24
|
+
end
|
25
|
+
|
26
|
+
custom_matcher :be_valid do |receiver, matcher, args|
|
27
|
+
matcher.positive_failure_message = "Expected to be valid but it was invalid #{receiver.errors.inspect}"
|
28
|
+
matcher.negative_failure_message = "Expected to be invalid but it was valid #{receiver.errors.inspect}"
|
29
|
+
receiver.valid?
|
30
|
+
end
|
31
|
+
|
32
|
+
custom_matcher :have_error_on do |receiver, matcher, args|
|
33
|
+
receiver.valid?
|
34
|
+
attribute = args[0]
|
35
|
+
expected_message = args[1]
|
36
|
+
|
37
|
+
if expected_message.nil?
|
38
|
+
matcher.positive_failure_message = "#{receiver} had no errors on #{attribute}"
|
39
|
+
matcher.negative_failure_message = "#{receiver} had errors on #{attribute} #{receiver.errors.inspect}"
|
40
|
+
!receiver.errors.on(attribute).blank?
|
41
|
+
else
|
42
|
+
actual = receiver.errors.on(attribute)
|
43
|
+
matcher.positive_failure_message = %Q(Expected error on #{attribute} to be "#{expected_message}" but was "#{actual}")
|
44
|
+
matcher.negative_failure_message = %Q(Expected error on #{attribute} not to be "#{expected_message}" but was "#{actual}")
|
45
|
+
actual == expected_message
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -1,57 +1,54 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'models'
|
3
|
-
|
4
|
-
class BelongsToPolymorphicProxyTest < Test::Unit::TestCase
|
5
|
-
def setup
|
6
|
-
clear_all_collections
|
7
|
-
end
|
8
|
-
|
9
|
-
should "default to nil" do
|
10
|
-
status = Status.new
|
11
|
-
status.target.should
|
12
|
-
status.target.inspect.should == "nil"
|
13
|
-
end
|
14
|
-
|
15
|
-
should "be able to replace the association" do
|
16
|
-
status = Status.new
|
17
|
-
project = Project.new(:name => "mongomapper")
|
18
|
-
status.target = project
|
19
|
-
status.save.should be_true
|
20
|
-
|
21
|
-
from_db = Status.find(status.id)
|
22
|
-
from_db.target.
|
23
|
-
from_db.
|
24
|
-
from_db.
|
25
|
-
from_db.
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
status.
|
33
|
-
|
34
|
-
|
35
|
-
from_db =
|
36
|
-
from_db.
|
37
|
-
from_db.
|
38
|
-
from_db.
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
@status =
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
@status.target.inspect.should == "nil"
|
55
|
-
end
|
56
|
-
end
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class BelongsToPolymorphicProxyTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
clear_all_collections
|
7
|
+
end
|
8
|
+
|
9
|
+
should "default to nil" do
|
10
|
+
status = Status.new
|
11
|
+
status.target.nil?.should == true
|
12
|
+
status.target.inspect.should == "nil"
|
13
|
+
end
|
14
|
+
|
15
|
+
should "be able to replace the association" do
|
16
|
+
status = Status.new
|
17
|
+
project = Project.new(:name => "mongomapper")
|
18
|
+
status.target = project
|
19
|
+
status.save.should be_true
|
20
|
+
|
21
|
+
from_db = Status.find(status.id)
|
22
|
+
from_db.target.nil?.should == false
|
23
|
+
from_db.target_id.should == project.id
|
24
|
+
from_db.target_type.should == "Project"
|
25
|
+
from_db.target.name.should == "mongomapper"
|
26
|
+
end
|
27
|
+
|
28
|
+
should "unset the association" do
|
29
|
+
status = Status.new
|
30
|
+
project = Project.new(:name => "mongomapper")
|
31
|
+
status.target = project
|
32
|
+
status.save.should be_true
|
33
|
+
|
34
|
+
from_db = Status.find(status.id)
|
35
|
+
from_db.target = nil
|
36
|
+
from_db.target_type.nil?.should == true
|
37
|
+
from_db.target_id.nil?.should == true
|
38
|
+
from_db.target.nil?.should == true
|
39
|
+
end
|
40
|
+
|
41
|
+
context "association id set but document not found" do
|
42
|
+
setup do
|
43
|
+
@status = Status.new
|
44
|
+
project = Project.new(:name => "mongomapper")
|
45
|
+
@status.target = project
|
46
|
+
@status.save.should be_true
|
47
|
+
project.destroy
|
48
|
+
end
|
49
|
+
|
50
|
+
should "return nil instead of raising error" do
|
51
|
+
@status.target.nil?.should == true
|
52
|
+
end
|
53
|
+
end
|
57
54
|
end
|
@@ -1,49 +1,48 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'models'
|
3
|
-
|
4
|
-
class BelongsToProxyTest < Test::Unit::TestCase
|
5
|
-
def setup
|
6
|
-
clear_all_collections
|
7
|
-
end
|
8
|
-
|
9
|
-
should "default to nil" do
|
10
|
-
status = Status.new
|
11
|
-
status.project.should
|
12
|
-
status.project.inspect.should == "nil"
|
13
|
-
end
|
14
|
-
|
15
|
-
should "be able to replace the association" do
|
16
|
-
status = Status.new
|
17
|
-
project = Project.new(:name => "mongomapper")
|
18
|
-
status.project = project
|
19
|
-
status.save.should be_true
|
20
|
-
|
21
|
-
from_db = Status.find(status.id)
|
22
|
-
from_db.project.should_not be_nil
|
23
|
-
from_db.project.
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
project =
|
30
|
-
status.
|
31
|
-
|
32
|
-
|
33
|
-
from_db =
|
34
|
-
from_db.project
|
35
|
-
from_db.project.should
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
@status.project.should
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
end
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class BelongsToProxyTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
clear_all_collections
|
7
|
+
end
|
8
|
+
|
9
|
+
should "default to nil" do
|
10
|
+
status = Status.new
|
11
|
+
status.project.nil?.should == true
|
12
|
+
status.project.inspect.should == "nil"
|
13
|
+
end
|
14
|
+
|
15
|
+
should "be able to replace the association" do
|
16
|
+
status = Status.new
|
17
|
+
project = Project.new(:name => "mongomapper")
|
18
|
+
status.project = project
|
19
|
+
status.save.should be_true
|
20
|
+
|
21
|
+
from_db = Status.find(status.id)
|
22
|
+
from_db.project.should_not be_nil
|
23
|
+
from_db.project.name.should == "mongomapper"
|
24
|
+
end
|
25
|
+
|
26
|
+
should "unset the association" do
|
27
|
+
status = Status.new
|
28
|
+
project = Project.new(:name => "mongomapper")
|
29
|
+
status.project = project
|
30
|
+
status.save.should be_true
|
31
|
+
|
32
|
+
from_db = Status.find(status.id)
|
33
|
+
from_db.project = nil
|
34
|
+
from_db.project.nil?.should == true
|
35
|
+
from_db.project.inspect.should == "nil"
|
36
|
+
end
|
37
|
+
|
38
|
+
context "association id set but document not found" do
|
39
|
+
setup do
|
40
|
+
@status = Status.new(:name => 'Foo', :project_id => '1234')
|
41
|
+
end
|
42
|
+
|
43
|
+
should "return nil instead of raising error" do
|
44
|
+
@status.project.nil?.should == true
|
45
|
+
@status.project.inspect.should == "nil"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -1,254 +1,209 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'models'
|
3
|
-
|
4
|
-
class ManyDocumentsAsProxyTest < Test::Unit::TestCase
|
5
|
-
def setup
|
6
|
-
clear_all_collections
|
7
|
-
end
|
8
|
-
|
9
|
-
should "default reader to empty array" do
|
10
|
-
Post.new.comments.should == []
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
PostComment.keys.keys.should include('
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
post
|
21
|
-
post.comments << PostComment.new(:body => '
|
22
|
-
post.comments
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
PostComment.new(:body => '
|
34
|
-
PostComment.new(:body => '
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
from_db
|
40
|
-
from_db.comments.
|
41
|
-
from_db.comments[
|
42
|
-
from_db.comments[
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
comment
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
comment
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
comment
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
comment
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
comment
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
comment
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
post
|
107
|
-
post.comments.create(:body => '
|
108
|
-
post.comments.create(:body => '
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
@
|
120
|
-
@
|
121
|
-
@
|
122
|
-
@post.
|
123
|
-
|
124
|
-
|
125
|
-
@
|
126
|
-
@
|
127
|
-
@
|
128
|
-
@
|
129
|
-
@post2.
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
@post.comments.find(:all).should include(@
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
comments
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
comments
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
comments
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
comments
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
context "with
|
178
|
-
should "work" do
|
179
|
-
@post.comments.
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
context "with one id" do
|
211
|
-
should "work for id in association" do
|
212
|
-
@post.comments.find(@comment2.id).should == @comment2
|
213
|
-
end
|
214
|
-
|
215
|
-
should "not work for id not in association" do
|
216
|
-
lambda {
|
217
|
-
@post.comments.find(@comment5.id)
|
218
|
-
}.should raise_error(MongoMapper::DocumentNotFound)
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
context "with multiple ids" do
|
223
|
-
should "work for ids in association" do
|
224
|
-
posts = @post.comments.find(@comment1.id, @comment2.id)
|
225
|
-
posts.should == [@comment1, @comment2]
|
226
|
-
end
|
227
|
-
|
228
|
-
should "not work for ids not in association" do
|
229
|
-
lambda {
|
230
|
-
@post.comments.find(@comment1.id, @comment2.id, @comment4.id)
|
231
|
-
}.should raise_error(MongoMapper::DocumentNotFound)
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
context "with #paginate" do
|
236
|
-
setup do
|
237
|
-
@comments = @post2.comments.paginate(:per_page => 2, :page => 1, :order => 'created_at asc')
|
238
|
-
end
|
239
|
-
|
240
|
-
should "return total pages" do
|
241
|
-
@comments.total_pages.should == 2
|
242
|
-
end
|
243
|
-
|
244
|
-
should "return total entries" do
|
245
|
-
@comments.total_entries.should == 3
|
246
|
-
end
|
247
|
-
|
248
|
-
should "return the subject" do
|
249
|
-
@comments.should include(@comment4)
|
250
|
-
@comments.should include(@comment5)
|
251
|
-
end
|
252
|
-
end
|
253
|
-
end
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class ManyDocumentsAsProxyTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
clear_all_collections
|
7
|
+
end
|
8
|
+
|
9
|
+
should "default reader to empty array" do
|
10
|
+
Post.new.comments.should == []
|
11
|
+
end
|
12
|
+
|
13
|
+
should "add type and id key to polymorphic class base" do
|
14
|
+
PostComment.keys.keys.should include('commentable_type')
|
15
|
+
PostComment.keys.keys.should include('commentable_id')
|
16
|
+
end
|
17
|
+
|
18
|
+
should "allow adding to association like it was an array" do
|
19
|
+
post = Post.new
|
20
|
+
post.comments << PostComment.new(:body => 'foo bar')
|
21
|
+
post.comments << PostComment.new(:body => 'baz')
|
22
|
+
post.comments.concat PostComment.new(:body => 'baz')
|
23
|
+
|
24
|
+
post.comments.size.should == 3
|
25
|
+
end
|
26
|
+
|
27
|
+
should "be able to replace the association" do
|
28
|
+
post = Post.new
|
29
|
+
|
30
|
+
lambda {
|
31
|
+
post.comments = [
|
32
|
+
PostComment.new(:body => 'foo'),
|
33
|
+
PostComment.new(:body => 'bar'),
|
34
|
+
PostComment.new(:body => 'baz')
|
35
|
+
]
|
36
|
+
}.should change { PostComment.count }.by(3)
|
37
|
+
|
38
|
+
from_db = Post.find(post.id)
|
39
|
+
from_db.comments.size.should == 3
|
40
|
+
from_db.comments[0].body.should == 'foo'
|
41
|
+
from_db.comments[1].body.should == 'bar'
|
42
|
+
from_db.comments[2].body.should == 'baz'
|
43
|
+
end
|
44
|
+
|
45
|
+
context "build" do
|
46
|
+
should "assign foreign key" do
|
47
|
+
post = Post.new
|
48
|
+
comment = post.comments.build
|
49
|
+
comment.commentable_id.should == post.id
|
50
|
+
end
|
51
|
+
|
52
|
+
should "assign _type" do
|
53
|
+
post = Post.new
|
54
|
+
comment = post.comments.build
|
55
|
+
comment.commentable_type.should == "Post"
|
56
|
+
end
|
57
|
+
|
58
|
+
should "allow assigning attributes" do
|
59
|
+
post = Post.new
|
60
|
+
comment = post.comments.build(:body => 'foo bar')
|
61
|
+
comment.body.should == 'foo bar'
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "create" do
|
66
|
+
should "assign foreign key" do
|
67
|
+
post = Post.new
|
68
|
+
comment = post.comments.create
|
69
|
+
comment.commentable_id.should == post.id
|
70
|
+
end
|
71
|
+
|
72
|
+
should "assign _type" do
|
73
|
+
post = Post.new
|
74
|
+
comment = post.comments.create
|
75
|
+
comment.commentable_type.should == "Post"
|
76
|
+
end
|
77
|
+
|
78
|
+
should "save record" do
|
79
|
+
post = Post.new
|
80
|
+
lambda {
|
81
|
+
post.comments.create(:body => 'baz')
|
82
|
+
}.should change { PostComment.count }
|
83
|
+
end
|
84
|
+
|
85
|
+
should "allow passing attributes" do
|
86
|
+
post = Post.create
|
87
|
+
comment = post.comments.create(:body => 'foo bar')
|
88
|
+
comment.body.should == 'foo bar'
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
context "count" do
|
93
|
+
should "work scoped to association" do
|
94
|
+
post = Post.create
|
95
|
+
3.times { post.comments.create(:body => 'foo bar') }
|
96
|
+
|
97
|
+
other_post = Post.create
|
98
|
+
2.times { other_post.comments.create(:body => 'baz') }
|
99
|
+
|
100
|
+
post.comments.count.should == 3
|
101
|
+
other_post.comments.count.should == 2
|
102
|
+
end
|
103
|
+
|
104
|
+
should "work with conditions" do
|
105
|
+
post = Post.create
|
106
|
+
post.comments.create(:body => 'foo bar')
|
107
|
+
post.comments.create(:body => 'baz')
|
108
|
+
post.comments.create(:body => 'foo bar')
|
109
|
+
|
110
|
+
post.comments.count(:body => 'foo bar').should == 2
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
context "Finding scoped to association" do
|
115
|
+
setup do
|
116
|
+
@post = Post.new
|
117
|
+
|
118
|
+
@comment1 = PostComment.create(:body => 'comment1')
|
119
|
+
@comment2 = PostComment.create(:body => 'comment2')
|
120
|
+
@comment3 = PostComment.create(:body => 'comment3')
|
121
|
+
@post.comments = [@comment1, @comment2]
|
122
|
+
@post.save
|
123
|
+
|
124
|
+
@post2 = Post.create(:body => "post #2")
|
125
|
+
@comment4 = PostComment.create(:body => 'comment4')
|
126
|
+
@comment5 = PostComment.create(:body => 'comment5')
|
127
|
+
@comment6 = PostComment.create(:body => 'comment6')
|
128
|
+
@post2.comments = [@comment4, @comment5, @comment6]
|
129
|
+
@post2.save
|
130
|
+
end
|
131
|
+
|
132
|
+
context "with :all" do
|
133
|
+
should "work" do
|
134
|
+
@post.comments.find(:all).should include(@comment1)
|
135
|
+
@post.comments.find(:all).should include(@comment2)
|
136
|
+
end
|
137
|
+
|
138
|
+
should "work with conditions" do
|
139
|
+
comments = @post.comments.find(:all, :conditions => {:body => 'comment1'})
|
140
|
+
comments.should == [@comment1]
|
141
|
+
end
|
142
|
+
|
143
|
+
should "work with order" do
|
144
|
+
comments = @post.comments.find(:all, :order => 'body desc')
|
145
|
+
comments.should == [@comment2, @comment1]
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
context "with #all" do
|
150
|
+
should "work" do
|
151
|
+
@post.comments.all.should == [@comment1, @comment2]
|
152
|
+
end
|
153
|
+
|
154
|
+
should "work with conditions" do
|
155
|
+
comments = @post.comments.all(:conditions => {:body => 'comment1'})
|
156
|
+
comments.should == [@comment1]
|
157
|
+
end
|
158
|
+
|
159
|
+
should "work with order" do
|
160
|
+
comments = @post.comments.all(:order => '$natural desc')
|
161
|
+
comments.should == [@comment2, @comment1]
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
context "with one id" do
|
166
|
+
should "work for id in association" do
|
167
|
+
@post.comments.find(@comment2.id).should == @comment2
|
168
|
+
end
|
169
|
+
|
170
|
+
should "not work for id not in association" do
|
171
|
+
lambda {
|
172
|
+
@post.comments.find(@comment5.id)
|
173
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
context "with multiple ids" do
|
178
|
+
should "work for ids in association" do
|
179
|
+
posts = @post.comments.find(@comment1.id, @comment2.id)
|
180
|
+
posts.should == [@comment1, @comment2]
|
181
|
+
end
|
182
|
+
|
183
|
+
should "not work for ids not in association" do
|
184
|
+
lambda {
|
185
|
+
@post.comments.find(@comment1.id, @comment2.id, @comment4.id)
|
186
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
context "with #paginate" do
|
191
|
+
setup do
|
192
|
+
@comments = @post2.comments.paginate(:per_page => 2, :page => 1, :order => 'created_at asc')
|
193
|
+
end
|
194
|
+
|
195
|
+
should "return total pages" do
|
196
|
+
@comments.total_pages.should == 2
|
197
|
+
end
|
198
|
+
|
199
|
+
should "return total entries" do
|
200
|
+
@comments.total_entries.should == 3
|
201
|
+
end
|
202
|
+
|
203
|
+
should "return the subject" do
|
204
|
+
@comments.should include(@comment4)
|
205
|
+
@comments.should include(@comment5)
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
254
209
|
end
|