seomoz-ripple 1.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. data/Gemfile +20 -0
  2. data/Guardfile +15 -0
  3. data/Rakefile +88 -0
  4. data/lib/rails/generators/ripple/configuration/configuration_generator.rb +13 -0
  5. data/lib/rails/generators/ripple/configuration/templates/ripple.yml +24 -0
  6. data/lib/rails/generators/ripple/js/js_generator.rb +13 -0
  7. data/lib/rails/generators/ripple/js/templates/js/contrib.js +63 -0
  8. data/lib/rails/generators/ripple/js/templates/js/iso8601.js +76 -0
  9. data/lib/rails/generators/ripple/js/templates/js/ripple.js +132 -0
  10. data/lib/rails/generators/ripple/model/model_generator.rb +20 -0
  11. data/lib/rails/generators/ripple/model/templates/model.rb +10 -0
  12. data/lib/rails/generators/ripple/observer/observer_generator.rb +16 -0
  13. data/lib/rails/generators/ripple/observer/templates/observer.rb +4 -0
  14. data/lib/rails/generators/ripple/test/templates/test_server.rb +46 -0
  15. data/lib/rails/generators/ripple/test/test_generator.rb +39 -0
  16. data/lib/rails/generators/ripple_generator.rb +78 -0
  17. data/lib/ripple.rb +79 -0
  18. data/lib/ripple/associations.rb +356 -0
  19. data/lib/ripple/associations/embedded.rb +35 -0
  20. data/lib/ripple/associations/instantiators.rb +26 -0
  21. data/lib/ripple/associations/linked.rb +65 -0
  22. data/lib/ripple/associations/many.rb +38 -0
  23. data/lib/ripple/associations/many_embedded_proxy.rb +38 -0
  24. data/lib/ripple/associations/many_linked_proxy.rb +66 -0
  25. data/lib/ripple/associations/many_reference_proxy.rb +93 -0
  26. data/lib/ripple/associations/many_stored_key_proxy.rb +76 -0
  27. data/lib/ripple/associations/one.rb +20 -0
  28. data/lib/ripple/associations/one_embedded_proxy.rb +35 -0
  29. data/lib/ripple/associations/one_key_proxy.rb +58 -0
  30. data/lib/ripple/associations/one_linked_proxy.rb +22 -0
  31. data/lib/ripple/associations/one_stored_key_proxy.rb +43 -0
  32. data/lib/ripple/associations/proxy.rb +118 -0
  33. data/lib/ripple/attribute_methods.rb +118 -0
  34. data/lib/ripple/attribute_methods/dirty.rb +50 -0
  35. data/lib/ripple/attribute_methods/query.rb +34 -0
  36. data/lib/ripple/attribute_methods/read.rb +26 -0
  37. data/lib/ripple/attribute_methods/write.rb +25 -0
  38. data/lib/ripple/callbacks.rb +74 -0
  39. data/lib/ripple/conflict/basic_resolver.rb +82 -0
  40. data/lib/ripple/conflict/document_hooks.rb +20 -0
  41. data/lib/ripple/conflict/resolver.rb +71 -0
  42. data/lib/ripple/conflict/test_helper.rb +33 -0
  43. data/lib/ripple/conversion.rb +28 -0
  44. data/lib/ripple/core_ext.rb +2 -0
  45. data/lib/ripple/core_ext/casting.rb +148 -0
  46. data/lib/ripple/core_ext/object.rb +8 -0
  47. data/lib/ripple/document.rb +104 -0
  48. data/lib/ripple/document/bucket_access.rb +25 -0
  49. data/lib/ripple/document/finders.rb +131 -0
  50. data/lib/ripple/document/key.rb +43 -0
  51. data/lib/ripple/document/link.rb +30 -0
  52. data/lib/ripple/document/persistence.rb +115 -0
  53. data/lib/ripple/embedded_document.rb +64 -0
  54. data/lib/ripple/embedded_document/around_callbacks.rb +18 -0
  55. data/lib/ripple/embedded_document/finders.rb +26 -0
  56. data/lib/ripple/embedded_document/persistence.rb +77 -0
  57. data/lib/ripple/i18n.rb +2 -0
  58. data/lib/ripple/inspection.rb +32 -0
  59. data/lib/ripple/locale/en.yml +21 -0
  60. data/lib/ripple/nested_attributes.rb +265 -0
  61. data/lib/ripple/observable.rb +28 -0
  62. data/lib/ripple/properties.rb +73 -0
  63. data/lib/ripple/property_type_mismatch.rb +12 -0
  64. data/lib/ripple/railtie.rb +13 -0
  65. data/lib/ripple/serialization.rb +84 -0
  66. data/lib/ripple/timestamps.rb +27 -0
  67. data/lib/ripple/translation.rb +14 -0
  68. data/lib/ripple/validations.rb +67 -0
  69. data/lib/ripple/validations/associated_validator.rb +43 -0
  70. data/ripple.gemspec +46 -0
  71. data/seomoz-ripple.gemspec +46 -0
  72. data/spec/fixtures/config.yml +8 -0
  73. data/spec/integration/ripple/associations_spec.rb +220 -0
  74. data/spec/integration/ripple/conflict_resolution_spec.rb +293 -0
  75. data/spec/integration/ripple/nested_attributes_spec.rb +264 -0
  76. data/spec/integration/ripple/persistence_spec.rb +57 -0
  77. data/spec/integration/ripple/search_associations_spec.rb +42 -0
  78. data/spec/ripple/associations/many_embedded_proxy_spec.rb +122 -0
  79. data/spec/ripple/associations/many_linked_proxy_spec.rb +191 -0
  80. data/spec/ripple/associations/many_reference_proxy_spec.rb +170 -0
  81. data/spec/ripple/associations/many_stored_key_proxy_spec.rb +158 -0
  82. data/spec/ripple/associations/one_embedded_proxy_spec.rb +125 -0
  83. data/spec/ripple/associations/one_key_proxy_spec.rb +82 -0
  84. data/spec/ripple/associations/one_linked_proxy_spec.rb +91 -0
  85. data/spec/ripple/associations/one_stored_key_proxy_spec.rb +72 -0
  86. data/spec/ripple/associations/proxy_spec.rb +84 -0
  87. data/spec/ripple/associations_spec.rb +129 -0
  88. data/spec/ripple/attribute_methods/dirty_spec.rb +80 -0
  89. data/spec/ripple/attribute_methods_spec.rb +230 -0
  90. data/spec/ripple/bucket_access_spec.rb +25 -0
  91. data/spec/ripple/callbacks_spec.rb +176 -0
  92. data/spec/ripple/conflict/resolver_spec.rb +42 -0
  93. data/spec/ripple/conversion_spec.rb +22 -0
  94. data/spec/ripple/core_ext_spec.rb +103 -0
  95. data/spec/ripple/document/link_spec.rb +67 -0
  96. data/spec/ripple/document_spec.rb +96 -0
  97. data/spec/ripple/embedded_document/finders_spec.rb +29 -0
  98. data/spec/ripple/embedded_document/persistence_spec.rb +80 -0
  99. data/spec/ripple/embedded_document_spec.rb +84 -0
  100. data/spec/ripple/finders_spec.rb +217 -0
  101. data/spec/ripple/inspection_spec.rb +51 -0
  102. data/spec/ripple/key_spec.rb +30 -0
  103. data/spec/ripple/observable_spec.rb +121 -0
  104. data/spec/ripple/persistence_spec.rb +326 -0
  105. data/spec/ripple/properties_spec.rb +262 -0
  106. data/spec/ripple/ripple_spec.rb +71 -0
  107. data/spec/ripple/serialization_spec.rb +51 -0
  108. data/spec/ripple/timestamps_spec.rb +76 -0
  109. data/spec/ripple/validations/associated_validator_spec.rb +77 -0
  110. data/spec/ripple/validations_spec.rb +104 -0
  111. data/spec/spec_helper.rb +26 -0
  112. data/spec/support/associations.rb +1 -0
  113. data/spec/support/associations/proxies.rb +17 -0
  114. data/spec/support/integration_setup.rb +11 -0
  115. data/spec/support/mocks.rb +4 -0
  116. data/spec/support/models.rb +4 -0
  117. data/spec/support/models/address.rb +12 -0
  118. data/spec/support/models/box.rb +13 -0
  119. data/spec/support/models/car.rb +16 -0
  120. data/spec/support/models/cardboard_box.rb +3 -0
  121. data/spec/support/models/clock.rb +12 -0
  122. data/spec/support/models/clock_observer.rb +3 -0
  123. data/spec/support/models/company.rb +23 -0
  124. data/spec/support/models/customer.rb +4 -0
  125. data/spec/support/models/driver.rb +6 -0
  126. data/spec/support/models/email.rb +4 -0
  127. data/spec/support/models/engine.rb +5 -0
  128. data/spec/support/models/family.rb +16 -0
  129. data/spec/support/models/favorite.rb +4 -0
  130. data/spec/support/models/invoice.rb +7 -0
  131. data/spec/support/models/late_invoice.rb +3 -0
  132. data/spec/support/models/ninja.rb +9 -0
  133. data/spec/support/models/note.rb +5 -0
  134. data/spec/support/models/page.rb +4 -0
  135. data/spec/support/models/paid_invoice.rb +4 -0
  136. data/spec/support/models/passenger.rb +6 -0
  137. data/spec/support/models/profile.rb +10 -0
  138. data/spec/support/models/seat.rb +5 -0
  139. data/spec/support/models/subscription.rb +27 -0
  140. data/spec/support/models/tasks.rb +14 -0
  141. data/spec/support/models/team.rb +11 -0
  142. data/spec/support/models/transactions.rb +17 -0
  143. data/spec/support/models/tree.rb +4 -0
  144. data/spec/support/models/user.rb +10 -0
  145. data/spec/support/models/wheel.rb +6 -0
  146. data/spec/support/models/widget.rb +22 -0
  147. data/spec/support/test_server.rb +18 -0
  148. data/spec/support/test_server.yml.example +2 -0
  149. metadata +362 -0
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ripple::Document::Key do
4
+ require 'support/models/box'
5
+ before do
6
+ @box = Box.new
7
+ end
8
+
9
+ it "should define key getter and setter" do
10
+ @box.should respond_to(:key)
11
+ @box.should respond_to(:key=)
12
+ end
13
+
14
+ it "should stringify the assigned key" do
15
+ @box.key = 2
16
+ @box.key.should == "2"
17
+ end
18
+
19
+ it "should use a property as the key" do
20
+ class ShapedBox < Box
21
+ key_on :shape
22
+ end
23
+ @box = ShapedBox.new
24
+ @box.key = "square"
25
+ @box.key.should == "square"
26
+ @box.shape.should == "square"
27
+ @box.shape = "oblong"
28
+ @box.key.should == "oblong"
29
+ end
30
+ end
@@ -0,0 +1,121 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ripple::Observable do
4
+ require 'support/models/clock'
5
+ require 'support/models/clock_observer'
6
+
7
+ before :each do
8
+ @client = Ripple.client
9
+ @backend = mock("Backend", :store_object => true)
10
+ @client.stub!(:backend).and_return(@backend)
11
+ @clock = Clock.new
12
+ @observer = ClockObserver.instance
13
+ end
14
+
15
+ context "given a document is created" do
16
+ it "should notify all observers twice" do
17
+ Clock.should_receive(:notify_observers).exactly(6).times
18
+ @clock.save
19
+ end
20
+
21
+ context "before creating the document" do
22
+ it "should call Observer#before_create" do
23
+ @observer.should_receive(:before_create)
24
+ @clock.save
25
+ end
26
+
27
+ it "should call Observer#before_save" do
28
+ @observer.should_receive(:before_save)
29
+ @clock.save
30
+ end
31
+
32
+ it "should call Observer#before_validation" do
33
+ @observer.should_receive(:before_validation)
34
+ @clock.save
35
+ end
36
+ end
37
+
38
+ context "after creating the document" do
39
+ it "should call Observer#after_create" do
40
+ @observer.should_receive(:after_create)
41
+ @clock.save
42
+ end
43
+
44
+ it "should call Observer#after_save" do
45
+ @observer.should_receive(:after_save)
46
+ @clock.save
47
+ end
48
+
49
+ it "should call Observer#after_validation" do
50
+ @observer.should_receive(:after_validation)
51
+ @clock.save
52
+ end
53
+ end
54
+ end
55
+
56
+ context "given a document is updated" do
57
+ before(:each) do
58
+ @clock.stub!(:new?).and_return(false)
59
+ end
60
+
61
+ it "should notify all observers twice" do
62
+ Clock.should_receive(:notify_observers).exactly(6).times
63
+ @clock.save
64
+ end
65
+
66
+ context "before updating the document" do
67
+ it "should call Observer#before_update" do
68
+ @observer.should_receive(:before_update)
69
+ @clock.save
70
+ end
71
+
72
+ it "should call Observer#before_save" do
73
+ @observer.should_receive(:before_save)
74
+ @clock.save
75
+ end
76
+
77
+ it "should call Observer#before_validation" do
78
+ @observer.should_receive(:before_validation)
79
+ @clock.save
80
+ end
81
+ end
82
+
83
+ context "after updating the document" do
84
+ it "should call Observer#after_update" do
85
+ @observer.should_receive(:after_update)
86
+ @clock.save
87
+ end
88
+
89
+ it "should call Observer#after_save" do
90
+ @observer.should_receive(:after_save)
91
+ @clock.save
92
+ end
93
+
94
+ it "should call Observer#after_validation" do
95
+ @observer.should_receive(:after_validation)
96
+ @clock.save
97
+ end
98
+ end
99
+ end
100
+
101
+ context "given a document is destroyed" do
102
+ it "should notify all observers twice" do
103
+ Clock.should_receive(:notify_observers).twice
104
+ @clock.destroy
105
+ end
106
+
107
+ context "before destroying the document" do
108
+ it "should call Observer#before_destroy" do
109
+ @observer.should_receive(:before_destroy)
110
+ @clock.destroy
111
+ end
112
+ end
113
+
114
+ context "after destroy the document" do
115
+ it "should call Observer#after_destroy" do
116
+ @observer.should_receive(:after_destroy)
117
+ @clock.destroy
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,326 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ripple::Document::Persistence do
4
+ require 'support/models/widget'
5
+
6
+ before :each do
7
+ @backend = mock("Backend")
8
+ @client = Ripple.client
9
+ @client.stub!(:backend).and_return(@backend)
10
+ @bucket = Ripple.client.bucket("widgets")
11
+ @widget = Widget.new(:size => 1000)
12
+ end
13
+
14
+ it "forces the content type to 'application/json'" do
15
+ @widget.robject.content_type = 'application/not-json'
16
+
17
+ @backend.should_receive(:store_object) do |obj, *_|
18
+ obj.content_type.should == 'application/json'
19
+ end
20
+
21
+ @widget.save
22
+ end
23
+
24
+ it "should save a new object to Riak" do
25
+ json = @widget.attributes.merge("_type" => "Widget").to_json
26
+ @backend.should_receive(:store_object) do |obj, _, _, _|
27
+ obj.raw_data.should == json
28
+ obj.key.should be_nil
29
+ # Simulate loading the response with the key
30
+ obj.key = "new_widget"
31
+ end
32
+ @widget.save
33
+ @widget.key.should == "new_widget"
34
+ @widget.should_not be_a_new_record
35
+ @widget.changes.should be_blank
36
+ end
37
+
38
+ it "should modify attributes and save a new object" do
39
+ json = @widget.attributes.merge("_type" => "Widget", "size" => 5).to_json
40
+ @backend.should_receive(:store_object) do |obj, _, _, _|
41
+ obj.raw_data.should == json
42
+ obj.key.should be_nil
43
+ # Simulate loading the response with the key
44
+ obj.key = "new_widget"
45
+ end
46
+ @widget.update_attributes(:size => 5)
47
+ @widget.key.should == "new_widget"
48
+ @widget.should_not be_a_new_record
49
+ @widget.changes.should be_blank
50
+ end
51
+
52
+ it "should modify a single attribute and save a new object" do
53
+ json = @widget.attributes.merge("_type" => "Widget", "size" => 5).to_json
54
+ @backend.should_receive(:store_object) do |obj, _, _, _|
55
+ obj.raw_data.should == json
56
+ obj.key.should be_nil
57
+ # Simulate loading the response with the key
58
+ obj.key = "new_widget"
59
+ end
60
+ @widget.update_attribute(:size, 5)
61
+ @widget.key.should == "new_widget"
62
+ @widget.should_not be_a_new_record
63
+ @widget.changes.should be_blank
64
+ @widget.size.should == 5
65
+ end
66
+
67
+ it "should instantiate and save a new object to riak" do
68
+ json = @widget.attributes.merge(:size => 10, :shipped_at => "2000-01-01T20:15:01Z", :_type => 'Widget').to_json
69
+ @backend.should_receive(:store_object) do |obj, _, _, _|
70
+ obj.raw_data.should == json
71
+ obj.key.should be_nil
72
+ # Simulate loading the response with the key
73
+ obj.key = "new_widget"
74
+ end
75
+ @widget = Widget.create(:size => 10, :shipped_at => Time.utc(2000,"jan",1,20,15,1))
76
+ @widget.size.should == 10
77
+ @widget.shipped_at.should == Time.utc(2000,"jan",1,20,15,1)
78
+ @widget.should_not be_a_new_record
79
+ end
80
+
81
+ it "should instantiate and save a new object to riak and allow its attributes to be set via a block" do
82
+ json = @widget.attributes.merge(:size => 10, :_type => 'Widget').to_json
83
+ @backend.should_receive(:store_object) do |obj, _, _, _|
84
+ obj.raw_data.should == json
85
+ obj.key.should be_nil
86
+ # Simulate loading the response with the key
87
+ obj.key = "new_widget"
88
+ end
89
+ @widget = Widget.create do |widget|
90
+ widget.size = 10
91
+ end
92
+ @widget.size.should == 10
93
+ @widget.should_not be_a_new_record
94
+ end
95
+
96
+ it "should save the attributes not having a corresponding property" do
97
+ attrs = @widget.attributes.merge("_type" => "Widget", "unknown_property" => "a_value")
98
+ @backend.should_receive(:store_object) do |obj, _, _, _|
99
+ obj.data.should == attrs
100
+ obj.key.should be_nil
101
+ # Simulate loading the response with the key
102
+ obj.key = "new_widget"
103
+ end
104
+ @widget["unknown_property"] = "a_value"
105
+ @widget.save
106
+ @widget.key.should == "new_widget"
107
+ @widget.should_not be_a_new_record
108
+ @widget.changes.should be_blank
109
+ end
110
+
111
+ it "should allow unexpected exceptions to be raised" do
112
+ robject = mock("robject", :key => @widget.key, "data=" => true, "content_type=" => true)
113
+ robject.should_receive(:store).and_raise(Riak::HTTPFailedRequest.new(:post, 200, 404, {}, "404 not found"))
114
+ @widget.stub!(:robject).and_return(robject)
115
+ lambda { @widget.save }.should raise_error(Riak::FailedRequest)
116
+ end
117
+
118
+ it "should reload a saved object, including associations" do
119
+ json = @widget.attributes.merge(:_type => "Widget").to_json
120
+ @backend.should_receive(:store_object) do |obj, _, _, _|
121
+ obj.raw_data.should == json
122
+ obj.key.should be_nil
123
+ # Simulate loading the response with the key
124
+ obj.key = "new_widget"
125
+ end
126
+ @widget.save
127
+ @backend.should_receive(:reload_object) do |obj, _|
128
+ obj.key.should == "new_widget"
129
+ obj.raw_data = '{"name":"spring","size":10,"shipped_at":"Sat, 01 Jan 2000 20:15:01 -0000","_type":"Widget"}'
130
+ end
131
+
132
+ @widget.widget_parts.should_receive(:reset)
133
+ @widget.reload
134
+ @widget.changes.should be_blank
135
+ @widget.name.should == "spring"
136
+ @widget.size.should == 10
137
+ @widget.shipped_at.should == Time.utc(2000,"jan",1,20,15,1)
138
+ end
139
+
140
+ it "should destroy a saved object" do
141
+ @backend.should_receive(:store_object).and_return(true)
142
+ @widget.key = "foo"
143
+ @widget.save
144
+ @widget.should_not be_new
145
+ @backend.should_receive(:delete_object).and_return(true)
146
+ @widget.destroy.should be_true
147
+ @widget.should be_frozen
148
+ end
149
+
150
+ it "should destroy all saved objects" do
151
+ @widget.should_receive(:destroy).and_return(true)
152
+ Widget.should_receive(:list).and_yield(@widget)
153
+ Widget.destroy_all.should be_true
154
+ end
155
+
156
+ it "should freeze an unsaved object when destroying" do
157
+ @backend.should_not_receive(:delete_object)
158
+ @widget.destroy.should be_true
159
+ @widget.should be_frozen
160
+ end
161
+
162
+ it "should be a root document" do
163
+ @widget._root_document.should == @widget
164
+ end
165
+
166
+ describe "when storing a class using single-bucket inheritance" do
167
+ before :each do
168
+ @cog = Cog.new(:size => 1000)
169
+ end
170
+
171
+ it "should store the _type field as the class name" do
172
+ json = @cog.attributes.merge("_type" => "Cog").to_json
173
+ @backend.should_receive(:store_object) do |obj, _, _, _|
174
+ obj.raw_data.should == json
175
+ obj.key = "new_widget"
176
+ end
177
+ @cog.save
178
+ @cog.should_not be_new_record
179
+ end
180
+ end
181
+
182
+ describe "modifying the default quorum values" do
183
+ before :each do
184
+ Widget.set_quorums :r => 1, :w => 1, :dw => 0, :rw => 1
185
+ @bucket = mock("bucket", :name => "widgets")
186
+ @robject = mock("object", :data => {"name" => "bar"}, :key => "gear")
187
+ Widget.stub(:bucket).and_return(@bucket)
188
+ end
189
+
190
+ it "should use the supplied R value when reading" do
191
+ @bucket.should_receive(:get).with("gear", :r => 1).and_return(@robject)
192
+ Widget.find("gear")
193
+ end
194
+
195
+ it "should use the supplied W and DW values when storing" do
196
+ Widget.new do |widget|
197
+ widget.key = "gear"
198
+ widget.send(:robject).should_receive(:store).with({:w => 1, :dw => 0})
199
+ widget.save
200
+ end
201
+ end
202
+
203
+ it "should use the supplied RW when deleting" do
204
+ widget = Widget.new
205
+ widget.key = "gear"
206
+ widget.instance_variable_set(:@new, false)
207
+ widget.send(:robject).should_receive(:delete).with({:rw => 1})
208
+ widget.destroy
209
+ end
210
+ end
211
+
212
+ shared_examples_for "saving a parent document with linked child documents" do
213
+ before(:each) do
214
+ @backend.stub(:store_object)
215
+ end
216
+
217
+ it 'saves new children when the parent is saved' do
218
+ children.each do |child|
219
+ child.stub(:new? => true)
220
+ child.should_receive(:save)
221
+ end
222
+ parent.save
223
+ end
224
+
225
+ it 'saves children that have changes when the parent is saved' do
226
+ children.each do |child|
227
+ child.stub(:new? => false)
228
+ child.stub(:changed? => true)
229
+ child.should_receive(:save)
230
+ end
231
+ parent.save
232
+ end
233
+
234
+ it 'does not save children that have no changes and are not new when the parent is saved' do
235
+ children.each do |child|
236
+ child.stub(:new? => false)
237
+ child.stub(:changed? => false)
238
+ child.should_not_receive(:save)
239
+ end
240
+ parent.save
241
+ end
242
+ end
243
+
244
+ context "for a document with a many linked association" do
245
+ before(:all) do
246
+ # check assumptions of these examples
247
+ Widget.associations[:widget_parts].should be_many
248
+ Widget.associations[:widget_parts].should be_linked
249
+ end
250
+
251
+ it_behaves_like "saving a parent document with linked child documents" do
252
+ let(:parent) { Widget.new(:name => 'fizzbuzz') }
253
+ let(:children) { %w[ fizz buzz ].map { |n| WidgetPart.new(:name => n) } }
254
+
255
+ before(:each) do
256
+ children.each { |c| parent.widget_parts << c }
257
+ end
258
+ end
259
+ end
260
+
261
+ describe "for a document with a one linked association" do
262
+ before(:all) do
263
+ # check assumptions of these examples
264
+ Invoice.associations[:customer].should be_one
265
+ Invoice.associations[:customer].should be_linked
266
+ end
267
+
268
+ it_behaves_like "saving a parent document with linked child documents" do
269
+ let(:parent) { Invoice.new }
270
+ let(:children) { [Customer.new] }
271
+
272
+ before(:each) do
273
+ parent.customer = children.first
274
+ end
275
+ end
276
+ end
277
+
278
+ shared_examples_for "embedded association persistence logic" do
279
+ before(:each) do
280
+ @backend.stub(:store_object)
281
+ end
282
+
283
+ it "does not save children when the parent is saved" do
284
+ children.each do |child|
285
+ child.stub(:new? => true, :changed? => true)
286
+ child.should_not_receive(:save)
287
+ end
288
+
289
+ parent.save
290
+ end
291
+ end
292
+
293
+ describe "for a document with a many embedded association" do
294
+ before(:all) do
295
+ # check assumptions of these examples
296
+ Clock.associations[:modes].should be_many
297
+ Clock.associations[:modes].should be_embedded
298
+ end
299
+
300
+ it_behaves_like "embedded association persistence logic" do
301
+ let(:parent) { Clock.new }
302
+ let(:children) { [1, 2].map { |i| Mode.new } }
303
+
304
+ before(:each) do
305
+ children.each { |c| parent.modes << c }
306
+ end
307
+ end
308
+ end
309
+
310
+ describe "for a document with a one embedded association" do
311
+ before(:all) do
312
+ # check assumptions of these examples
313
+ Parent.associations[:child].should be_one
314
+ Parent.associations[:child].should be_embedded
315
+ end
316
+
317
+ it_behaves_like "embedded association persistence logic" do
318
+ let(:parent) { Parent.new }
319
+ let(:children) { [Child.new(:name => 'Bobby', :age => 9)] }
320
+
321
+ before(:each) do
322
+ parent.child = children.first
323
+ end
324
+ end
325
+ end
326
+ end