crnixon-mongomapper 0.2.0 → 0.3.4
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/.gitignore +1 -0
- data/History +48 -0
- data/README.rdoc +5 -3
- data/Rakefile +6 -4
- data/VERSION +1 -1
- data/bin/mmconsole +56 -0
- data/lib/mongomapper.rb +29 -18
- data/lib/mongomapper/associations.rb +53 -38
- data/lib/mongomapper/associations/base.rb +53 -20
- data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +34 -0
- data/lib/mongomapper/associations/belongs_to_proxy.rb +10 -14
- data/lib/mongomapper/associations/many_documents_as_proxy.rb +27 -0
- data/lib/mongomapper/associations/many_documents_proxy.rb +103 -0
- data/lib/mongomapper/associations/many_embedded_polymorphic_proxy.rb +33 -0
- data/lib/mongomapper/associations/{has_many_embedded_proxy.rb → many_embedded_proxy.rb} +6 -8
- data/lib/mongomapper/associations/many_polymorphic_proxy.rb +11 -0
- data/lib/mongomapper/associations/{array_proxy.rb → many_proxy.rb} +1 -1
- data/lib/mongomapper/associations/proxy.rb +24 -21
- data/lib/mongomapper/callbacks.rb +1 -1
- data/lib/mongomapper/document.rb +160 -74
- data/lib/mongomapper/dynamic_finder.rb +38 -0
- data/lib/mongomapper/embedded_document.rb +154 -105
- data/lib/mongomapper/finder_options.rb +11 -7
- data/lib/mongomapper/key.rb +15 -21
- data/lib/mongomapper/pagination.rb +52 -0
- data/lib/mongomapper/rails_compatibility/document.rb +15 -0
- data/lib/mongomapper/rails_compatibility/embedded_document.rb +25 -0
- data/lib/mongomapper/serialization.rb +1 -1
- data/lib/mongomapper/serializers/json_serializer.rb +15 -0
- data/lib/mongomapper/support.rb +30 -0
- data/mongomapper.gemspec +87 -46
- data/test/NOTE_ON_TESTING +1 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +53 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +45 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +253 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +131 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +106 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +261 -0
- data/test/functional/associations/test_many_proxy.rb +295 -0
- data/test/functional/test_associations.rb +47 -0
- data/test/{test_callbacks.rb → functional/test_callbacks.rb} +2 -1
- data/test/functional/test_document.rb +952 -0
- data/test/functional/test_pagination.rb +81 -0
- data/test/functional/test_rails_compatibility.rb +30 -0
- data/test/functional/test_validations.rb +172 -0
- data/test/models.rb +169 -0
- data/test/test_helper.rb +7 -2
- data/test/unit/serializers/test_json_serializer.rb +189 -0
- data/test/unit/test_association_base.rb +144 -0
- data/test/unit/test_document.rb +123 -0
- data/test/unit/test_embedded_document.rb +526 -0
- data/test/{test_finder_options.rb → unit/test_finder_options.rb} +36 -1
- data/test/{test_key.rb → unit/test_key.rb} +59 -12
- data/test/{test_mongomapper.rb → unit/test_mongomapper.rb} +0 -0
- data/test/{test_observing.rb → unit/test_observing.rb} +0 -0
- data/test/unit/test_pagination.rb +113 -0
- data/test/unit/test_rails_compatibility.rb +34 -0
- data/test/{test_serializations.rb → unit/test_serializations.rb} +0 -2
- data/test/{test_validations.rb → unit/test_validations.rb} +0 -134
- metadata +81 -43
- data/lib/mongomapper/associations/has_many_proxy.rb +0 -28
- data/lib/mongomapper/associations/polymorphic_belongs_to_proxy.rb +0 -31
- data/lib/mongomapper/rails_compatibility.rb +0 -23
- data/test/serializers/test_json_serializer.rb +0 -104
- data/test/test_associations.rb +0 -174
- data/test/test_document.rb +0 -944
- data/test/test_embedded_document.rb +0 -253
- data/test/test_rails_compatibility.rb +0 -29
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: crnixon-mongomapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Nunemaker
|
@@ -9,8 +9,8 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
13
|
-
default_executable:
|
12
|
+
date: 2009-08-28 00:00:00 -07:00
|
13
|
+
default_executable: mmconsole
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
@@ -28,9 +28,9 @@ dependencies:
|
|
28
28
|
version_requirement:
|
29
29
|
version_requirements: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 0.11.1
|
34
34
|
version:
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: jnunemaker-validatable
|
@@ -38,9 +38,9 @@ dependencies:
|
|
38
38
|
version_requirement:
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
40
40
|
requirements:
|
41
|
-
- - "
|
41
|
+
- - ">="
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
version: 1.7.
|
43
|
+
version: 1.7.2
|
44
44
|
version:
|
45
45
|
- !ruby/object:Gem::Dependency
|
46
46
|
name: mocha
|
@@ -48,7 +48,7 @@ dependencies:
|
|
48
48
|
version_requirement:
|
49
49
|
version_requirements: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
|
-
- - "
|
51
|
+
- - ">="
|
52
52
|
- !ruby/object:Gem::Version
|
53
53
|
version: 0.9.4
|
54
54
|
version:
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
version_requirement:
|
59
59
|
version_requirements: !ruby/object:Gem::Requirement
|
60
60
|
requirements:
|
61
|
-
- - "
|
61
|
+
- - ">="
|
62
62
|
- !ruby/object:Gem::Version
|
63
63
|
version: 0.4.0
|
64
64
|
version:
|
65
65
|
description:
|
66
66
|
email: nunemaker@gmail.com
|
67
|
-
executables:
|
68
|
-
|
67
|
+
executables:
|
68
|
+
- mmconsole
|
69
69
|
extensions: []
|
70
70
|
|
71
71
|
extra_rdoc_files:
|
@@ -78,42 +78,66 @@ files:
|
|
78
78
|
- README.rdoc
|
79
79
|
- Rakefile
|
80
80
|
- VERSION
|
81
|
+
- bin/mmconsole
|
81
82
|
- lib/mongomapper.rb
|
82
83
|
- lib/mongomapper/associations.rb
|
83
|
-
- lib/mongomapper/associations/array_proxy.rb
|
84
84
|
- lib/mongomapper/associations/base.rb
|
85
|
+
- lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb
|
85
86
|
- lib/mongomapper/associations/belongs_to_proxy.rb
|
86
|
-
- lib/mongomapper/associations/
|
87
|
-
- lib/mongomapper/associations/
|
88
|
-
- lib/mongomapper/associations/
|
87
|
+
- lib/mongomapper/associations/many_documents_as_proxy.rb
|
88
|
+
- lib/mongomapper/associations/many_documents_proxy.rb
|
89
|
+
- lib/mongomapper/associations/many_embedded_polymorphic_proxy.rb
|
90
|
+
- lib/mongomapper/associations/many_embedded_proxy.rb
|
91
|
+
- lib/mongomapper/associations/many_polymorphic_proxy.rb
|
92
|
+
- lib/mongomapper/associations/many_proxy.rb
|
89
93
|
- lib/mongomapper/associations/proxy.rb
|
90
94
|
- lib/mongomapper/callbacks.rb
|
91
95
|
- lib/mongomapper/document.rb
|
96
|
+
- lib/mongomapper/dynamic_finder.rb
|
92
97
|
- lib/mongomapper/embedded_document.rb
|
93
98
|
- lib/mongomapper/finder_options.rb
|
94
99
|
- lib/mongomapper/key.rb
|
95
100
|
- lib/mongomapper/observing.rb
|
96
|
-
- lib/mongomapper/
|
101
|
+
- lib/mongomapper/pagination.rb
|
102
|
+
- lib/mongomapper/rails_compatibility/document.rb
|
103
|
+
- lib/mongomapper/rails_compatibility/embedded_document.rb
|
97
104
|
- lib/mongomapper/save_with_validation.rb
|
98
105
|
- lib/mongomapper/serialization.rb
|
99
106
|
- lib/mongomapper/serializers/json_serializer.rb
|
107
|
+
- lib/mongomapper/support.rb
|
100
108
|
- lib/mongomapper/validations.rb
|
101
109
|
- mongomapper.gemspec
|
102
|
-
- test/
|
103
|
-
- test/
|
104
|
-
- test/
|
105
|
-
- test/
|
106
|
-
- test/
|
107
|
-
- test/
|
110
|
+
- test/NOTE_ON_TESTING
|
111
|
+
- test/functional/associations/test_belongs_to_polymorphic_proxy.rb
|
112
|
+
- test/functional/associations/test_belongs_to_proxy.rb
|
113
|
+
- test/functional/associations/test_many_documents_as_proxy.rb
|
114
|
+
- test/functional/associations/test_many_embedded_polymorphic_proxy.rb
|
115
|
+
- test/functional/associations/test_many_embedded_proxy.rb
|
116
|
+
- test/functional/associations/test_many_polymorphic_proxy.rb
|
117
|
+
- test/functional/associations/test_many_proxy.rb
|
118
|
+
- test/functional/test_associations.rb
|
119
|
+
- test/functional/test_callbacks.rb
|
120
|
+
- test/functional/test_document.rb
|
121
|
+
- test/functional/test_pagination.rb
|
122
|
+
- test/functional/test_rails_compatibility.rb
|
123
|
+
- test/functional/test_validations.rb
|
124
|
+
- test/models.rb
|
108
125
|
- test/test_helper.rb
|
109
|
-
- test/
|
110
|
-
- test/
|
111
|
-
- test/
|
112
|
-
- test/
|
113
|
-
- test/
|
114
|
-
- test/
|
115
|
-
|
126
|
+
- test/unit/serializers/test_json_serializer.rb
|
127
|
+
- test/unit/test_association_base.rb
|
128
|
+
- test/unit/test_document.rb
|
129
|
+
- test/unit/test_embedded_document.rb
|
130
|
+
- test/unit/test_finder_options.rb
|
131
|
+
- test/unit/test_key.rb
|
132
|
+
- test/unit/test_mongomapper.rb
|
133
|
+
- test/unit/test_observing.rb
|
134
|
+
- test/unit/test_pagination.rb
|
135
|
+
- test/unit/test_rails_compatibility.rb
|
136
|
+
- test/unit/test_serializations.rb
|
137
|
+
- test/unit/test_validations.rb
|
138
|
+
has_rdoc: false
|
116
139
|
homepage: http://github.com/jnunemaker/mongomapper
|
140
|
+
licenses:
|
117
141
|
post_install_message:
|
118
142
|
rdoc_options:
|
119
143
|
- --charset=UTF-8
|
@@ -134,21 +158,35 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
158
|
requirements: []
|
135
159
|
|
136
160
|
rubyforge_project: mongomapper
|
137
|
-
rubygems_version: 1.
|
161
|
+
rubygems_version: 1.3.5
|
138
162
|
signing_key:
|
139
|
-
specification_version:
|
163
|
+
specification_version: 3
|
140
164
|
summary: Awesome gem for modeling your domain and storing it in mongo
|
141
165
|
test_files:
|
142
|
-
- test/
|
143
|
-
- test/
|
144
|
-
- test/
|
145
|
-
- test/
|
146
|
-
- test/
|
147
|
-
- test/
|
166
|
+
- test/functional/associations/test_belongs_to_polymorphic_proxy.rb
|
167
|
+
- test/functional/associations/test_belongs_to_proxy.rb
|
168
|
+
- test/functional/associations/test_many_documents_as_proxy.rb
|
169
|
+
- test/functional/associations/test_many_embedded_polymorphic_proxy.rb
|
170
|
+
- test/functional/associations/test_many_embedded_proxy.rb
|
171
|
+
- test/functional/associations/test_many_polymorphic_proxy.rb
|
172
|
+
- test/functional/associations/test_many_proxy.rb
|
173
|
+
- test/functional/test_associations.rb
|
174
|
+
- test/functional/test_callbacks.rb
|
175
|
+
- test/functional/test_document.rb
|
176
|
+
- test/functional/test_pagination.rb
|
177
|
+
- test/functional/test_rails_compatibility.rb
|
178
|
+
- test/functional/test_validations.rb
|
179
|
+
- test/models.rb
|
148
180
|
- test/test_helper.rb
|
149
|
-
- test/
|
150
|
-
- test/
|
151
|
-
- test/
|
152
|
-
- test/
|
153
|
-
- test/
|
154
|
-
- test/
|
181
|
+
- test/unit/serializers/test_json_serializer.rb
|
182
|
+
- test/unit/test_association_base.rb
|
183
|
+
- test/unit/test_document.rb
|
184
|
+
- test/unit/test_embedded_document.rb
|
185
|
+
- test/unit/test_finder_options.rb
|
186
|
+
- test/unit/test_key.rb
|
187
|
+
- test/unit/test_mongomapper.rb
|
188
|
+
- test/unit/test_observing.rb
|
189
|
+
- test/unit/test_pagination.rb
|
190
|
+
- test/unit/test_rails_compatibility.rb
|
191
|
+
- test/unit/test_serializations.rb
|
192
|
+
- test/unit/test_validations.rb
|
@@ -1,28 +0,0 @@
|
|
1
|
-
module MongoMapper
|
2
|
-
module Associations
|
3
|
-
class HasManyProxy < ArrayProxy
|
4
|
-
def replace(v)
|
5
|
-
if load_target
|
6
|
-
@target.map(&:destroy)
|
7
|
-
end
|
8
|
-
|
9
|
-
v.each do |o|
|
10
|
-
@owner.save if @owner.new?
|
11
|
-
o.__send__(:write_attribute, self.foreign_key, @owner.id)
|
12
|
-
o.save
|
13
|
-
o
|
14
|
-
end
|
15
|
-
reload_target
|
16
|
-
end
|
17
|
-
|
18
|
-
protected
|
19
|
-
def find_target
|
20
|
-
@association.klass.find(:all, {:conditions => {self.foreign_key => @owner.id}})
|
21
|
-
end
|
22
|
-
|
23
|
-
def foreign_key
|
24
|
-
@association.options[:foreign_key] || @owner.class.name.underscore.gsub("/", "_") + "_id"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
module MongoMapper
|
2
|
-
module Associations
|
3
|
-
class PolymorphicBelongsToProxy < Proxy
|
4
|
-
def replace(v)
|
5
|
-
ref_id = "#{@association.name}_id"
|
6
|
-
ref_type = "#{@association.name}_type"
|
7
|
-
|
8
|
-
if v
|
9
|
-
v.save if v.new?
|
10
|
-
@owner.__send__(:write_attribute, ref_id, v.id)
|
11
|
-
@owner.__send__(:write_attribute, ref_type, v.class.name)
|
12
|
-
else
|
13
|
-
@owner.__send__(:write_attribute, ref_id, nil)
|
14
|
-
@owner.__send__(:write_attribute, ref_type, nil)
|
15
|
-
end
|
16
|
-
@owner.save
|
17
|
-
|
18
|
-
reload_target
|
19
|
-
end
|
20
|
-
|
21
|
-
protected
|
22
|
-
def find_target
|
23
|
-
ref_id = @owner.__send__(:read_attribute, "#{@association.name}_id")
|
24
|
-
ref_type = @owner.__send__(:read_attribute, "#{@association.name}_type")
|
25
|
-
if ref_id && ref_type
|
26
|
-
ref_type.constantize.find(ref_id)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module MongoMapper
|
2
|
-
module RailsCompatibility
|
3
|
-
def self.included(model)
|
4
|
-
model.class_eval do
|
5
|
-
alias_method :new_record?, :new?
|
6
|
-
extend ClassMethods
|
7
|
-
end
|
8
|
-
class << model
|
9
|
-
alias_method :has_many, :many
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
module ClassMethods
|
14
|
-
def column_names
|
15
|
-
keys.keys
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def to_param
|
20
|
-
id
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,104 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class JsonSerializationTest < Test::Unit::TestCase
|
4
|
-
class Contact
|
5
|
-
include MongoMapper::EmbeddedDocument
|
6
|
-
key :name, String
|
7
|
-
key :age, Integer
|
8
|
-
key :created_at, Time
|
9
|
-
key :awesome, Boolean
|
10
|
-
key :preferences, Hash
|
11
|
-
end
|
12
|
-
|
13
|
-
def setup
|
14
|
-
Contact.include_root_in_json = false
|
15
|
-
@contact = Contact.new(
|
16
|
-
:name => 'Konata Izumi',
|
17
|
-
:age => 16,
|
18
|
-
:created_at => Time.utc(2006, 8, 1),
|
19
|
-
:awesome => true,
|
20
|
-
:preferences => { :shows => 'anime' }
|
21
|
-
)
|
22
|
-
end
|
23
|
-
|
24
|
-
should "include demodulized root" do
|
25
|
-
Contact.include_root_in_json = true
|
26
|
-
assert_match %r{^\{"contact": \{}, @contact.to_json
|
27
|
-
end
|
28
|
-
|
29
|
-
should "encode all encodable attributes" do
|
30
|
-
json = @contact.to_json
|
31
|
-
|
32
|
-
assert_match %r{"name": "Konata Izumi"}, json
|
33
|
-
assert_match %r{"age": 16}, json
|
34
|
-
assert json.include?(%("created_at": #{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
|
35
|
-
assert_match %r{"awesome": true}, json
|
36
|
-
assert_match %r{"preferences": \{"shows": "anime"\}}, json
|
37
|
-
end
|
38
|
-
|
39
|
-
should "allow attribute filtering with only" do
|
40
|
-
json = @contact.to_json(:only => [:name, :age])
|
41
|
-
|
42
|
-
assert_match %r{"name": "Konata Izumi"}, json
|
43
|
-
assert_match %r{"age": 16}, json
|
44
|
-
assert_no_match %r{"awesome"}, json
|
45
|
-
assert_no_match %r{"created_at"}, json
|
46
|
-
assert_no_match %r{"preferences"}, json
|
47
|
-
end
|
48
|
-
|
49
|
-
should "allow attribute filtering with except" do
|
50
|
-
json = @contact.to_json(:except => [:name, :age])
|
51
|
-
|
52
|
-
assert_no_match %r{"name"}, json
|
53
|
-
assert_no_match %r{"age"}, json
|
54
|
-
assert_match %r{"awesome"}, json
|
55
|
-
assert_match %r{"created_at"}, json
|
56
|
-
assert_match %r{"preferences"}, json
|
57
|
-
end
|
58
|
-
|
59
|
-
context "including methods" do
|
60
|
-
setup do
|
61
|
-
def @contact.label; "Has cheezburger"; end
|
62
|
-
def @contact.favorite_quote; "Constraints are liberating"; end
|
63
|
-
end
|
64
|
-
|
65
|
-
should "include single method" do
|
66
|
-
# Single method.
|
67
|
-
assert_match %r{"label": "Has cheezburger"}, @contact.to_json(:only => :name, :methods => :label)
|
68
|
-
end
|
69
|
-
|
70
|
-
should "include multiple methods" do
|
71
|
-
json = @contact.to_json(:only => :name, :methods => [:label, :favorite_quote])
|
72
|
-
assert_match %r{"label": "Has cheezburger"}, json
|
73
|
-
assert_match %r{"favorite_quote": "Constraints are liberating"}, json
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
context "array of records" do
|
78
|
-
setup do
|
79
|
-
@contacts = [
|
80
|
-
Contact.new(:name => 'David', :age => 39),
|
81
|
-
Contact.new(:name => 'Mary', :age => 14)
|
82
|
-
]
|
83
|
-
end
|
84
|
-
|
85
|
-
should "allow attribute filtering with only" do
|
86
|
-
assert_equal %([{"name": "David"}, {"name": "Mary"}]), @contacts.to_json(:only => :name)
|
87
|
-
end
|
88
|
-
|
89
|
-
should "allow attribute filtering with except" do
|
90
|
-
json = @contacts.to_json(:except => [:name, :preferences, :awesome, :created_at])
|
91
|
-
assert_equal %([{"age": 39}, {"age": 14}]), json
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
should "allow options for hash of records" do
|
96
|
-
contacts = {
|
97
|
-
1 => Contact.new(:name => 'David', :age => 39),
|
98
|
-
2 => Contact.new(:name => 'Mary', :age => 14)
|
99
|
-
}
|
100
|
-
|
101
|
-
assert_equal %({"1": {"name": "David"}}), contacts.to_json(:only => [1, :name])
|
102
|
-
end
|
103
|
-
|
104
|
-
end
|
data/test/test_associations.rb
DELETED
@@ -1,174 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class Address
|
4
|
-
include MongoMapper::EmbeddedDocument
|
5
|
-
|
6
|
-
key :address, String
|
7
|
-
key :city, String
|
8
|
-
key :state, String
|
9
|
-
key :zip, Integer
|
10
|
-
end
|
11
|
-
|
12
|
-
class Project
|
13
|
-
include MongoMapper::Document
|
14
|
-
|
15
|
-
key :name, String
|
16
|
-
|
17
|
-
many :statuses
|
18
|
-
many :addresses
|
19
|
-
end
|
20
|
-
|
21
|
-
class Status
|
22
|
-
include MongoMapper::Document
|
23
|
-
|
24
|
-
belongs_to :project
|
25
|
-
belongs_to :target, :polymorphic => true
|
26
|
-
|
27
|
-
key :name, String
|
28
|
-
end
|
29
|
-
|
30
|
-
class Person
|
31
|
-
include MongoMapper::EmbeddedDocument
|
32
|
-
key :name, String
|
33
|
-
key :child, Person
|
34
|
-
end
|
35
|
-
|
36
|
-
class AssociationsTest < Test::Unit::TestCase
|
37
|
-
def setup
|
38
|
-
Project.collection.clear
|
39
|
-
Status.collection.clear
|
40
|
-
end
|
41
|
-
|
42
|
-
context "Polymorphic Belongs To" do
|
43
|
-
should "default to nil" do
|
44
|
-
status = Status.new
|
45
|
-
status.target.should be_nil
|
46
|
-
end
|
47
|
-
|
48
|
-
should "store the association" do
|
49
|
-
status = Status.new
|
50
|
-
project = Project.new(:name => "mongomapper")
|
51
|
-
status.target = project
|
52
|
-
status.save.should be_true
|
53
|
-
|
54
|
-
from_db = Status.find(status.id)
|
55
|
-
from_db.target.should_not be_nil
|
56
|
-
from_db.target_id.should == project.id
|
57
|
-
from_db.target_type.should == "Project"
|
58
|
-
from_db.target.name.should == "mongomapper"
|
59
|
-
end
|
60
|
-
|
61
|
-
should "unset the association" do
|
62
|
-
status = Status.new
|
63
|
-
project = Project.new(:name => "mongomapper")
|
64
|
-
status.target = project
|
65
|
-
status.save.should be_true
|
66
|
-
|
67
|
-
from_db = Status.find(status.id)
|
68
|
-
from_db.target = nil
|
69
|
-
from_db.target_type.should be_nil
|
70
|
-
from_db.target_id.should be_nil
|
71
|
-
from_db.target.should be_nil
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
context "Belongs To" do
|
76
|
-
should "default to nil" do
|
77
|
-
status = Status.new
|
78
|
-
status.project.should be_nil
|
79
|
-
end
|
80
|
-
|
81
|
-
should "store the association" do
|
82
|
-
status = Status.new
|
83
|
-
project = Project.new(:name => "mongomapper")
|
84
|
-
status.project = project
|
85
|
-
status.save.should be_true
|
86
|
-
|
87
|
-
from_db = Status.find(status.id)
|
88
|
-
from_db.project.should_not be_nil
|
89
|
-
from_db.project.name.should == "mongomapper"
|
90
|
-
end
|
91
|
-
|
92
|
-
should "unset the association" do
|
93
|
-
status = Status.new
|
94
|
-
project = Project.new(:name => "mongomapper")
|
95
|
-
status.project = project
|
96
|
-
status.save.should be_true
|
97
|
-
|
98
|
-
from_db = Status.find(status.id)
|
99
|
-
from_db.project = nil
|
100
|
-
from_db.project.should be_nil
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
context "Many documents" do
|
105
|
-
should "default reader to empty array" do
|
106
|
-
project = Project.new
|
107
|
-
project.statuses.should == []
|
108
|
-
end
|
109
|
-
|
110
|
-
should "allow adding to association like it was an array" do
|
111
|
-
project = Project.new
|
112
|
-
project.statuses << Status.new
|
113
|
-
project.statuses.push Status.new
|
114
|
-
project.statuses.size.should == 2
|
115
|
-
end
|
116
|
-
|
117
|
-
should "store the association" do
|
118
|
-
project = Project.new
|
119
|
-
project.statuses = [Status.new("name" => "ready")]
|
120
|
-
project.save.should be_true
|
121
|
-
|
122
|
-
from_db = Project.find(project.id)
|
123
|
-
from_db.statuses.size.should == 1
|
124
|
-
from_db.statuses[0].name.should == "ready"
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
context "Many embedded documents" do
|
129
|
-
should "default reader to empty array" do
|
130
|
-
project = Project.new
|
131
|
-
project.addresses.should == []
|
132
|
-
end
|
133
|
-
|
134
|
-
should "allow adding to association like it was an array" do
|
135
|
-
project = Project.new
|
136
|
-
project.addresses << Address.new
|
137
|
-
project.addresses.push Address.new
|
138
|
-
project.addresses.size.should == 2
|
139
|
-
end
|
140
|
-
|
141
|
-
should "be embedded in document on save" do
|
142
|
-
sb = Address.new(:city => 'South Bend', :state => 'IN')
|
143
|
-
chi = Address.new(:city => 'Chicago', :state => 'IL')
|
144
|
-
project = Project.new
|
145
|
-
project.addresses << sb
|
146
|
-
project.addresses << chi
|
147
|
-
project.save
|
148
|
-
|
149
|
-
from_db = Project.find(project.id)
|
150
|
-
from_db.addresses.size.should == 2
|
151
|
-
from_db.addresses[0].should == sb
|
152
|
-
from_db.addresses[1].should == chi
|
153
|
-
end
|
154
|
-
|
155
|
-
should "allow embedding arbitrarily deep" do
|
156
|
-
@document = Class.new do
|
157
|
-
include MongoMapper::Document
|
158
|
-
key :person, Person
|
159
|
-
end
|
160
|
-
|
161
|
-
meg = Person.new(:name => "Meg")
|
162
|
-
meg.child = Person.new(:name => "Steve")
|
163
|
-
meg.child.child = Person.new(:name => "Linda")
|
164
|
-
|
165
|
-
doc = @document.new(:person => meg)
|
166
|
-
doc.save
|
167
|
-
|
168
|
-
from_db = @document.find(doc.id)
|
169
|
-
from_db.person.name.should == 'Meg'
|
170
|
-
from_db.person.child.name.should == 'Steve'
|
171
|
-
from_db.person.child.child.name.should == 'Linda'
|
172
|
-
end
|
173
|
-
end
|
174
|
-
end
|