Skip to content

Commit

Permalink
Fix issue #188 for embedded one relations
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Mateus Pontes committed Apr 13, 2017
1 parent 4b2c393 commit 454e075
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### 0.6.2 (Next)

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

### 0.6.1 (2017/01/04)

Expand Down
17 changes: 17 additions & 0 deletions lib/mongoid/history/attributes/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def attributes
@attributes[k] = format_field(k, v)
end
end
insert_embeds_one_changes_on_child if trackable_class.tracked_embeds_one.present? && changes.empty?
@attributes
end

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

def insert_embeds_one_changes_on_child
trackable_class.tracked_embeds_one.each do |rel|
rel_class = trackable_class.embeds_one_class(rel)
paranoia_field = Mongoid::History.trackable_class_settings(rel_class)[:paranoia_field]
paranoia_field = rel_class.aliased_fields.key(paranoia_field) || paranoia_field
rel = aliased_fields.key(rel) || rel
obj = trackable.send(rel)
next if !obj || (obj.respond_to?(paranoia_field) && obj.public_send(paranoia_field).present?)
@attributes[rel] = {}
obj.changes.each do |k, v|
@attributes[rel] = [{ k => v.first }, { k => v.last }]
end
end
@attributes
end

def insert_embeds_many_changes(relation, value)
relation = trackable_class.database_field_name(relation)
relation_class = trackable_class.embeds_many_class(relation)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe Mongoid::History::Tracker, focus: true do
describe Mongoid::History::Tracker do
before :all do
# Child model (will be embedded in Parent)
class Child
Expand All @@ -19,7 +19,7 @@ class Parent
field :name, type: String
embeds_one :child

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

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(2)
expect(p.history_tracks.length).to eq(3)
expect(p.history_tracks.last.original['child']['name']).to eq('todd')
expect(p.history_tracks.last.modified['child']['name']).to eq('mario')
end

Expand Down

0 comments on commit 454e075

Please sign in to comment.