forked from mongoid/mongoid-history
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working on a better fix than mongoid#191
- Loading branch information
Showing
3 changed files
with
133 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
spec/integration/nested_embedded_documents_tracked_in_parent_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
require 'spec_helper' | ||
|
||
describe Mongoid::History::Tracker do | ||
describe 'Tracking of changes from embedded documents' do | ||
before :each do | ||
# Child model (will be embedded in Parent) | ||
class Child | ||
include Mongoid::Document | ||
include Mongoid::History::Trackable | ||
|
||
store_in collection: :child | ||
|
||
field :name | ||
embedded_in :parent, inverse_of: :child | ||
end | ||
|
||
# Parent model (embeds one Child) | ||
class Parent | ||
include Mongoid::Document | ||
include Mongoid::History::Trackable | ||
|
||
field :name, type: String | ||
embeds_one :child | ||
|
||
store_in collection: :parent | ||
|
||
track_history( | ||
on: %i[fields embedded_relations], | ||
version_field: :version, | ||
track_create: true, | ||
track_update: true, | ||
track_destroy: false, | ||
modifier_field: nil | ||
) | ||
end | ||
end | ||
|
||
after :each do | ||
Object.send(:remove_const, :Parent) | ||
Object.send(:remove_const, :Child) | ||
end | ||
|
||
it 'tracks history for nested embedded documents in parent' do | ||
p = Parent.new(name: 'bowser') | ||
p.child = Child.new(name: 'todd') | ||
p.save! | ||
expect(p.history_tracks.length).to eq(1) | ||
change = p.history_tracks.last | ||
aggregate_failures do | ||
expect(change.modified['name']).to eq('bowser') | ||
expect(change.modified['child']['name']).to eq('todd') | ||
end | ||
|
||
p.update_attributes(name: 'brow') | ||
expect(p.history_tracks.length).to eq(2) | ||
|
||
p.child.name = 'mario' | ||
p.save! | ||
expect(p.history_tracks.length).to eq(3) | ||
require 'byebug' | ||
aggregate_failures do | ||
expect(p.history_tracks.last.original['child']['name']).to eq('todd') | ||
expect(p.history_tracks.last.modified['child']['name']).to eq('mario') | ||
end | ||
end | ||
end | ||
end |