toystore-mongo 0.8.0 → 0.8.1

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.
@@ -81,8 +81,14 @@ module Toy
81
81
  def atomic_update_attributes(attrs={})
82
82
  self.attributes = attrs
83
83
  if valid?
84
- atomic_update('$set' => persistable_changes)
85
- true
84
+ run_callbacks(:save) do
85
+ run_callbacks(:update) do
86
+ criteria = {'_id' => id}
87
+ update = {'$set' => persistable_changes}
88
+ store.client.update(criteria, update, {:safe => store.options[:safe]})
89
+ true
90
+ end
91
+ end
86
92
  else
87
93
  false
88
94
  end
@@ -1,5 +1,5 @@
1
1
  module Toy
2
2
  module Mongo
3
- VERSION = '0.8.0'
3
+ VERSION = '0.8.1'
4
4
  end
5
5
  end
@@ -135,9 +135,41 @@ describe Toy::Mongo::Querying do
135
135
  end
136
136
 
137
137
  it "only persists changes" do
138
- @user.should_receive(:atomic_update).with('$set' => {'name' => 'Frank'})
138
+ query = {'_id' => @user.id}
139
+ update = {'$set' => {'name' => 'Frank'}}
140
+ @user.store.client.should_receive(:update).with(query, update, {:safe => nil})
139
141
  @user.atomic_update_attributes(:name => 'Frank')
140
142
  end
143
+
144
+ it "persists changes with safe option from store" do
145
+ User.store(:mongo, STORE, :safe => true)
146
+ query = {'_id' => @user.id}
147
+ update = {'$set' => {'name' => 'Frank'}}
148
+ @user.store.client.should_receive(:update).with(query, update, {:safe => true})
149
+ @user.atomic_update_attributes(:name => 'Frank')
150
+ end
151
+
152
+ it "runs callbacks in correct order" do
153
+ doc = User.create.tap(&:clear_history)
154
+ doc.name = 'John Nunemaker'
155
+ doc.atomic_update_attributes
156
+ doc.history.should == [:before_save, :before_update, :after_update, :after_save]
157
+ end
158
+
159
+ it "persists changes that happen in callbacks" do
160
+ User.class_eval do
161
+ attribute :name_lower, String
162
+
163
+ before_save do |record|
164
+ record.name_lower = name
165
+ end
166
+ end
167
+
168
+ user = User.create(:name => 'Frank')
169
+ user.atomic_update_attributes('name' => 'John')
170
+ user.name_lower.should == 'John'
171
+ user.reload.name_lower.should == 'John'
172
+ end
141
173
  end
142
174
 
143
175
  describe "#get" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toystore-mongo
3
3
  version: !ruby/object:Gem::Version
4
- hash: 63
4
+ hash: 61
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 8
9
- - 0
10
- version: 0.8.0
9
+ - 1
10
+ version: 0.8.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Nunemaker
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-23 00:00:00 Z
18
+ date: 2011-09-27 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: plucky