diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 7bd7def..babce8d 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -31,7 +31,7 @@ Metrics/AbcSize: # Offense count: 122 # Configuration parameters: CountComments, ExcludedMethods. Metrics/BlockLength: - Max: 837 + Max: 900 # Offense count: 1 # Configuration parameters: CountComments. @@ -56,7 +56,7 @@ Metrics/MethodLength: # Offense count: 2 # Configuration parameters: CountComments. Metrics/ModuleLength: - Max: 191 + Max: 200 # Offense count: 6 Metrics/PerceivedComplexity: diff --git a/CHANGELOG.md b/CHANGELOG.md index b5134bb..80c58e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ### 0.8.4 (Next) +* [#249](https://github.com/mongoid/mongoid-history/pull/249): Don't update version on embedded documents if the document itself is being destroyed - [@getaroom](https://github.com/getaroom). * [#248](https://github.com/mongoid/mongoid-history/pull/248): Don't update version on embedded documents if an ancestor is being destroyed in the same operation - [@getaroom](https://github.com/getaroom). * Your contribution here. diff --git a/lib/mongoid/history/trackable.rb b/lib/mongoid/history/trackable.rb index b735ffc..f798523 100644 --- a/lib/mongoid/history/trackable.rb +++ b/lib/mongoid/history/trackable.rb @@ -325,9 +325,13 @@ def track_history_for_action?(action) track_history? && !(action.to_sym == :update && modified_attributes_for_update.blank?) end + def increment_current_version?(action) + action != :destroy && !ancestor_flagged_for_destroy?(_parent) + end + def track_history_for_action(action) if track_history_for_action?(action) - current_version = ancestor_flagged_for_destroy?(_parent) ? next_version : increment_current_version + current_version = increment_current_version?(action) ? increment_current_version : next_version last_track = self.class.tracker_class.create!( history_tracker_attributes(action.to_sym) .merge(version: current_version, action: action.to_s, trackable: self) diff --git a/spec/integration/integration_spec.rb b/spec/integration/integration_spec.rb index 5825a00..5ab85f5 100644 --- a/spec/integration/integration_spec.rb +++ b/spec/integration/integration_spec.rb @@ -509,8 +509,8 @@ class Foo < Comment it 'should be possible to destroy after re-create embedded from parent' do comment.destroy - post.history_tracks.last.undo!(user) - post.history_tracks.last.undo!(user) + post.history_tracks[-1].undo!(user) + post.history_tracks[-1].undo!(user) post.reload expect(post.comments.count).to eq(0) end diff --git a/spec/unit/trackable_spec.rb b/spec/unit/trackable_spec.rb index bcaf4e0..82d09d2 100644 --- a/spec/unit/trackable_spec.rb +++ b/spec/unit/trackable_spec.rb @@ -902,6 +902,47 @@ class EmbOne expect(names_of_destroyed).to include 'child 2' end end + + context 'with multiple embeds_many models' do + let(:m) do + MyDeeplyNestedModel.create!( + children: [ + MyNestableModel.new( + name: 'parent', + children: [ + MyNestableModel.new(name: 'child 1'), + MyNestableModel.new(name: 'child 2'), + MyNestableModel.new(name: 'child 3') + ] + ) + ] + ) + end + + let(:attributes) do + { + 'children_attributes' => [ + { + 'id' => m.children[0].id, + 'children_attributes' => [ + { 'id' => m.children[0].children[0].id, '_destroy' => '0' }, + { 'id' => m.children[0].children[1].id, '_destroy' => '1' }, + { 'id' => m.children[0].children[2].id, '_destroy' => '1' } + ] + } + ] + } + end + + subject(:updated) do + m.update_attributes attributes + m.reload + end + + it 'does not corrupt the document' do + expect(updated.children[0].children.length).to eq(1) + end + end end describe '#track_create' do