Skip to content

Commit

Permalink
Fix for mongoid3.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikwat authored and dblock committed Jan 18, 2018
1 parent 895367e commit c2ff8d2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions lib/mongoid/history/trackable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,22 +311,31 @@ def track_has_and_belongs_to_many(related)
else
original_ids + [related_id]
end

modified = { metadata.key => modified_ids }
original = { metadata.key => original_ids }

current_version = (version || 0) + 1
set(version: current_version)

action = :update
self.class.tracker_class.create!(
history_tracker_attributes(action.to_sym)
.merge(version: current_version,
.merge(version: increment_and_set_version,
action: action.to_s,
original: original,
modified: modified,
trackable: self)
)
end

private

def increment_and_set_version
if Mongoid::Compatibility::Version.mongoid3?
inc(:version, 1)
else
current_version = (version || 0) + 1
set(version: current_version)
current_version
end
end
end

module RelationMethods
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/has_and_belongs_to_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Tag
expect(post.history_tracks.last.modified).to include('tag_ids' => [tag.id, tag2.id])
end

it 'should assign empty tag_ids on original' do
it 'should assign tag_ids on original' do
expect(post.history_tracks.last.original).to include('tag_ids' => [tag.id])
end
end
Expand Down

0 comments on commit c2ff8d2

Please sign in to comment.