mongo_mapper 0.12.0 → 0.13.0.beta1
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.
- checksums.yaml +7 -0
- data/README.rdoc +35 -13
- data/bin/mmconsole +1 -1
- data/lib/mongo_mapper.rb +4 -0
- data/lib/mongo_mapper/connection.rb +17 -6
- data/lib/mongo_mapper/document.rb +1 -0
- data/lib/mongo_mapper/exceptions.rb +4 -1
- data/lib/mongo_mapper/extensions/binary.rb +1 -1
- data/lib/mongo_mapper/extensions/boolean.rb +20 -23
- data/lib/mongo_mapper/extensions/date.rb +3 -3
- data/lib/mongo_mapper/extensions/integer.rb +5 -1
- data/lib/mongo_mapper/extensions/kernel.rb +2 -0
- data/lib/mongo_mapper/extensions/ordered_hash.rb +23 -0
- data/lib/mongo_mapper/extensions/string.rb +2 -2
- data/lib/mongo_mapper/extensions/time.rb +7 -5
- data/lib/mongo_mapper/middleware/identity_map.rb +3 -4
- data/lib/mongo_mapper/plugins.rb +1 -1
- data/lib/mongo_mapper/plugins/associations.rb +11 -5
- data/lib/mongo_mapper/plugins/associations/base.rb +5 -3
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +0 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +8 -8
- data/lib/mongo_mapper/plugins/associations/collection.rb +2 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +32 -7
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +2 -2
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +12 -12
- data/lib/mongo_mapper/plugins/associations/proxy.rb +5 -1
- data/lib/mongo_mapper/plugins/associations/single_association.rb +6 -6
- data/lib/mongo_mapper/plugins/clone.rb +4 -2
- data/lib/mongo_mapper/plugins/dirty.rb +22 -21
- data/lib/mongo_mapper/plugins/document.rb +4 -4
- data/lib/mongo_mapper/plugins/dumpable.rb +22 -0
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +58 -9
- data/lib/mongo_mapper/plugins/identity_map.rb +42 -32
- data/lib/mongo_mapper/plugins/keys.rb +133 -54
- data/lib/mongo_mapper/plugins/keys/key.rb +68 -22
- data/lib/mongo_mapper/plugins/modifiers.rb +26 -19
- data/lib/mongo_mapper/plugins/persistence.rb +15 -5
- data/lib/mongo_mapper/plugins/querying.rb +15 -40
- data/lib/mongo_mapper/plugins/querying/{decorator.rb → decorated_plucky_query.rb} +24 -4
- data/lib/mongo_mapper/plugins/rails.rb +22 -2
- data/lib/mongo_mapper/plugins/safe.rb +8 -5
- data/lib/mongo_mapper/plugins/sci.rb +26 -4
- data/lib/mongo_mapper/plugins/scopes.rb +5 -4
- data/lib/mongo_mapper/plugins/timestamps.rb +11 -4
- data/lib/mongo_mapper/plugins/validations.rb +1 -1
- data/lib/mongo_mapper/utils.rb +12 -0
- data/lib/mongo_mapper/version.rb +1 -1
- data/lib/rails/generators/mongo_mapper/config/config_generator.rb +20 -7
- data/lib/rails/generators/mongo_mapper/config/templates/mongo.yml +6 -0
- data/lib/rails/generators/mongo_mapper/model/model_generator.rb +18 -1
- data/lib/rails/generators/mongo_mapper/model/templates/model.rb +9 -5
- data/{test/functional/test_accessible.rb → spec/functional/accessible_spec.rb} +29 -29
- data/{test/functional/associations/test_belongs_to_polymorphic_proxy.rb → spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb} +10 -10
- data/{test/functional/associations/test_belongs_to_proxy.rb → spec/functional/associations/belongs_to_proxy_spec.rb} +82 -64
- data/{test/functional/associations/test_in_array_proxy.rb → spec/functional/associations/in_array_proxy_spec.rb} +68 -68
- data/{test/functional/associations/test_many_documents_as_proxy.rb → spec/functional/associations/many_documents_as_proxy_spec.rb} +37 -38
- data/{test/functional/associations/test_many_documents_proxy.rb → spec/functional/associations/many_documents_proxy_spec.rb} +233 -146
- data/{test/functional/associations/test_many_embedded_polymorphic_proxy.rb → spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb} +19 -20
- data/{test/functional/associations/test_many_embedded_proxy.rb → spec/functional/associations/many_embedded_proxy_spec.rb} +23 -24
- data/{test/functional/associations/test_many_polymorphic_proxy.rb → spec/functional/associations/many_polymorphic_proxy_spec.rb} +45 -46
- data/{test/functional/associations/test_one_as_proxy.rb → spec/functional/associations/one_as_proxy_spec.rb} +75 -77
- data/{test/functional/associations/test_one_embedded_polymorphic_proxy.rb → spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb} +31 -32
- data/{test/functional/associations/test_one_embedded_proxy.rb → spec/functional/associations/one_embedded_proxy_spec.rb} +10 -10
- data/{test/functional/associations/test_one_proxy.rb → spec/functional/associations/one_proxy_spec.rb} +125 -102
- data/spec/functional/associations_spec.rb +48 -0
- data/{test/functional/test_binary.rb → spec/functional/binary_spec.rb} +6 -6
- data/spec/functional/caching_spec.rb +75 -0
- data/{test/functional/test_callbacks.rb → spec/functional/callbacks_spec.rb} +84 -26
- data/{test/functional/test_dirty.rb → spec/functional/dirty_spec.rb} +57 -42
- data/{test/functional/test_document.rb → spec/functional/document_spec.rb} +52 -52
- data/spec/functional/dumpable_spec.rb +24 -0
- data/{test/functional/test_dynamic_querying.rb → spec/functional/dynamic_querying_spec.rb} +14 -14
- data/{test/functional/test_embedded_document.rb → spec/functional/embedded_document_spec.rb} +51 -42
- data/{test/functional/test_equality.rb → spec/functional/equality_spec.rb} +4 -4
- data/spec/functional/extensions_spec.rb +16 -0
- data/{test/functional/test_identity_map.rb → spec/functional/identity_map_spec.rb} +73 -61
- data/spec/functional/indexes_spec.rb +48 -0
- data/spec/functional/keys_spec.rb +224 -0
- data/{test/functional/test_logger.rb → spec/functional/logger_spec.rb} +6 -6
- data/spec/functional/modifiers_spec.rb +550 -0
- data/spec/functional/pagination_spec.rb +89 -0
- data/spec/functional/protected_spec.rb +199 -0
- data/spec/functional/querying_spec.rb +1003 -0
- data/spec/functional/rails_spec.rb +55 -0
- data/spec/functional/safe_spec.rb +163 -0
- data/{test/functional/test_sci.rb → spec/functional/sci_spec.rb} +123 -34
- data/{test/functional/test_scopes.rb → spec/functional/scopes_spec.rb} +59 -26
- data/spec/functional/timestamps_spec.rb +97 -0
- data/{test/functional/test_touch.rb → spec/functional/touch_spec.rb} +13 -13
- data/spec/functional/userstamps_spec.rb +46 -0
- data/{test/functional/test_validations.rb → spec/functional/validations_spec.rb} +64 -64
- data/spec/spec_helper.rb +81 -0
- data/spec/support/matchers.rb +24 -0
- data/{test → spec/support}/models.rb +1 -6
- data/spec/unit/associations/base_spec.rb +146 -0
- data/spec/unit/associations/belongs_to_association_spec.rb +30 -0
- data/spec/unit/associations/many_association_spec.rb +64 -0
- data/spec/unit/associations/one_association_spec.rb +48 -0
- data/{test/unit/associations/test_proxy.rb → spec/unit/associations/proxy_spec.rb} +21 -21
- data/{test/unit/test_clone.rb → spec/unit/clone_spec.rb} +21 -11
- data/spec/unit/config_generator_spec.rb +24 -0
- data/{test/unit/test_document.rb → spec/unit/document_spec.rb} +42 -42
- data/{test/unit/test_dynamic_finder.rb → spec/unit/dynamic_finder_spec.rb} +28 -28
- data/{test/unit/test_embedded_document.rb → spec/unit/embedded_document_spec.rb} +102 -108
- data/{test/unit/test_equality.rb → spec/unit/equality_spec.rb} +7 -7
- data/{test/unit/test_exceptions.rb → spec/unit/exceptions_spec.rb} +3 -3
- data/{test/unit/test_extensions.rb → spec/unit/extensions_spec.rb} +85 -71
- data/spec/unit/identity_map_middleware_spec.rb +134 -0
- data/{test/unit/test_inspect.rb → spec/unit/inspect_spec.rb} +8 -8
- data/{test/unit/test_key.rb → spec/unit/key_spec.rb} +82 -52
- data/spec/unit/keys_spec.rb +155 -0
- data/spec/unit/model_generator_spec.rb +47 -0
- data/spec/unit/mongo_mapper_spec.rb +184 -0
- data/spec/unit/pagination_spec.rb +11 -0
- data/{test/unit/test_plugins.rb → spec/unit/plugins_spec.rb} +14 -14
- data/spec/unit/rails_compatibility_spec.rb +40 -0
- data/{test/unit/test_rails_reflect_on_association.rb → spec/unit/rails_reflect_on_association_spec.rb} +9 -9
- data/{test/unit/test_rails.rb → spec/unit/rails_spec.rb} +31 -31
- data/spec/unit/serialization_spec.rb +169 -0
- data/spec/unit/serializers/json_serializer_spec.rb +218 -0
- data/spec/unit/serializers/xml_serializer_spec.rb +198 -0
- data/{test/unit/test_time_zones.rb → spec/unit/time_zones_spec.rb} +8 -8
- data/{test/unit/test_translation.rb → spec/unit/translation_spec.rb} +6 -6
- data/{test/unit/test_validations.rb → spec/unit/validations_spec.rb} +72 -59
- metadata +199 -179
- data/test/_NOTE_ON_TESTING +0 -1
- data/test/functional/test_associations.rb +0 -46
- data/test/functional/test_caching.rb +0 -77
- data/test/functional/test_indexes.rb +0 -50
- data/test/functional/test_modifiers.rb +0 -537
- data/test/functional/test_pagination.rb +0 -91
- data/test/functional/test_protected.rb +0 -201
- data/test/functional/test_querying.rb +0 -935
- data/test/functional/test_safe.rb +0 -76
- data/test/functional/test_timestamps.rb +0 -62
- data/test/functional/test_userstamps.rb +0 -44
- data/test/support/railtie.rb +0 -4
- data/test/support/railtie/autoloaded.rb +0 -2
- data/test/support/railtie/not_autoloaded.rb +0 -3
- data/test/support/railtie/parent.rb +0 -3
- data/test/test_active_model_lint.rb +0 -18
- data/test/test_helper.rb +0 -93
- data/test/unit/associations/test_base.rb +0 -146
- data/test/unit/associations/test_belongs_to_association.rb +0 -29
- data/test/unit/associations/test_many_association.rb +0 -63
- data/test/unit/associations/test_one_association.rb +0 -47
- data/test/unit/serializers/test_json_serializer.rb +0 -216
- data/test/unit/serializers/test_xml_serializer.rb +0 -196
- data/test/unit/test_identity_map_middleware.rb +0 -132
- data/test/unit/test_keys.rb +0 -65
- data/test/unit/test_mongo_mapper.rb +0 -157
- data/test/unit/test_pagination.rb +0 -11
- data/test/unit/test_rails_compatibility.rb +0 -38
- data/test/unit/test_serialization.rb +0 -166
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Rails Compatibility" do
|
4
|
+
module EmbeddedDocuments
|
5
|
+
class Item
|
6
|
+
include MongoMapper::EmbeddedDocument
|
7
|
+
key :for_all, String
|
8
|
+
end
|
9
|
+
|
10
|
+
class FirstItem < Item
|
11
|
+
key :first_only, String
|
12
|
+
many :second_items
|
13
|
+
end
|
14
|
+
|
15
|
+
class SecondItem < Item
|
16
|
+
key :second_only, String
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "EmbeddedDocument" do
|
21
|
+
it "should alias many to has_many" do
|
22
|
+
EmbeddedDocuments::FirstItem.should respond_to(:has_many)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should alias one to has_one" do
|
26
|
+
EmbeddedDocuments::FirstItem.should respond_to(:has_one)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should have column names" do
|
30
|
+
EmbeddedDocuments::Item.column_names.sort.should == ['_id', '_type', 'for_all']
|
31
|
+
EmbeddedDocuments::FirstItem.column_names.sort.should == ['_id', '_type', 'first_only', 'for_all']
|
32
|
+
EmbeddedDocuments::SecondItem.column_names.sort.should == ['_id', '_type', 'for_all', 'second_only']
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should alias new to new_record?" do
|
36
|
+
instance = EmbeddedDocuments::Item.new
|
37
|
+
instance.new_record?.should == instance.new?
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
module ReflectOnAssociationTestModels
|
4
4
|
class Tree
|
@@ -49,9 +49,9 @@ module ReflectOnAssociationTestModels
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
|
52
|
+
describe "ReflectOnAssociation" do
|
53
53
|
context "one-to-many association" do
|
54
|
-
should
|
54
|
+
it "should return :has_many association for Tree#birds" do
|
55
55
|
association = ReflectOnAssociationTestModels::Tree.reflect_on_association(:birds)
|
56
56
|
association.klass.should == ReflectOnAssociationTestModels::Bird
|
57
57
|
association.macro.should == :has_many
|
@@ -59,7 +59,7 @@ class ReflectOnAssociationTest < Test::Unit::TestCase
|
|
59
59
|
association.options.should == {}
|
60
60
|
end
|
61
61
|
|
62
|
-
should
|
62
|
+
it "should return :belongs_to association for Bird#tree" do
|
63
63
|
association = ReflectOnAssociationTestModels::Bird.reflect_on_association(:tree)
|
64
64
|
association.klass.should == ReflectOnAssociationTestModels::Tree
|
65
65
|
association.macro.should == :belongs_to
|
@@ -69,7 +69,7 @@ class ReflectOnAssociationTest < Test::Unit::TestCase
|
|
69
69
|
end
|
70
70
|
|
71
71
|
context "many-to-many association" do
|
72
|
-
should
|
72
|
+
it "should return :has_many association for Book#authors" do
|
73
73
|
association = ReflectOnAssociationTestModels::Book.reflect_on_association(:authors)
|
74
74
|
association.klass.should == ReflectOnAssociationTestModels::Author
|
75
75
|
association.macro.should == :has_many
|
@@ -79,7 +79,7 @@ class ReflectOnAssociationTest < Test::Unit::TestCase
|
|
79
79
|
end
|
80
80
|
|
81
81
|
context "one-to-one association" do
|
82
|
-
should
|
82
|
+
it "should return :has_one association for Employee#desk" do
|
83
83
|
association = ReflectOnAssociationTestModels::Employee.reflect_on_association(:desk)
|
84
84
|
association.klass.should == ReflectOnAssociationTestModels::Desk
|
85
85
|
association.macro.should == :has_one
|
@@ -87,7 +87,7 @@ class ReflectOnAssociationTest < Test::Unit::TestCase
|
|
87
87
|
association.options.should == {}
|
88
88
|
end
|
89
89
|
|
90
|
-
should
|
90
|
+
it "should return :belongs_to association for Desk#employee" do
|
91
91
|
association = ReflectOnAssociationTestModels::Desk.reflect_on_association(:employee)
|
92
92
|
association.klass.should == ReflectOnAssociationTestModels::Employee
|
93
93
|
association.macro.should == :belongs_to
|
@@ -97,7 +97,7 @@ class ReflectOnAssociationTest < Test::Unit::TestCase
|
|
97
97
|
end
|
98
98
|
|
99
99
|
context "embeds one" do
|
100
|
-
should
|
100
|
+
it "should return :has_one association for Body#heart" do
|
101
101
|
association = ReflectOnAssociationTestModels::Body.reflect_on_association(:heart)
|
102
102
|
association.klass.should == ReflectOnAssociationTestModels::Heart
|
103
103
|
association.macro.should == :has_one
|
@@ -107,7 +107,7 @@ class ReflectOnAssociationTest < Test::Unit::TestCase
|
|
107
107
|
end
|
108
108
|
|
109
109
|
context "embeds many" do
|
110
|
-
should
|
110
|
+
it "should return :has_many association for Order#line_items" do
|
111
111
|
association = ReflectOnAssociationTestModels::Order.reflect_on_association(:line_items)
|
112
112
|
association.klass.should == ReflectOnAssociationTestModels::LineItem
|
113
113
|
association.macro.should == :has_many
|
@@ -1,19 +1,19 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
|
3
|
+
describe "Rails integration" do
|
4
4
|
context "Document" do
|
5
|
-
|
5
|
+
before do
|
6
6
|
@klass = Doc('Post') do
|
7
7
|
key :foo, String
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
11
|
context "Class methods" do
|
12
|
-
should
|
12
|
+
it "should alias has_many to many" do
|
13
13
|
@klass.should respond_to(:has_many)
|
14
14
|
end
|
15
15
|
|
16
|
-
should
|
16
|
+
it 'should pass on block given in has_many' do
|
17
17
|
@klass.class_eval do
|
18
18
|
has_many :posts do
|
19
19
|
def foo_bars; true; end
|
@@ -23,17 +23,17 @@ class TestRails < Test::Unit::TestCase
|
|
23
23
|
@klass.new.posts.should respond_to(:foo_bars)
|
24
24
|
end
|
25
25
|
|
26
|
-
should
|
26
|
+
it "should alias has_one to one" do
|
27
27
|
@klass.should respond_to(:has_one)
|
28
28
|
end
|
29
29
|
|
30
|
-
should
|
30
|
+
it "should have column names" do
|
31
31
|
@klass.column_names.sort.should == ['_id', 'foo']
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
35
|
context "Instance methods" do
|
36
|
-
|
36
|
+
before do
|
37
37
|
@klass.class_eval do
|
38
38
|
def bar=(value)
|
39
39
|
write_attribute(:foo, value)
|
@@ -49,31 +49,31 @@ class TestRails < Test::Unit::TestCase
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
should
|
52
|
+
it "should alias new_record? to new?" do
|
53
53
|
@klass.new.should be_new_record
|
54
54
|
end
|
55
55
|
|
56
|
-
should
|
56
|
+
it "should be able to read key with read_attribute" do
|
57
57
|
@klass.new(:foo => 'Bar').bar.should == 'Bar'
|
58
58
|
end
|
59
59
|
|
60
|
-
should
|
60
|
+
it "should be able to read key before type cast with read_attribute_before_type_cast" do
|
61
61
|
@klass.new(:foo => 21).bar_before_type_cast.should == 21
|
62
62
|
@klass.new(:foo => 21).bar.should == '21'
|
63
63
|
end
|
64
64
|
|
65
|
-
should
|
65
|
+
it "should be able to write key with write_attribute" do
|
66
66
|
@klass.new(:bar => 'Setting Foo').foo.should == 'Setting Foo'
|
67
67
|
end
|
68
68
|
|
69
69
|
context '#to_param' do
|
70
|
-
should
|
70
|
+
it "should be nil if not persisted" do
|
71
71
|
@klass.new.tap do |doc|
|
72
72
|
doc.to_param.should be_nil
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
-
should
|
76
|
+
it "should array representation of id if persisted" do
|
77
77
|
@klass.create.tap do |doc|
|
78
78
|
doc.to_param.should == doc.id.to_s
|
79
79
|
end
|
@@ -81,13 +81,13 @@ class TestRails < Test::Unit::TestCase
|
|
81
81
|
end
|
82
82
|
|
83
83
|
context '#to_key' do
|
84
|
-
should
|
84
|
+
it "should be nil if not persisted" do
|
85
85
|
@klass.new.tap do |doc|
|
86
86
|
doc.to_key.should be_nil
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
90
|
-
should
|
90
|
+
it "should array representation of id if persisted" do
|
91
91
|
@klass.create.tap do |doc|
|
92
92
|
doc.to_key.should == [doc.id]
|
93
93
|
end
|
@@ -97,26 +97,26 @@ class TestRails < Test::Unit::TestCase
|
|
97
97
|
end
|
98
98
|
|
99
99
|
context "EmbeddedDocument" do
|
100
|
-
|
100
|
+
before do
|
101
101
|
@klass = EDoc('Post') { key :foo, String }
|
102
102
|
end
|
103
103
|
|
104
104
|
context "Class methods" do
|
105
|
-
should
|
105
|
+
it "should alias has_many to many" do
|
106
106
|
@klass.should respond_to(:has_many)
|
107
107
|
end
|
108
108
|
|
109
|
-
should
|
109
|
+
it "should alias has_one to one" do
|
110
110
|
@klass.should respond_to(:has_one)
|
111
111
|
end
|
112
112
|
|
113
|
-
should
|
113
|
+
it "should have column names" do
|
114
114
|
@klass.column_names.sort.should == ['_id', 'foo']
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
118
|
context "Instance methods" do
|
119
|
-
|
119
|
+
before do
|
120
120
|
@klass.class_eval do
|
121
121
|
def bar=(value)
|
122
122
|
write_attribute(:foo, value)
|
@@ -132,48 +132,48 @@ class TestRails < Test::Unit::TestCase
|
|
132
132
|
end
|
133
133
|
end
|
134
134
|
|
135
|
-
should
|
135
|
+
it "should alias new_record? to new?" do
|
136
136
|
@klass.new.should be_new_record
|
137
137
|
end
|
138
138
|
|
139
|
-
should
|
139
|
+
it "should be able to read key with read_attribute" do
|
140
140
|
@klass.new(:foo => 'Bar').bar.should == 'Bar'
|
141
141
|
end
|
142
142
|
|
143
|
-
should
|
143
|
+
it "should be able to read key before type cast with read_attribute_before_type_cast" do
|
144
144
|
@klass.new(:foo => 21).bar_before_type_cast.should == 21
|
145
145
|
@klass.new(:foo => 21).bar.should == '21'
|
146
146
|
end
|
147
147
|
|
148
|
-
should
|
148
|
+
it "should be able to write key with write_attribute" do
|
149
149
|
@klass.new(:bar => 'Setting Foo').foo.should == 'Setting Foo'
|
150
150
|
end
|
151
151
|
|
152
152
|
context '#to_param' do
|
153
|
-
should
|
153
|
+
it "should be nil if not persisted" do
|
154
154
|
@klass.new.tap do |doc|
|
155
155
|
doc.to_param.should be_nil
|
156
156
|
end
|
157
157
|
end
|
158
158
|
|
159
|
-
should
|
159
|
+
it "should array representation of id if persisted" do
|
160
160
|
@klass.new.tap do |doc|
|
161
|
-
doc.
|
161
|
+
doc.should_receive(:persisted?).and_return(true)
|
162
162
|
doc.to_param.should == doc.id.to_s
|
163
163
|
end
|
164
164
|
end
|
165
165
|
end
|
166
166
|
|
167
167
|
context '#to_key' do
|
168
|
-
should
|
168
|
+
it "should be nil if not persisted" do
|
169
169
|
@klass.new.tap do |doc|
|
170
170
|
doc.to_key.should be_nil
|
171
171
|
end
|
172
172
|
end
|
173
173
|
|
174
|
-
should
|
174
|
+
it "should array representation of id if persisted" do
|
175
175
|
@klass.new.tap do |doc|
|
176
|
-
doc.
|
176
|
+
doc.should_receive(:persisted?).and_return(true)
|
177
177
|
doc.to_key.should == [doc.id]
|
178
178
|
end
|
179
179
|
end
|
@@ -0,0 +1,169 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Serialization
|
4
|
+
class List
|
5
|
+
include MongoMapper::Document
|
6
|
+
key :name
|
7
|
+
many :items, :class_name => 'Serialization::Item'
|
8
|
+
belongs_to :creator, :class_name => 'Serialization::User'
|
9
|
+
end
|
10
|
+
|
11
|
+
class Assignment
|
12
|
+
include MongoMapper::EmbeddedDocument
|
13
|
+
belongs_to :assigned_by, :class_name => 'Serialization::User'
|
14
|
+
belongs_to :user, :class_name => 'Serialization::User'
|
15
|
+
|
16
|
+
def serializable_hash(options = {})
|
17
|
+
super({:only => :user_id}.merge(options))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Item
|
22
|
+
include MongoMapper::Document
|
23
|
+
|
24
|
+
key :title
|
25
|
+
key :description
|
26
|
+
many :assignments, :class_name => 'Serialization::Assignment'
|
27
|
+
|
28
|
+
def a_method
|
29
|
+
1
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
class User
|
35
|
+
include MongoMapper::Document
|
36
|
+
key :name, String
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "Serialization" do
|
40
|
+
before do
|
41
|
+
@document = EDoc do
|
42
|
+
key :name, String
|
43
|
+
key :age, Integer
|
44
|
+
key :awesome, Boolean
|
45
|
+
key :preferences, Hash
|
46
|
+
key :created_at, Time
|
47
|
+
end
|
48
|
+
|
49
|
+
@instance = @document.new(
|
50
|
+
:name => 'John Doe',
|
51
|
+
:age => 25,
|
52
|
+
:awesome => true,
|
53
|
+
:preferences => {:language => 'Ruby'},
|
54
|
+
:created_at => Time.now.change(:usec => 0)
|
55
|
+
)
|
56
|
+
end
|
57
|
+
|
58
|
+
context "#serializable_hash" do
|
59
|
+
before do
|
60
|
+
@user1 = User.new(:name => 'Brandon')
|
61
|
+
@user2 = User.new(:name => 'John')
|
62
|
+
@item = Item.new(
|
63
|
+
:title => 'Serialization',
|
64
|
+
:description => 'Make it work like magic!',
|
65
|
+
:assignments => [
|
66
|
+
Assignment.new(:assigned_by => @user1, :user => @user2)
|
67
|
+
]
|
68
|
+
)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should only include specified attributes with :only option" do
|
72
|
+
@item.serializable_hash(:only => :title).should == {'title' => 'Serialization'}
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should exclude attributes specified with :except option" do
|
76
|
+
hash = @item.serializable_hash(:except => :description)
|
77
|
+
hash['title'].should_not be_nil
|
78
|
+
hash['description'].should be_nil
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should add :methods with :only option" do
|
82
|
+
@item.serializable_hash(:only => :title, :methods => :a_method).should == {
|
83
|
+
'title' => 'Serialization',
|
84
|
+
'a_method' => 1
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should call #serializable_hash on embedded many docs" do
|
89
|
+
@item.serializable_hash.should == {
|
90
|
+
'id' => @item.id,
|
91
|
+
'title' => 'Serialization',
|
92
|
+
'description' => 'Make it work like magic!',
|
93
|
+
'assignments' => [{'user_id' => @user2.id}]
|
94
|
+
}
|
95
|
+
end
|
96
|
+
|
97
|
+
context "with :include" do
|
98
|
+
before do
|
99
|
+
@list = List.new(:title => 'MongoMapper', :items => [@item], :creator => @user1)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should add many association" do
|
103
|
+
hash = @list.serializable_hash(:include => :items)
|
104
|
+
hash['items'].should be_instance_of(Array)
|
105
|
+
hash['items'].first['title'].should == 'Serialization'
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should add belongs_to association" do
|
109
|
+
hash = @list.serializable_hash(:include => :creator)
|
110
|
+
hash['creator'].should == @user1.serializable_hash
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should add one association" do
|
114
|
+
author_class = Doc do
|
115
|
+
key :post_id, ObjectId
|
116
|
+
end
|
117
|
+
post_class = Doc('Post') do
|
118
|
+
one :author, :class => author_class
|
119
|
+
end
|
120
|
+
|
121
|
+
author = author_class.new
|
122
|
+
hash = post_class.new(:author => author).serializable_hash(:include => :author)
|
123
|
+
hash['author'].should == author.serializable_hash
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should include multiple associations" do
|
127
|
+
hash = @list.serializable_hash(:include => [:items, :creator])
|
128
|
+
hash['items'].should be_instance_of(Array)
|
129
|
+
hash['creator'].should == @user1.serializable_hash
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should include multiple associations with options" do
|
133
|
+
hash = @list.serializable_hash(:include => {:creator => {:only => :name}})
|
134
|
+
hash['creator'].should == @user1.serializable_hash(:only => :name)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
[:json, :xml].each do |format|
|
140
|
+
context format do
|
141
|
+
it "should be reversable" do
|
142
|
+
serialized = @instance.send("to_#{format}")
|
143
|
+
unserialized = @document.send("from_#{format}", serialized)
|
144
|
+
|
145
|
+
@instance.should == unserialized
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should allow attribute only filtering" do
|
149
|
+
serialized = @instance.send("to_#{format}", :only => [ :age, :name ])
|
150
|
+
unserialized = @document.send("from_#{format}", serialized)
|
151
|
+
|
152
|
+
@instance.name.should == unserialized.name
|
153
|
+
@instance.age.should == unserialized.age
|
154
|
+
unserialized.awesome.should be_false
|
155
|
+
unserialized.created_at.should be_nil
|
156
|
+
end
|
157
|
+
|
158
|
+
it "should allow attribute except filtering" do
|
159
|
+
serialized = @instance.send("to_#{format}", :except => [ :age, :name ])
|
160
|
+
unserialized = @document.send("from_#{format}", serialized)
|
161
|
+
|
162
|
+
unserialized.name.should be_nil
|
163
|
+
unserialized.age.should be_nil
|
164
|
+
@instance.awesome.should == unserialized.awesome
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|