Skip to content

Commit

Permalink
Merge pull request #667 from ujihisa/master
Browse files Browse the repository at this point in the history
Update dirty before callbacks
  • Loading branch information
smtlaissezfaire committed Jan 19, 2021
2 parents f5322e9 + ad9e819 commit 85cc344
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/mongo_mapper/plugins/dirty.rb
Expand Up @@ -13,7 +13,7 @@ def create_accessors_for(key)
end
end

def create_or_update(*)
def save_to_collection(*)
super.tap do
changes_applied
end
Expand Down
59 changes: 59 additions & 0 deletions spec/functional/dirty_with_callbacks_spec.rb
@@ -0,0 +1,59 @@
require 'spec_helper'

describe 'DirtyWithCallbacks' do
it 'should update its changes/previous_changes before after_create/after_update callbacks' do
history = {}

document = Doc {
key :x, String

after_save {
history[:after_save] = {
changes: changes,
previous_changes: previous_changes,
}
}
after_create {
history[:after_create] = {
changes: changes,
previous_changes: previous_changes,
}
}
after_update {
history[:after_update] = {
changes: changes,
previous_changes: previous_changes,
}
}
}

d = document.new(x: 'hello')
d.save

history.should == {
after_save: {
changes: {},
previous_changes: {'x' => [nil, 'hello']},
},
after_create: {
changes: {},
previous_changes: {'x' => [nil, 'hello']},
},
}
history.clear

d.x = 'world'
d.save!

history.should == {
after_save: {
changes: {},
previous_changes: {'x' => ['hello', 'world']},
},
after_update: {
changes: {},
previous_changes: {'x' => ['hello', 'world']},
},
}
end
end

0 comments on commit 85cc344

Please sign in to comment.