Skip to content

Commit

Permalink
Fix issue mongoid#188 for embedded one relations
Browse files Browse the repository at this point in the history
* Fix test to check Parent and Child updates
  • Loading branch information
Mateus Pontes committed Apr 13, 2017
1 parent 4b2c393 commit e8c9352
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
49 changes: 27 additions & 22 deletions lib/mongoid/history/attributes/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,43 @@ class Update < ::Mongoid::History::Attributes::Base
def attributes
@attributes = {}
changes.each do |k, v|
if trackable_class.tracked_embeds_one?(k)
insert_embeds_one_changes(k, v)
elsif trackable_class.tracked_embeds_many?(k)
insert_embeds_many_changes(k, v)
elsif trackable_class.tracked?(k, :update)
if trackable_class.tracked?(k, :update)
@attributes[k] = format_field(k, v)
end
end
insert_embeds_one_changes
# insert_embeds_many_changes
@attributes
end

private

def insert_embeds_one_changes(relation, value)
relation = trackable_class.database_field_name(relation)
relation_class = trackable_class.embeds_one_class(relation)
paranoia_field = Mongoid::History.trackable_class_settings(relation_class)[:paranoia_field]
@attributes[relation] = []
@attributes[relation][0] = value[0][paranoia_field].present? ? {} : format_embeds_one_relation(relation, value[0])
@attributes[relation][1] = value[1][paranoia_field].present? ? {} : format_embeds_one_relation(relation, value[1])
def insert_embeds_one_changes
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)
paranoia_field = Mongoid::History.trackable_class_settings(relation_class)[:paranoia_field]
@attributes[relation] = []
@attributes[relation][0] = value[0].reject { |rel| rel[paranoia_field].present? }
.map { |v_attrs| format_embeds_many_relation(relation, v_attrs) }
@attributes[relation][1] = value[1].reject { |rel| rel[paranoia_field].present? }
.map { |v_attrs| format_embeds_many_relation(relation, v_attrs) }
end
# def insert_embeds_many_changes(relation, value)
# relation = trackable_class.database_field_name(relation)
# relation_class = trackable_class.embeds_many_class(relation)
# paranoia_field = Mongoid::History.trackable_class_settings(relation_class)[:paranoia_field]
# @attributes[relation] = []
# @attributes[relation][0] = value[0].reject { |rel| rel[paranoia_field].present? }
# .map { |v_attrs| format_embeds_many_relation(relation, v_attrs) }
# @attributes[relation][1] = value[1].reject { |rel| rel[paranoia_field].present? }
# .map { |v_attrs| format_embeds_many_relation(relation, v_attrs) }
# end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,16 @@ 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)
p p.history_tracks.to_a

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 e8c9352

Please sign in to comment.