diff --git a/lib/mongo_mapper/plugins/dirty.rb b/lib/mongo_mapper/plugins/dirty.rb index b02057f11..dfc526b5c 100644 --- a/lib/mongo_mapper/plugins/dirty.rb +++ b/lib/mongo_mapper/plugins/dirty.rb @@ -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 diff --git a/spec/functional/dirty_with_callbacks_spec.rb b/spec/functional/dirty_with_callbacks_spec.rb new file mode 100644 index 000000000..db299070e --- /dev/null +++ b/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