Navigation Menu

Skip to content

Commit

Permalink
Persist default values on create.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed May 20, 2012
1 parent 0bcba28 commit 4e5b22d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/toy/mongo/partial_updating.rb
Expand Up @@ -28,9 +28,13 @@ def persistable_changes

def persist
if partial_updates?
updates = persistable_changes
if new_record? || (persisted? && updates.present?)
adapter.write id, updates
if new_record?
adapter.write id, persisted_attributes
else
updates = persistable_changes
if updates.present?
adapter.write id, updates
end
end
else
super
Expand Down
9 changes: 9 additions & 0 deletions spec/toy/mongo/partial_updating_spec.rb
Expand Up @@ -145,6 +145,15 @@
user.bio.should eq('Surprise!')
end

it "persists default values that did not change" do
User.attribute :version, Integer, :default => 1
user = User.new
user.adapter.should_receive(:write).with(user.id, {
'version' => 1,
})
user.save
end

it "does not persist virtual attributes" do
User.attribute :name, String
User.attribute :password, String, :virtual => true
Expand Down

0 comments on commit 4e5b22d

Please sign in to comment.