From 1f34a8544788c9a359081b9c3387ecc7d4987c78 Mon Sep 17 00:00:00 2001 From: Maxime Petazzoni Date: Fri, 7 Apr 2017 15:54:38 -0700 Subject: [PATCH] Add spec file to test tracking history of embedded relations in parent Demonstrates #187. --- ...bedded_documents_tracked_in_parent_spec.rb | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 spec/integration/nested_embedded_documents_tracked_in_parent_spec.rb diff --git a/spec/integration/nested_embedded_documents_tracked_in_parent_spec.rb b/spec/integration/nested_embedded_documents_tracked_in_parent_spec.rb new file mode 100644 index 00000000..21ca5dbe --- /dev/null +++ b/spec/integration/nested_embedded_documents_tracked_in_parent_spec.rb @@ -0,0 +1,51 @@ +require 'spec_helper' + +describe Mongoid::History::Tracker do + before :all do + # Child model (will be embedded in Parent) + class Child + include Mongoid::Document + include Mongoid::History::Trackable + + 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 + + track_history on: [:fields, :embedded_relations], + version_field: :version, + track_create: true, + track_update: true, + track_destroy: 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! + + expect(p.history_tracks.length).to eq(1) + change = p.history_tracks.last + expect(change.modified['name']).to eq('bowser') + expect(change.modified['child']['name']).to eq('todd') + + p.child.name = 'mario' + p.save! + + expect(p.history_tracks.length).to eq(2) + expect(p.history_tracks.last.modified['child']['name']).to eq('mario') + end + + after :all do + Object.send(:remove_const, :Parent) + Object.send(:remove_const, :Child) + end +end