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.
Files changed (154) hide show
  1. checksums.yaml +7 -0
  2. data/README.rdoc +35 -13
  3. data/bin/mmconsole +1 -1
  4. data/lib/mongo_mapper.rb +4 -0
  5. data/lib/mongo_mapper/connection.rb +17 -6
  6. data/lib/mongo_mapper/document.rb +1 -0
  7. data/lib/mongo_mapper/exceptions.rb +4 -1
  8. data/lib/mongo_mapper/extensions/binary.rb +1 -1
  9. data/lib/mongo_mapper/extensions/boolean.rb +20 -23
  10. data/lib/mongo_mapper/extensions/date.rb +3 -3
  11. data/lib/mongo_mapper/extensions/integer.rb +5 -1
  12. data/lib/mongo_mapper/extensions/kernel.rb +2 -0
  13. data/lib/mongo_mapper/extensions/ordered_hash.rb +23 -0
  14. data/lib/mongo_mapper/extensions/string.rb +2 -2
  15. data/lib/mongo_mapper/extensions/time.rb +7 -5
  16. data/lib/mongo_mapper/middleware/identity_map.rb +3 -4
  17. data/lib/mongo_mapper/plugins.rb +1 -1
  18. data/lib/mongo_mapper/plugins/associations.rb +11 -5
  19. data/lib/mongo_mapper/plugins/associations/base.rb +5 -3
  20. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +0 -0
  21. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +8 -8
  22. data/lib/mongo_mapper/plugins/associations/collection.rb +2 -0
  23. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +32 -7
  24. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +2 -2
  25. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +12 -12
  26. data/lib/mongo_mapper/plugins/associations/proxy.rb +5 -1
  27. data/lib/mongo_mapper/plugins/associations/single_association.rb +6 -6
  28. data/lib/mongo_mapper/plugins/clone.rb +4 -2
  29. data/lib/mongo_mapper/plugins/dirty.rb +22 -21
  30. data/lib/mongo_mapper/plugins/document.rb +4 -4
  31. data/lib/mongo_mapper/plugins/dumpable.rb +22 -0
  32. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +58 -9
  33. data/lib/mongo_mapper/plugins/identity_map.rb +42 -32
  34. data/lib/mongo_mapper/plugins/keys.rb +133 -54
  35. data/lib/mongo_mapper/plugins/keys/key.rb +68 -22
  36. data/lib/mongo_mapper/plugins/modifiers.rb +26 -19
  37. data/lib/mongo_mapper/plugins/persistence.rb +15 -5
  38. data/lib/mongo_mapper/plugins/querying.rb +15 -40
  39. data/lib/mongo_mapper/plugins/querying/{decorator.rb → decorated_plucky_query.rb} +24 -4
  40. data/lib/mongo_mapper/plugins/rails.rb +22 -2
  41. data/lib/mongo_mapper/plugins/safe.rb +8 -5
  42. data/lib/mongo_mapper/plugins/sci.rb +26 -4
  43. data/lib/mongo_mapper/plugins/scopes.rb +5 -4
  44. data/lib/mongo_mapper/plugins/timestamps.rb +11 -4
  45. data/lib/mongo_mapper/plugins/validations.rb +1 -1
  46. data/lib/mongo_mapper/utils.rb +12 -0
  47. data/lib/mongo_mapper/version.rb +1 -1
  48. data/lib/rails/generators/mongo_mapper/config/config_generator.rb +20 -7
  49. data/lib/rails/generators/mongo_mapper/config/templates/mongo.yml +6 -0
  50. data/lib/rails/generators/mongo_mapper/model/model_generator.rb +18 -1
  51. data/lib/rails/generators/mongo_mapper/model/templates/model.rb +9 -5
  52. data/{test/functional/test_accessible.rb → spec/functional/accessible_spec.rb} +29 -29
  53. data/{test/functional/associations/test_belongs_to_polymorphic_proxy.rb → spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb} +10 -10
  54. data/{test/functional/associations/test_belongs_to_proxy.rb → spec/functional/associations/belongs_to_proxy_spec.rb} +82 -64
  55. data/{test/functional/associations/test_in_array_proxy.rb → spec/functional/associations/in_array_proxy_spec.rb} +68 -68
  56. data/{test/functional/associations/test_many_documents_as_proxy.rb → spec/functional/associations/many_documents_as_proxy_spec.rb} +37 -38
  57. data/{test/functional/associations/test_many_documents_proxy.rb → spec/functional/associations/many_documents_proxy_spec.rb} +233 -146
  58. data/{test/functional/associations/test_many_embedded_polymorphic_proxy.rb → spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb} +19 -20
  59. data/{test/functional/associations/test_many_embedded_proxy.rb → spec/functional/associations/many_embedded_proxy_spec.rb} +23 -24
  60. data/{test/functional/associations/test_many_polymorphic_proxy.rb → spec/functional/associations/many_polymorphic_proxy_spec.rb} +45 -46
  61. data/{test/functional/associations/test_one_as_proxy.rb → spec/functional/associations/one_as_proxy_spec.rb} +75 -77
  62. data/{test/functional/associations/test_one_embedded_polymorphic_proxy.rb → spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb} +31 -32
  63. data/{test/functional/associations/test_one_embedded_proxy.rb → spec/functional/associations/one_embedded_proxy_spec.rb} +10 -10
  64. data/{test/functional/associations/test_one_proxy.rb → spec/functional/associations/one_proxy_spec.rb} +125 -102
  65. data/spec/functional/associations_spec.rb +48 -0
  66. data/{test/functional/test_binary.rb → spec/functional/binary_spec.rb} +6 -6
  67. data/spec/functional/caching_spec.rb +75 -0
  68. data/{test/functional/test_callbacks.rb → spec/functional/callbacks_spec.rb} +84 -26
  69. data/{test/functional/test_dirty.rb → spec/functional/dirty_spec.rb} +57 -42
  70. data/{test/functional/test_document.rb → spec/functional/document_spec.rb} +52 -52
  71. data/spec/functional/dumpable_spec.rb +24 -0
  72. data/{test/functional/test_dynamic_querying.rb → spec/functional/dynamic_querying_spec.rb} +14 -14
  73. data/{test/functional/test_embedded_document.rb → spec/functional/embedded_document_spec.rb} +51 -42
  74. data/{test/functional/test_equality.rb → spec/functional/equality_spec.rb} +4 -4
  75. data/spec/functional/extensions_spec.rb +16 -0
  76. data/{test/functional/test_identity_map.rb → spec/functional/identity_map_spec.rb} +73 -61
  77. data/spec/functional/indexes_spec.rb +48 -0
  78. data/spec/functional/keys_spec.rb +224 -0
  79. data/{test/functional/test_logger.rb → spec/functional/logger_spec.rb} +6 -6
  80. data/spec/functional/modifiers_spec.rb +550 -0
  81. data/spec/functional/pagination_spec.rb +89 -0
  82. data/spec/functional/protected_spec.rb +199 -0
  83. data/spec/functional/querying_spec.rb +1003 -0
  84. data/spec/functional/rails_spec.rb +55 -0
  85. data/spec/functional/safe_spec.rb +163 -0
  86. data/{test/functional/test_sci.rb → spec/functional/sci_spec.rb} +123 -34
  87. data/{test/functional/test_scopes.rb → spec/functional/scopes_spec.rb} +59 -26
  88. data/spec/functional/timestamps_spec.rb +97 -0
  89. data/{test/functional/test_touch.rb → spec/functional/touch_spec.rb} +13 -13
  90. data/spec/functional/userstamps_spec.rb +46 -0
  91. data/{test/functional/test_validations.rb → spec/functional/validations_spec.rb} +64 -64
  92. data/spec/spec_helper.rb +81 -0
  93. data/spec/support/matchers.rb +24 -0
  94. data/{test → spec/support}/models.rb +1 -6
  95. data/spec/unit/associations/base_spec.rb +146 -0
  96. data/spec/unit/associations/belongs_to_association_spec.rb +30 -0
  97. data/spec/unit/associations/many_association_spec.rb +64 -0
  98. data/spec/unit/associations/one_association_spec.rb +48 -0
  99. data/{test/unit/associations/test_proxy.rb → spec/unit/associations/proxy_spec.rb} +21 -21
  100. data/{test/unit/test_clone.rb → spec/unit/clone_spec.rb} +21 -11
  101. data/spec/unit/config_generator_spec.rb +24 -0
  102. data/{test/unit/test_document.rb → spec/unit/document_spec.rb} +42 -42
  103. data/{test/unit/test_dynamic_finder.rb → spec/unit/dynamic_finder_spec.rb} +28 -28
  104. data/{test/unit/test_embedded_document.rb → spec/unit/embedded_document_spec.rb} +102 -108
  105. data/{test/unit/test_equality.rb → spec/unit/equality_spec.rb} +7 -7
  106. data/{test/unit/test_exceptions.rb → spec/unit/exceptions_spec.rb} +3 -3
  107. data/{test/unit/test_extensions.rb → spec/unit/extensions_spec.rb} +85 -71
  108. data/spec/unit/identity_map_middleware_spec.rb +134 -0
  109. data/{test/unit/test_inspect.rb → spec/unit/inspect_spec.rb} +8 -8
  110. data/{test/unit/test_key.rb → spec/unit/key_spec.rb} +82 -52
  111. data/spec/unit/keys_spec.rb +155 -0
  112. data/spec/unit/model_generator_spec.rb +47 -0
  113. data/spec/unit/mongo_mapper_spec.rb +184 -0
  114. data/spec/unit/pagination_spec.rb +11 -0
  115. data/{test/unit/test_plugins.rb → spec/unit/plugins_spec.rb} +14 -14
  116. data/spec/unit/rails_compatibility_spec.rb +40 -0
  117. data/{test/unit/test_rails_reflect_on_association.rb → spec/unit/rails_reflect_on_association_spec.rb} +9 -9
  118. data/{test/unit/test_rails.rb → spec/unit/rails_spec.rb} +31 -31
  119. data/spec/unit/serialization_spec.rb +169 -0
  120. data/spec/unit/serializers/json_serializer_spec.rb +218 -0
  121. data/spec/unit/serializers/xml_serializer_spec.rb +198 -0
  122. data/{test/unit/test_time_zones.rb → spec/unit/time_zones_spec.rb} +8 -8
  123. data/{test/unit/test_translation.rb → spec/unit/translation_spec.rb} +6 -6
  124. data/{test/unit/test_validations.rb → spec/unit/validations_spec.rb} +72 -59
  125. metadata +199 -179
  126. data/test/_NOTE_ON_TESTING +0 -1
  127. data/test/functional/test_associations.rb +0 -46
  128. data/test/functional/test_caching.rb +0 -77
  129. data/test/functional/test_indexes.rb +0 -50
  130. data/test/functional/test_modifiers.rb +0 -537
  131. data/test/functional/test_pagination.rb +0 -91
  132. data/test/functional/test_protected.rb +0 -201
  133. data/test/functional/test_querying.rb +0 -935
  134. data/test/functional/test_safe.rb +0 -76
  135. data/test/functional/test_timestamps.rb +0 -62
  136. data/test/functional/test_userstamps.rb +0 -44
  137. data/test/support/railtie.rb +0 -4
  138. data/test/support/railtie/autoloaded.rb +0 -2
  139. data/test/support/railtie/not_autoloaded.rb +0 -3
  140. data/test/support/railtie/parent.rb +0 -3
  141. data/test/test_active_model_lint.rb +0 -18
  142. data/test/test_helper.rb +0 -93
  143. data/test/unit/associations/test_base.rb +0 -146
  144. data/test/unit/associations/test_belongs_to_association.rb +0 -29
  145. data/test/unit/associations/test_many_association.rb +0 -63
  146. data/test/unit/associations/test_one_association.rb +0 -47
  147. data/test/unit/serializers/test_json_serializer.rb +0 -216
  148. data/test/unit/serializers/test_xml_serializer.rb +0 -196
  149. data/test/unit/test_identity_map_middleware.rb +0 -132
  150. data/test/unit/test_keys.rb +0 -65
  151. data/test/unit/test_mongo_mapper.rb +0 -157
  152. data/test/unit/test_pagination.rb +0 -11
  153. data/test/unit/test_rails_compatibility.rb +0 -38
  154. 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 'test_helper'
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
- class ReflectOnAssociationTest < Test::Unit::TestCase
52
+ describe "ReflectOnAssociation" do
53
53
  context "one-to-many association" do
54
- should "return :has_many association for Tree#birds" do
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 "return :belongs_to association for Bird#tree" do
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 "return :has_many association for Book#authors" do
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 "return :has_one association for Employee#desk" do
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 "return :belongs_to association for Desk#employee" do
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 "return :has_one association for Body#heart" do
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 "return :has_many association for Order#line_items" do
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 'test_helper'
1
+ require 'spec_helper'
2
2
 
3
- class TestRails < Test::Unit::TestCase
3
+ describe "Rails integration" do
4
4
  context "Document" do
5
- setup do
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 "alias has_many to many" do
12
+ it "should alias has_many to many" do
13
13
  @klass.should respond_to(:has_many)
14
14
  end
15
15
 
16
- should 'pass on block given in has_many' do
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 "alias has_one to one" do
26
+ it "should alias has_one to one" do
27
27
  @klass.should respond_to(:has_one)
28
28
  end
29
29
 
30
- should "have column names" do
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
- setup do
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 "alias new_record? to new?" do
52
+ it "should alias new_record? to new?" do
53
53
  @klass.new.should be_new_record
54
54
  end
55
55
 
56
- should "be able to read key with read_attribute" do
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 "be able to read key before type cast with read_attribute_before_type_cast" do
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 "be able to write key with write_attribute" do
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 "be nil if not persisted" do
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 "array representation of id if persisted" do
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 "be nil if not persisted" do
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 "array representation of id if persisted" do
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
- setup do
100
+ before do
101
101
  @klass = EDoc('Post') { key :foo, String }
102
102
  end
103
103
 
104
104
  context "Class methods" do
105
- should "alias has_many to many" do
105
+ it "should alias has_many to many" do
106
106
  @klass.should respond_to(:has_many)
107
107
  end
108
108
 
109
- should "alias has_one to one" do
109
+ it "should alias has_one to one" do
110
110
  @klass.should respond_to(:has_one)
111
111
  end
112
112
 
113
- should "have column names" do
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
- setup do
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 "alias new_record? to new?" do
135
+ it "should alias new_record? to new?" do
136
136
  @klass.new.should be_new_record
137
137
  end
138
138
 
139
- should "be able to read key with read_attribute" do
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 "be able to read key before type cast with read_attribute_before_type_cast" do
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 "be able to write key with write_attribute" do
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 "be nil if not persisted" do
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 "array representation of id if persisted" do
159
+ it "should array representation of id if persisted" do
160
160
  @klass.new.tap do |doc|
161
- doc.expects(:persisted?).returns(true)
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 "be nil if not persisted" do
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 "array representation of id if persisted" do
174
+ it "should array representation of id if persisted" do
175
175
  @klass.new.tap do |doc|
176
- doc.expects(:persisted?).returns(true)
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