Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -56,7 +56,7 @@ Metrics/MethodLength:
# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 191
Max: 200

# Offense count: 6
Metrics/PerceivedComplexity:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
6 changes: 5 additions & 1 deletion lib/mongoid/history/trackable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions spec/integration/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions spec/unit/trackable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down