couchbase-model-relationship 0.1.10 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -41,6 +41,7 @@ module Couchbase
41
41
  pristine = model.find(@id)
42
42
  update_attributes(pristine.attributes)
43
43
  @meta[:cas] = pristine.meta[:cas]
44
+ clean!
44
45
  self
45
46
  end
46
47
 
@@ -37,6 +37,17 @@ module Couchbase
37
37
  end
38
38
  end
39
39
 
40
+ def children_changed?
41
+ changed_children.present?
42
+ end
43
+
44
+ def save_with_changed_children
45
+ save_if_changed
46
+ changed_children.each do |child|
47
+ child.respond_to?(:save_with_changed_children) ? child.save_with_changed_children : child.save_if_changed
48
+ end
49
+ end
50
+
40
51
  def delete_with_autodelete_children(options = {})
41
52
  self.class.child_associations.select(&:auto_delete).each do |association|
42
53
  association.fetch(self).try :delete, options
@@ -69,6 +80,12 @@ module Couchbase
69
80
  end.compact
70
81
  end
71
82
 
83
+ def changed_children
84
+ loaded_children.select do |child|
85
+ child.changed? || (child.respond_to?(:children_changed?) ? child.children_changed? : nil)
86
+ end
87
+ end
88
+
72
89
  def reload_all
73
90
  children.each(&:reload)
74
91
  reload
@@ -1,7 +1,7 @@
1
1
  module Couchbase
2
2
  class Model
3
3
  module Relationship
4
- VERSION = "0.1.10"
4
+ VERSION = "0.1.11"
5
5
  end
6
6
  end
7
7
  end
data/spec/dirty_spec.rb CHANGED
@@ -84,6 +84,23 @@ describe "Dirty" do
84
84
  end
85
85
  end
86
86
 
87
+ describe "reloading" do
88
+ before do
89
+ stub_server DirtyTest
90
+ end
91
+
92
+ it "resets the dirty state" do
93
+ subject.name = 'abc'
94
+ subject.save
95
+
96
+ subject.name = 'def'
97
+ subject.should be_changed
98
+
99
+ subject.reload
100
+ subject.should_not be_changed
101
+ end
102
+ end
103
+
87
104
  describe "saving" do
88
105
  it "doesn't saves if not changed on request" do
89
106
  subject.stubs(changed?: false)
data/spec/parent_spec.rb CHANGED
@@ -21,6 +21,10 @@ class ParentTest < Couchbase::Model
21
21
  child :dont_load, auto_load: false
22
22
  end
23
23
 
24
+ class GrandParentTest < Couchbase::Model
25
+ child :multiple_child_test
26
+ end
27
+
24
28
  class InheritanceTest < ParentTest
25
29
  end
26
30
 
@@ -144,6 +148,44 @@ describe "parent" do
144
148
  subject.save_with_children.should eq(:saved)
145
149
  end
146
150
 
151
+ context "with dirty children" do
152
+ it "knows if it's children are dirty" do
153
+ subject = MultipleChildTest.new
154
+ subject.brother = Brother.new
155
+ subject.brother.stubs(changed?: false)
156
+ subject.sister = Sister.new
157
+ subject.sister.stubs(changed?: true)
158
+
159
+ subject.changed_children.should eq([subject.sister])
160
+ subject.should be_children_changed
161
+ end
162
+
163
+ it "knows if it's grand children are dirty" do
164
+ subject = GrandParentTest.new
165
+ subject.multiple_child_test = MultipleChildTest.new
166
+ subject.multiple_child_test.brother = Brother.new
167
+ subject.multiple_child_test.brother.stubs(changed?: false)
168
+ subject.multiple_child_test.sister = Sister.new
169
+ subject.multiple_child_test.sister.stubs(changed?: true)
170
+
171
+ subject.changed_children.should eq([subject.multiple_child_test])
172
+ subject.should be_children_changed
173
+ end
174
+
175
+ it "saves all dirty objects" do
176
+ subject = GrandParentTest.new
177
+ subject.multiple_child_test = MultipleChildTest.new
178
+ subject.multiple_child_test.brother = Brother.new
179
+ subject.multiple_child_test.brother.stubs(changed?: false)
180
+ subject.multiple_child_test.brother.expects(:save).never
181
+ subject.multiple_child_test.sister = Sister.new
182
+ subject.multiple_child_test.sister.stubs(changed?: true)
183
+ subject.multiple_child_test.sister.expects(:save)
184
+
185
+ subject.save_with_changed_children
186
+ end
187
+ end
188
+
147
189
  it "doesn't save children if the main object isn't valid" do
148
190
  subject = InvalidTest.new
149
191
  subject.brother = Brother.new
data/spec/spec_helper.rb CHANGED
@@ -139,9 +139,9 @@ module RspecHelpers
139
139
  end
140
140
 
141
141
  def stub_server(*klasses)
142
- Thread.current['mock'] = start_mock
143
- bucket = Couchbase.connect(:hostname => @mock.host, :port => @mock.port)
144
- [Post, ValidPost, Brewery, Beer, Attachment].each do |model|
142
+ Thread.current['mock'] = mock = start_mock
143
+ bucket = Couchbase.connect(:hostname => mock.host, :port => mock.port)
144
+ klasses.each do |model|
145
145
  model.bucket = bucket
146
146
  end
147
147
  bucket
@@ -187,9 +187,9 @@ RSpec.configure do |config|
187
187
  config.mock_framework = :mocha
188
188
  config.include RspecHelpers
189
189
 
190
- #config.after(:suite) do
191
- # stop_mock
192
- #end
190
+ config.after(:suite) do
191
+ Thread.current['mock'].try :stop
192
+ end
193
193
  end
194
194
 
195
195
  Mocha::Configuration.prevent :stubbing_non_existent_method
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couchbase-model-relationship
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.1.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-13 00:00:00.000000000 Z
12
+ date: 2014-02-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -218,7 +218,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
218
218
  version: '0'
219
219
  segments:
220
220
  - 0
221
- hash: 1444561710009836312
221
+ hash: 977357591952759392
222
222
  required_rubygems_version: !ruby/object:Gem::Requirement
223
223
  none: false
224
224
  requirements:
@@ -227,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
227
227
  version: '0'
228
228
  segments:
229
229
  - 0
230
- hash: 1444561710009836312
230
+ hash: 977357591952759392
231
231
  requirements: []
232
232
  rubyforge_project:
233
233
  rubygems_version: 1.8.24