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.
Add spec file to test tracking history of embedded relations in parent
Demonstrates mongoid#187.
- Loading branch information
1 parent
e060205
commit df4bd12
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
require 'spec_helper' | ||
|
||
describe Mongoid::History::Tracker do | ||
before :all do | ||
class Child | ||
include Mongoid::Document | ||
include Mongoid::History::Trackable | ||
|
||
field :name | ||
embedded_in :parent, inverse_of: :child | ||
end | ||
|
||
class Parent | ||
include Mongoid::Document | ||
include Mongoid::History::Trackable | ||
|
||
field :name, type: String | ||
embeds_one :child | ||
|
||
track_history on: [:fields, :embedded_relations], # track title and body fields only, default is :all | ||
modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier | ||
modifier_field_inverse_of: :nil, | ||
version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version | ||
track_create: false, # track document creation, default is false | ||
track_update: true, # track document updates, default is true | ||
track_destroy: false # track document destruction, default is false | ||
end | ||
end | ||
|
||
it 'should be able to track history for nested embedded documents in parent' do | ||
p = Parent.new(name: 'bowser') | ||
p.child = Child.new(name: 'todd') | ||
p.save! | ||
|
||
p.child.name = 'mario' | ||
p.save! | ||
|
||
changes = p.history_tracks.reverse | ||
expect(changes.length).to eq(1) | ||
expect(changes.first.modified['name']).to eq('mario') | ||
end | ||
|
||
after :all do | ||
Object.send(:remove_const, :Parent) | ||
Object.send(:remove_const, :Child) | ||
end | ||
end |