djsun-mongomapper 0.3.3 → 0.3.5
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/History +7 -1
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/bin/mmconsole +0 -1
- data/lib/mongomapper.rb +4 -3
- data/lib/mongomapper/associations/base.rb +25 -11
- data/lib/mongomapper/associations/belongs_to_proxy.rb +2 -2
- data/lib/mongomapper/associations/many_documents_as_proxy.rb +27 -0
- data/lib/mongomapper/document.rb +3 -3
- data/lib/mongomapper/embedded_document.rb +7 -3
- data/lib/mongomapper/serialization.rb +1 -1
- data/mongomapper.gemspec +8 -5
- data/test/functional/associations/test_many_documents_as_proxy.rb +253 -0
- data/test/functional/test_document.rb +1 -1
- data/test/models.rb +47 -17
- data/test/unit/serializers/test_json_serializer.rb +36 -4
- data/test/unit/test_document.rb +4 -4
- data/test/unit/test_embedded_document.rb +21 -0
- data/test/unit/test_mongomapper.rb +3 -3
- metadata +7 -5
data/History
CHANGED
@@ -1,6 +1,12 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.5 8/29/2009
|
2
|
+
* updated to 0.14 of the ruby driver
|
3
|
+
* _type key gets set automatically at initialize time if defined and blank
|
4
|
+
|
5
|
+
0.3.4 8/28/2009
|
2
6
|
* BACKWORDS COMPATIBILITY BREAK: Timestamps are now optional. To use them add timestamps! to your model.
|
3
7
|
* BACKWORDS COMPATIBILITY BREAK: Associations keys are no longer created automatically when you use belongs_to and many. Too much was hidden from the developer. You now have to declare them like key :creator_id, String and such.
|
8
|
+
* to_json now includes dynamic keys and embedded stuff by default
|
9
|
+
* added polymorphic many with :as option (dcu)
|
4
10
|
|
5
11
|
0.3.3 8/16/2009
|
6
12
|
* BACKWORDS COMPATIBILITY BREAK: _id is now once again a string rather than an object id and will stay that way.
|
data/Rakefile
CHANGED
@@ -12,7 +12,7 @@ begin
|
|
12
12
|
gem.rubyforge_project = "mongomapper"
|
13
13
|
|
14
14
|
gem.add_dependency('activesupport')
|
15
|
-
gem.add_dependency('mongodb-mongo', '0.
|
15
|
+
gem.add_dependency('mongodb-mongo', '0.14')
|
16
16
|
gem.add_dependency('jnunemaker-validatable', '1.7.2')
|
17
17
|
|
18
18
|
gem.add_development_dependency('mocha', '0.9.4')
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.5
|
data/bin/mmconsole
CHANGED
data/lib/mongomapper.rb
CHANGED
@@ -2,10 +2,10 @@ require 'pathname'
|
|
2
2
|
require 'rubygems'
|
3
3
|
|
4
4
|
gem 'activesupport'
|
5
|
-
gem 'mongodb-mongo', '0.
|
5
|
+
gem 'mongodb-mongo', '0.14'
|
6
6
|
gem 'jnunemaker-validatable', '1.7.2'
|
7
7
|
|
8
|
-
require 'activesupport'
|
8
|
+
# require 'activesupport'
|
9
9
|
require 'mongo'
|
10
10
|
require 'validatable'
|
11
11
|
|
@@ -25,6 +25,7 @@ require dir + 'associations/many_proxy'
|
|
25
25
|
require dir + 'associations/many_polymorphic_proxy'
|
26
26
|
require dir + 'associations/many_embedded_proxy'
|
27
27
|
require dir + 'associations/many_embedded_polymorphic_proxy'
|
28
|
+
require dir + 'associations/many_documents_as_proxy'
|
28
29
|
|
29
30
|
require dir + 'callbacks'
|
30
31
|
require dir + 'finder_options'
|
@@ -53,7 +54,7 @@ module MongoMapper
|
|
53
54
|
end
|
54
55
|
|
55
56
|
def self.connection
|
56
|
-
@@connection ||=
|
57
|
+
@@connection ||= Mongo::Connection.new
|
57
58
|
end
|
58
59
|
|
59
60
|
def self.connection=(new_connection)
|
@@ -6,7 +6,7 @@ module MongoMapper
|
|
6
6
|
def initialize(type, name, options = {})
|
7
7
|
@type, @name, @options = type, name, options
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
def class_name
|
11
11
|
@class_name ||= begin
|
12
12
|
if cn = options[:class_name]
|
@@ -18,35 +18,43 @@ module MongoMapper
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def klass
|
23
23
|
@klass ||= class_name.constantize
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
def many?
|
27
27
|
@many_type ||= @type == :many
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
def belongs_to?
|
31
31
|
@belongs_to_type ||= @type == :belongs_to
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
def polymorphic?
|
35
35
|
!!@options[:polymorphic]
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
|
+
def as?
|
39
|
+
!!@options[:as]
|
40
|
+
end
|
41
|
+
|
38
42
|
def type_key_name
|
39
|
-
@type_key_name ||= many? ? '_type' : "#{
|
43
|
+
@type_key_name ||= many? ? '_type' : "#{as}_type"
|
44
|
+
end
|
45
|
+
|
46
|
+
def as
|
47
|
+
@options[:as] || self.name
|
40
48
|
end
|
41
|
-
|
49
|
+
|
42
50
|
def foreign_key
|
43
51
|
@options[:foreign_key] || "#{name}_id"
|
44
52
|
end
|
45
|
-
|
53
|
+
|
46
54
|
def ivar
|
47
55
|
@ivar ||= "@_#{name}"
|
48
56
|
end
|
49
|
-
|
57
|
+
|
50
58
|
def embeddable?
|
51
59
|
many? && klass.embeddable?
|
52
60
|
end
|
@@ -57,7 +65,13 @@ module MongoMapper
|
|
57
65
|
if self.klass.embeddable?
|
58
66
|
polymorphic? ? ManyEmbeddedPolymorphicProxy : ManyEmbeddedProxy
|
59
67
|
else
|
60
|
-
polymorphic?
|
68
|
+
if polymorphic?
|
69
|
+
ManyPolymorphicProxy
|
70
|
+
elsif as?
|
71
|
+
ManyDocumentsAsProxy
|
72
|
+
else
|
73
|
+
ManyProxy
|
74
|
+
end
|
61
75
|
end
|
62
76
|
else
|
63
77
|
polymorphic? ? BelongsToPolymorphicProxy : BelongsToProxy
|
@@ -6,12 +6,12 @@ module MongoMapper
|
|
6
6
|
doc.save if doc.new?
|
7
7
|
id = doc.id
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
@owner.send("#{@association.foreign_key}=", id)
|
11
11
|
reset
|
12
12
|
end
|
13
13
|
|
14
|
-
protected
|
14
|
+
protected
|
15
15
|
def find_target
|
16
16
|
if association_id = @owner.send(@association.foreign_key)
|
17
17
|
@association.klass.find_by_id(association_id)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Associations
|
3
|
+
class ManyDocumentsAsProxy < ManyDocumentsProxy
|
4
|
+
protected
|
5
|
+
def scoped_conditions
|
6
|
+
{as_type_name => @owner.class.name, as_id_name => @owner.id}
|
7
|
+
end
|
8
|
+
|
9
|
+
def apply_scope(doc)
|
10
|
+
ensure_owner_saved
|
11
|
+
|
12
|
+
doc.send("#{as_type_name}=", @owner.class.name)
|
13
|
+
doc.send("#{as_id_name}=", @owner.id)
|
14
|
+
|
15
|
+
doc
|
16
|
+
end
|
17
|
+
|
18
|
+
def as_type_name
|
19
|
+
@as_type_name ||= @association.options[:as].to_s + "_type"
|
20
|
+
end
|
21
|
+
|
22
|
+
def as_id_name
|
23
|
+
@as_id_name ||= @association.options[:as].to_s + "_id"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/mongomapper/document.rb
CHANGED
@@ -59,13 +59,13 @@ module MongoMapper
|
|
59
59
|
|
60
60
|
def find_by_id(id)
|
61
61
|
criteria = FinderOptions.to_mongo_criteria(:_id => id)
|
62
|
-
if doc = collection.
|
62
|
+
if doc = collection.find_one(criteria)
|
63
63
|
new(doc)
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
67
|
def count(conditions={})
|
68
|
-
collection.
|
68
|
+
collection.find(FinderOptions.to_mongo_criteria(conditions)).count
|
69
69
|
end
|
70
70
|
|
71
71
|
def create(*docs)
|
@@ -320,7 +320,7 @@ module MongoMapper
|
|
320
320
|
|
321
321
|
def assign_id
|
322
322
|
if read_attribute(:_id).blank?
|
323
|
-
write_attribute(:_id,
|
323
|
+
write_attribute(:_id, Mongo::ObjectID.new.to_s)
|
324
324
|
end
|
325
325
|
end
|
326
326
|
|
@@ -84,7 +84,7 @@ module MongoMapper
|
|
84
84
|
|
85
85
|
private
|
86
86
|
def accessors_module
|
87
|
-
if const_defined?('MongoMapperKeys')
|
87
|
+
if const_defined?('MongoMapperKeys')
|
88
88
|
const_get 'MongoMapperKeys'
|
89
89
|
else
|
90
90
|
const_set 'MongoMapperKeys', Module.new
|
@@ -94,7 +94,7 @@ module MongoMapper
|
|
94
94
|
def create_accessors_for(key)
|
95
95
|
accessors_module.module_eval <<-end_eval
|
96
96
|
def #{key.name}
|
97
|
-
read_attribute(
|
97
|
+
read_attribute(:'#{key.name}')
|
98
98
|
end
|
99
99
|
|
100
100
|
def #{key.name}_before_typecast
|
@@ -160,10 +160,14 @@ module MongoMapper
|
|
160
160
|
end
|
161
161
|
|
162
162
|
self.attributes = attrs
|
163
|
+
|
164
|
+
if respond_to?(:_type=) && self['_type'].blank?
|
165
|
+
self._type = self.class.name
|
166
|
+
end
|
163
167
|
end
|
164
168
|
|
165
169
|
if self.class.embeddable? && read_attribute(:_id).blank?
|
166
|
-
write_attribute :_id,
|
170
|
+
write_attribute :_id, Mongo::ObjectID.new.to_s
|
167
171
|
end
|
168
172
|
end
|
169
173
|
|
data/mongomapper.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongomapper}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["John Nunemaker"]
|
12
|
-
s.date = %q{2009-08-
|
12
|
+
s.date = %q{2009-08-29}
|
13
13
|
s.default_executable = %q{mmconsole}
|
14
14
|
s.email = %q{nunemaker@gmail.com}
|
15
15
|
s.executables = ["mmconsole"]
|
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
|
|
30
30
|
"lib/mongomapper/associations/base.rb",
|
31
31
|
"lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb",
|
32
32
|
"lib/mongomapper/associations/belongs_to_proxy.rb",
|
33
|
+
"lib/mongomapper/associations/many_documents_as_proxy.rb",
|
33
34
|
"lib/mongomapper/associations/many_documents_proxy.rb",
|
34
35
|
"lib/mongomapper/associations/many_embedded_polymorphic_proxy.rb",
|
35
36
|
"lib/mongomapper/associations/many_embedded_proxy.rb",
|
@@ -55,6 +56,7 @@ Gem::Specification.new do |s|
|
|
55
56
|
"test/NOTE_ON_TESTING",
|
56
57
|
"test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
|
57
58
|
"test/functional/associations/test_belongs_to_proxy.rb",
|
59
|
+
"test/functional/associations/test_many_documents_as_proxy.rb",
|
58
60
|
"test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
|
59
61
|
"test/functional/associations/test_many_embedded_proxy.rb",
|
60
62
|
"test/functional/associations/test_many_polymorphic_proxy.rb",
|
@@ -89,6 +91,7 @@ Gem::Specification.new do |s|
|
|
89
91
|
s.test_files = [
|
90
92
|
"test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
|
91
93
|
"test/functional/associations/test_belongs_to_proxy.rb",
|
94
|
+
"test/functional/associations/test_many_documents_as_proxy.rb",
|
92
95
|
"test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
|
93
96
|
"test/functional/associations/test_many_embedded_proxy.rb",
|
94
97
|
"test/functional/associations/test_many_polymorphic_proxy.rb",
|
@@ -121,20 +124,20 @@ Gem::Specification.new do |s|
|
|
121
124
|
|
122
125
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
123
126
|
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
124
|
-
s.add_runtime_dependency(%q<mongodb-mongo>, ["= 0.
|
127
|
+
s.add_runtime_dependency(%q<mongodb-mongo>, ["= 0.14"])
|
125
128
|
s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.7.2"])
|
126
129
|
s.add_development_dependency(%q<mocha>, ["= 0.9.4"])
|
127
130
|
s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
128
131
|
else
|
129
132
|
s.add_dependency(%q<activesupport>, [">= 0"])
|
130
|
-
s.add_dependency(%q<mongodb-mongo>, ["= 0.
|
133
|
+
s.add_dependency(%q<mongodb-mongo>, ["= 0.14"])
|
131
134
|
s.add_dependency(%q<jnunemaker-validatable>, ["= 1.7.2"])
|
132
135
|
s.add_dependency(%q<mocha>, ["= 0.9.4"])
|
133
136
|
s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
134
137
|
end
|
135
138
|
else
|
136
139
|
s.add_dependency(%q<activesupport>, [">= 0"])
|
137
|
-
s.add_dependency(%q<mongodb-mongo>, ["= 0.
|
140
|
+
s.add_dependency(%q<mongodb-mongo>, ["= 0.14"])
|
138
141
|
s.add_dependency(%q<jnunemaker-validatable>, ["= 1.7.2"])
|
139
142
|
s.add_dependency(%q<mocha>, ["= 0.9.4"])
|
140
143
|
s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
@@ -0,0 +1,253 @@
|
|
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 :first" do
|
166
|
+
should "work" do
|
167
|
+
lambda {@post.comments.find(:first)}.should_not raise_error
|
168
|
+
end
|
169
|
+
|
170
|
+
should "work with conditions" do
|
171
|
+
comment = @post.comments.find(:first, :conditions => {:body => 'comment2'})
|
172
|
+
comment.body.should == 'comment2'
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
context "with #first" do
|
177
|
+
should "work" do
|
178
|
+
@post.comments.first.should == @comment1
|
179
|
+
end
|
180
|
+
|
181
|
+
should "work with conditions" do
|
182
|
+
comment = @post.comments.first(:conditions => {:body => 'comment2'}, :order => 'body desc')
|
183
|
+
comment.should == @comment2
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
context "with :last" do
|
188
|
+
should "work" do
|
189
|
+
@post.comments.find(:last, :order => 'created_at asc').should == @comment2
|
190
|
+
end
|
191
|
+
|
192
|
+
should "work with conditions" do
|
193
|
+
post = @post.comments.find(:last, :conditions => {:body => 'comment1'})
|
194
|
+
post.body.should == 'comment1'
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
context "with #last" do
|
199
|
+
should "work" do
|
200
|
+
@post.comments.last.should == @comment2
|
201
|
+
end
|
202
|
+
|
203
|
+
should "work with conditions" do
|
204
|
+
comment = @post.comments.last(:conditions => {:body => 'comment1'})
|
205
|
+
comment.should == @comment1
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
context "with one id" do
|
210
|
+
should "work for id in association" do
|
211
|
+
@post.comments.find(@comment2.id).should == @comment2
|
212
|
+
end
|
213
|
+
|
214
|
+
should "not work for id not in association" do
|
215
|
+
lambda {
|
216
|
+
@post.comments.find(@comment5.id)
|
217
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
context "with multiple ids" do
|
222
|
+
should "work for ids in association" do
|
223
|
+
posts = @post.comments.find(@comment1.id, @comment2.id)
|
224
|
+
posts.should == [@comment1, @comment2]
|
225
|
+
end
|
226
|
+
|
227
|
+
should "not work for ids not in association" do
|
228
|
+
lambda {
|
229
|
+
@post.comments.find(@comment1.id, @comment2.id, @comment4.id)
|
230
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
context "with #paginate" do
|
235
|
+
setup do
|
236
|
+
@comments = @post2.comments.paginate(:per_page => 2, :page => 1, :order => 'created_at asc')
|
237
|
+
end
|
238
|
+
|
239
|
+
should "return total pages" do
|
240
|
+
@comments.total_pages.should == 2
|
241
|
+
end
|
242
|
+
|
243
|
+
should "return total entries" do
|
244
|
+
@comments.total_entries.should == 3
|
245
|
+
end
|
246
|
+
|
247
|
+
should "return the subject" do
|
248
|
+
@comments.should include(@comment4)
|
249
|
+
@comments.should include(@comment5)
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
253
|
+
end
|
@@ -26,7 +26,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
26
26
|
|
27
27
|
context "Loading a document from the database with keys that are not defined" do
|
28
28
|
setup do
|
29
|
-
@id =
|
29
|
+
@id = Mongo::ObjectID.new.to_s
|
30
30
|
@document.collection.insert({
|
31
31
|
:_id => @id,
|
32
32
|
:first_name => 'John',
|
data/test/models.rb
CHANGED
@@ -1,6 +1,30 @@
|
|
1
|
+
class Post
|
2
|
+
include MongoMapper::Document
|
3
|
+
|
4
|
+
key :title, String
|
5
|
+
key :body, String
|
6
|
+
|
7
|
+
has_many :comments, :as => :commentable, :class_name => 'PostComment'
|
8
|
+
|
9
|
+
timestamps!
|
10
|
+
end
|
11
|
+
|
12
|
+
class PostComment
|
13
|
+
include MongoMapper::Document
|
14
|
+
|
15
|
+
key :username, String, :default => 'Anonymous'
|
16
|
+
key :body, String
|
17
|
+
|
18
|
+
key :commentable_id, String
|
19
|
+
key :commentable_type, String
|
20
|
+
belongs_to :commentable, :polymorphic => true
|
21
|
+
|
22
|
+
timestamps!
|
23
|
+
end
|
24
|
+
|
1
25
|
class Address
|
2
26
|
include MongoMapper::EmbeddedDocument
|
3
|
-
|
27
|
+
|
4
28
|
key :address, String
|
5
29
|
key :city, String
|
6
30
|
key :state, String
|
@@ -9,29 +33,35 @@ end
|
|
9
33
|
|
10
34
|
class Message
|
11
35
|
include MongoMapper::Document
|
12
|
-
|
36
|
+
|
13
37
|
key :body, String
|
14
38
|
key :position, Integer
|
15
39
|
key :_type, String
|
16
40
|
key :room_id, String
|
17
|
-
|
41
|
+
|
18
42
|
belongs_to :room
|
19
43
|
end
|
20
44
|
|
45
|
+
class Answer
|
46
|
+
include MongoMapper::Document
|
47
|
+
|
48
|
+
key :body, String
|
49
|
+
end
|
50
|
+
|
21
51
|
class Enter < Message; end
|
22
52
|
class Exit < Message; end
|
23
53
|
class Chat < Message; end
|
24
54
|
|
25
55
|
class Room
|
26
56
|
include MongoMapper::Document
|
27
|
-
|
57
|
+
|
28
58
|
key :name, String
|
29
59
|
many :messages, :polymorphic => true
|
30
60
|
end
|
31
61
|
|
32
62
|
class Project
|
33
63
|
include MongoMapper::Document
|
34
|
-
|
64
|
+
|
35
65
|
key :name, String
|
36
66
|
many :statuses
|
37
67
|
many :addresses
|
@@ -39,13 +69,13 @@ end
|
|
39
69
|
|
40
70
|
class Status
|
41
71
|
include MongoMapper::Document
|
42
|
-
|
72
|
+
|
43
73
|
key :project_id, String
|
44
74
|
key :target_id, String
|
45
75
|
key :target_type, String
|
46
76
|
key :name, String
|
47
77
|
key :position, Integer
|
48
|
-
|
78
|
+
|
49
79
|
belongs_to :project
|
50
80
|
belongs_to :target, :polymorphic => true
|
51
81
|
end
|
@@ -63,23 +93,23 @@ end
|
|
63
93
|
|
64
94
|
class Person
|
65
95
|
include MongoMapper::EmbeddedDocument
|
66
|
-
|
96
|
+
|
67
97
|
key :name, String
|
68
98
|
key :child, Person
|
69
|
-
|
99
|
+
|
70
100
|
many :pets
|
71
101
|
end
|
72
102
|
|
73
103
|
class Pet
|
74
104
|
include MongoMapper::EmbeddedDocument
|
75
|
-
|
105
|
+
|
76
106
|
key :name, String
|
77
107
|
key :species, String
|
78
108
|
end
|
79
109
|
|
80
110
|
class Media
|
81
111
|
include MongoMapper::EmbeddedDocument
|
82
|
-
|
112
|
+
|
83
113
|
key :_type, String
|
84
114
|
key :file, String
|
85
115
|
end
|
@@ -99,40 +129,40 @@ end
|
|
99
129
|
|
100
130
|
class Catalog
|
101
131
|
include MongoMapper::Document
|
102
|
-
|
132
|
+
|
103
133
|
many :medias, :polymorphic => true
|
104
134
|
end
|
105
135
|
|
106
136
|
module TrModels
|
107
137
|
class Transport
|
108
138
|
include MongoMapper::EmbeddedDocument
|
109
|
-
|
139
|
+
|
110
140
|
key :_type, String
|
111
141
|
key :license_plate, String
|
112
142
|
end
|
113
143
|
|
114
144
|
class Car < TrModels::Transport
|
115
145
|
include MongoMapper::EmbeddedDocument
|
116
|
-
|
146
|
+
|
117
147
|
key :model, String
|
118
148
|
key :year, Integer
|
119
149
|
end
|
120
150
|
|
121
151
|
class Bus < TrModels::Transport
|
122
152
|
include MongoMapper::EmbeddedDocument
|
123
|
-
|
153
|
+
|
124
154
|
key :max_passengers, Integer
|
125
155
|
end
|
126
156
|
|
127
157
|
class Ambulance < TrModels::Transport
|
128
158
|
include MongoMapper::EmbeddedDocument
|
129
|
-
|
159
|
+
|
130
160
|
key :icu, Boolean
|
131
161
|
end
|
132
162
|
|
133
163
|
class Fleet
|
134
164
|
include MongoMapper::Document
|
135
|
-
|
165
|
+
|
136
166
|
many :transports, :polymorphic => true, :class_name => "TrModels::Transport"
|
137
167
|
key :name, String
|
138
168
|
end
|
@@ -1,6 +1,11 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class JsonSerializationTest < Test::Unit::TestCase
|
4
|
+
class Tag
|
5
|
+
include MongoMapper::EmbeddedDocument
|
6
|
+
key :name, String
|
7
|
+
end
|
8
|
+
|
4
9
|
class Contact
|
5
10
|
include MongoMapper::Document
|
6
11
|
key :name, String
|
@@ -8,6 +13,8 @@ class JsonSerializationTest < Test::Unit::TestCase
|
|
8
13
|
key :created_at, Time
|
9
14
|
key :awesome, Boolean
|
10
15
|
key :preferences, Hash
|
16
|
+
|
17
|
+
many :tags, :class_name => 'JsonSerializationTest::Tag'
|
11
18
|
end
|
12
19
|
|
13
20
|
def setup
|
@@ -136,12 +143,20 @@ class JsonSerializationTest < Test::Unit::TestCase
|
|
136
143
|
end
|
137
144
|
|
138
145
|
should "allow attribute filtering with only" do
|
139
|
-
|
146
|
+
json = @contacts.to_json(:only => :name)
|
147
|
+
assert_match %r{\{"name":"David"\}}, json
|
148
|
+
assert_match %r{\{"name":"Mary"\}}, json
|
140
149
|
end
|
141
150
|
|
142
151
|
should "allow attribute filtering with except" do
|
143
152
|
json = @contacts.to_json(:except => [:name, :preferences, :awesome, :created_at, :updated_at])
|
144
|
-
|
153
|
+
assert_match %r{"age":39}, json
|
154
|
+
assert_match %r{"age":14}, json
|
155
|
+
assert_no_match %r{"name":}, json
|
156
|
+
assert_no_match %r{"preferences":}, json
|
157
|
+
assert_no_match %r{"awesome":}, json
|
158
|
+
assert_no_match %r{"created_at":}, json
|
159
|
+
assert_no_match %r{"updated_at":}, json
|
145
160
|
end
|
146
161
|
end
|
147
162
|
|
@@ -150,8 +165,25 @@ class JsonSerializationTest < Test::Unit::TestCase
|
|
150
165
|
1 => Contact.new(:name => 'David', :age => 39),
|
151
166
|
2 => Contact.new(:name => 'Mary', :age => 14)
|
152
167
|
}
|
153
|
-
|
154
|
-
|
168
|
+
json = contacts.to_json(:only => [1, :name])
|
169
|
+
assert_match %r{"1":}, json
|
170
|
+
assert_match %r{\{"name":"David"\}}, json
|
171
|
+
assert_no_match %r{"2":}, json
|
172
|
+
end
|
173
|
+
|
174
|
+
should "include embedded attributes" do
|
175
|
+
contact = Contact.new(:name => 'John', :age => 27)
|
176
|
+
contact.tags = [Tag.new(:name => 'awesome'), Tag.new(:name => 'ruby')]
|
177
|
+
json = contact.to_json
|
178
|
+
assert_match %r{"tags":}, json
|
179
|
+
assert_match %r{"name":"awesome"}, json
|
180
|
+
assert_match %r{"name":"ruby"}, json
|
155
181
|
end
|
156
182
|
|
183
|
+
should "include dynamic attributes" do
|
184
|
+
contact = Contact.new(:name => 'John', :age => 27, :foo => 'bar')
|
185
|
+
contact['smell'] = 'stinky'
|
186
|
+
json = contact.to_json
|
187
|
+
assert_match %r{"smell":"stinky"}, json
|
188
|
+
end
|
157
189
|
end
|
data/test/unit/test_document.rb
CHANGED
@@ -18,11 +18,11 @@ class DocumentTest < Test::Unit::TestCase
|
|
18
18
|
end
|
19
19
|
|
20
20
|
should "have a connection" do
|
21
|
-
@document.connection.should be_instance_of(
|
21
|
+
@document.connection.should be_instance_of(Mongo::Connection)
|
22
22
|
end
|
23
23
|
|
24
24
|
should "allow setting different connection without affecting the default" do
|
25
|
-
conn =
|
25
|
+
conn = Mongo::Connection.new
|
26
26
|
@document.connection conn
|
27
27
|
@document.connection.should == conn
|
28
28
|
@document.connection.should_not == MongoMapper.connection
|
@@ -43,13 +43,13 @@ class DocumentTest < Test::Unit::TestCase
|
|
43
43
|
include MongoMapper::Document
|
44
44
|
end
|
45
45
|
|
46
|
-
Item.collection.should be_instance_of(
|
46
|
+
Item.collection.should be_instance_of(Mongo::Collection)
|
47
47
|
Item.collection.name.should == 'items'
|
48
48
|
end
|
49
49
|
|
50
50
|
should "allow setting the collection name" do
|
51
51
|
@document.collection('foobar')
|
52
|
-
@document.collection.should be_instance_of(
|
52
|
+
@document.collection.should be_instance_of(Mongo::Collection)
|
53
53
|
@document.collection.name.should == 'foobar'
|
54
54
|
end
|
55
55
|
end # Document class
|
@@ -282,6 +282,27 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
282
282
|
}.should_not raise_error
|
283
283
|
end
|
284
284
|
end
|
285
|
+
|
286
|
+
context "initialized when _type key present" do
|
287
|
+
setup do
|
288
|
+
::FooBar = Class.new do
|
289
|
+
include MongoMapper::EmbeddedDocument
|
290
|
+
key :_type, String
|
291
|
+
end
|
292
|
+
end
|
293
|
+
|
294
|
+
teardown do
|
295
|
+
Object.send(:remove_const, :FooBar)
|
296
|
+
end
|
297
|
+
|
298
|
+
should "set _type to class name" do
|
299
|
+
FooBar.new._type.should == 'FooBar'
|
300
|
+
end
|
301
|
+
|
302
|
+
should "not change _type if already set" do
|
303
|
+
FooBar.new(:_type => 'Foo')._type.should == 'Foo'
|
304
|
+
end
|
305
|
+
end
|
285
306
|
|
286
307
|
context "mass assigning keys" do
|
287
308
|
should "update values for keys provided" do
|
@@ -4,19 +4,19 @@ class Address; end
|
|
4
4
|
|
5
5
|
class MongoMapperTest < Test::Unit::TestCase
|
6
6
|
should "be able to write and read connection" do
|
7
|
-
conn =
|
7
|
+
conn = Mongo::Connection.new
|
8
8
|
MongoMapper.connection = conn
|
9
9
|
MongoMapper.connection.should == conn
|
10
10
|
end
|
11
11
|
|
12
12
|
should "default connection to new mongo ruby driver" do
|
13
13
|
MongoMapper.connection = nil
|
14
|
-
MongoMapper.connection.should be_instance_of(
|
14
|
+
MongoMapper.connection.should be_instance_of(Mongo::Connection)
|
15
15
|
end
|
16
16
|
|
17
17
|
should "be able to write and read default database" do
|
18
18
|
MongoMapper.database = DefaultDatabase
|
19
|
-
MongoMapper.database.should be_instance_of(
|
19
|
+
MongoMapper.database.should be_instance_of(Mongo::DB)
|
20
20
|
MongoMapper.database.name.should == DefaultDatabase
|
21
21
|
end
|
22
22
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: djsun-mongomapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Nunemaker
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-08-
|
12
|
+
date: 2009-08-29 00:00:00 -07:00
|
13
13
|
default_executable: mmconsole
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -30,7 +30,7 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.
|
33
|
+
version: "0.14"
|
34
34
|
version:
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: jnunemaker-validatable
|
@@ -84,6 +84,7 @@ files:
|
|
84
84
|
- lib/mongomapper/associations/base.rb
|
85
85
|
- lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb
|
86
86
|
- lib/mongomapper/associations/belongs_to_proxy.rb
|
87
|
+
- lib/mongomapper/associations/many_documents_as_proxy.rb
|
87
88
|
- lib/mongomapper/associations/many_documents_proxy.rb
|
88
89
|
- lib/mongomapper/associations/many_embedded_polymorphic_proxy.rb
|
89
90
|
- lib/mongomapper/associations/many_embedded_proxy.rb
|
@@ -109,6 +110,7 @@ files:
|
|
109
110
|
- test/NOTE_ON_TESTING
|
110
111
|
- test/functional/associations/test_belongs_to_polymorphic_proxy.rb
|
111
112
|
- test/functional/associations/test_belongs_to_proxy.rb
|
113
|
+
- test/functional/associations/test_many_documents_as_proxy.rb
|
112
114
|
- test/functional/associations/test_many_embedded_polymorphic_proxy.rb
|
113
115
|
- test/functional/associations/test_many_embedded_proxy.rb
|
114
116
|
- test/functional/associations/test_many_polymorphic_proxy.rb
|
@@ -135,7 +137,6 @@ files:
|
|
135
137
|
- test/unit/test_validations.rb
|
136
138
|
has_rdoc: false
|
137
139
|
homepage: http://github.com/jnunemaker/mongomapper
|
138
|
-
licenses:
|
139
140
|
post_install_message:
|
140
141
|
rdoc_options:
|
141
142
|
- --charset=UTF-8
|
@@ -156,13 +157,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
156
157
|
requirements: []
|
157
158
|
|
158
159
|
rubyforge_project: mongomapper
|
159
|
-
rubygems_version: 1.
|
160
|
+
rubygems_version: 1.2.0
|
160
161
|
signing_key:
|
161
162
|
specification_version: 3
|
162
163
|
summary: Awesome gem for modeling your domain and storing it in mongo
|
163
164
|
test_files:
|
164
165
|
- test/functional/associations/test_belongs_to_polymorphic_proxy.rb
|
165
166
|
- test/functional/associations/test_belongs_to_proxy.rb
|
167
|
+
- test/functional/associations/test_many_documents_as_proxy.rb
|
166
168
|
- test/functional/associations/test_many_embedded_polymorphic_proxy.rb
|
167
169
|
- test/functional/associations/test_many_embedded_proxy.rb
|
168
170
|
- test/functional/associations/test_many_polymorphic_proxy.rb
|