Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dirty before callbacks #667

Merged
merged 2 commits into from Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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