Skip to content

Commit f741e5c

Browse files
author
Mateus Pontes
committed
Fix issue #188 for embedded one relations
* Fix test to check Parent and Child updates Fix: remove focus: true and debug info with puts Fix tests: add method to track changes on update with changes * Add method to track changes with changes empty
1 parent 4b2c393 commit f741e5c

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### 0.6.2 (Next)
22

33
* Your contribution here.
4+
* [#191](https://github.com/mongoid/mongoid-history/pull/191): Track changes on embed_one - [@mateuspontes](https://github.com/mateuspontes).
45

56
### 0.6.1 (2017/01/04)
67

lib/mongoid/history/attributes/update.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def attributes
1313
@attributes[k] = format_field(k, v)
1414
end
1515
end
16+
insert_embeds_one_changes_on_child if trackable_class.tracked_embeds_one.present? && changes.empty?
1617
@attributes
1718
end
1819

@@ -27,6 +28,22 @@ def insert_embeds_one_changes(relation, value)
2728
@attributes[relation][1] = value[1][paranoia_field].present? ? {} : format_embeds_one_relation(relation, value[1])
2829
end
2930

31+
def insert_embeds_one_changes_on_child
32+
trackable_class.tracked_embeds_one.each do |rel|
33+
rel_class = trackable_class.embeds_one_class(rel)
34+
paranoia_field = Mongoid::History.trackable_class_settings(rel_class)[:paranoia_field]
35+
paranoia_field = rel_class.aliased_fields.key(paranoia_field) || paranoia_field
36+
rel = aliased_fields.key(rel) || rel
37+
obj = trackable.send(rel)
38+
next if !obj || (obj.respond_to?(paranoia_field) && obj.public_send(paranoia_field).present?)
39+
@attributes[rel] = {}
40+
obj.changes.each do |k, v|
41+
@attributes[rel] = [{ k => v.first }, { k => v.last }]
42+
end
43+
end
44+
@attributes
45+
end
46+
3047
def insert_embeds_many_changes(relation, value)
3148
relation = trackable_class.database_field_name(relation)
3249
relation_class = trackable_class.embeds_many_class(relation)

spec/integration/nested_embedded_documents_tracked_in_parent_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'spec_helper'
22

3-
describe Mongoid::History::Tracker, focus: true do
3+
describe Mongoid::History::Tracker do
44
before :all do
55
# Child model (will be embedded in Parent)
66
class Child
@@ -19,7 +19,7 @@ class Parent
1919
field :name, type: String
2020
embeds_one :child
2121

22-
track_history on: %i[(fields embedded_relations)],
22+
track_history on: %i[fields embedded_relations],
2323
version_field: :version,
2424
track_create: true,
2525
track_update: true,
@@ -37,10 +37,14 @@ class Parent
3737
expect(change.modified['name']).to eq('bowser')
3838
expect(change.modified['child']['name']).to eq('todd')
3939

40+
p.update_attributes(name: 'brow')
41+
expect(p.history_tracks.length).to eq(2)
42+
4043
p.child.name = 'mario'
4144
p.save!
4245

43-
expect(p.history_tracks.length).to eq(2)
46+
expect(p.history_tracks.length).to eq(3)
47+
expect(p.history_tracks.last.original['child']['name']).to eq('todd')
4448
expect(p.history_tracks.last.modified['child']['name']).to eq('mario')
4549
end
4650

0 commit comments

Comments
 (0)