-
Notifications
You must be signed in to change notification settings - Fork 130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes to embedded documents not tracked in parent history #187
Comments
The changes with First I would change Finally, try to build a short repro in a spec. One thing that comes to mind - is |
I tried with In my application, |
@mpetazzoni i tried to replicate the behavior by setting up a minimal example. There is a gem mongoid_relations_dirty_tracking to see the embedded documents changes in parent object's Another solution on SO. It might take some more time to try these solutions on my end. I will let you know if i find a fix. Please let us know, if any of the above solutions work for you. |
I have patched for support of Rails5/Mongoid6 mongoid_relations_dirty_tracking here. Problem is that test is still failing even when examples from mongoid_relations_dirty_tracking are fine. |
Any news about it? How you solved it? |
#191 seems like it's almost there, but not ready to merge |
How it should work actually? For example in case Edit: After investigation, I think, that there should be all embedded objects, not changed only. |
My temporary solution is: # app/models/concerns/mongoid/embedded_dirty_tracking.rb: module Mongoid module EmbeddedDirtyTracking extend ActiveSupport::Concern included do after_initialize :store_embedded_shadow after_save :store_embedded_shadow end def store_embedded_shadow @embedded_shadow = {} self.class.tracked_embedded.each do |rel_name| @embedded_shadow[rel_name] = tracked_embedded_attributes(rel_name).dup end end def embedded_changes changes = {} @embedded_shadow.each do |rel_name, shadow_values| current_values = tracked_embedded_attributes(rel_name) if current_values != shadow_values changes[rel_name] = [shadow_values, current_values] end end changes end def embedded_changed? !embedded_changes.empty? end def changed_with_embedded? changed? or embedded_changed? end def changes_with_embedded (changes || {}).merge(embedded_changes) end def tracked_embedded_attributes(rel_name) values = nil case relations[rel_name].macro when :embeds_one values = send(rel_name)&.attributes&.clone || {} when :embeds_many values = Array.new values += send(rel_name).map { |child| child.attributes.clone } end values end module ClassMethods def tracked_embedded relations.select {|_, rel| %i[embeds_one embeds_many].include?(rel.macro) }.keys end end end end # app/models/concerns/trackable.rb module Trackable extend ActiveSupport::Concern included do include Mongoid::EmbeddedDirtyTracking include Mongoid::History::Trackable track_history on: [:fields, :embedded_relations], except: [:_keywords], changes_method: :changes_with_embedded end end # app/models/city.rb class City include Mongoid::Document include Trackable field :shortcut, type: String field :name, type: String embeds_many :excursions, cascade_callbacks: true accepts_nested_attributes_for :excursions, allow_destroy: true end # app/models/excursion.rb class Excursion include Mongoid::Document field :name, type: String embedded_in :city end Inspired from https://github.com/Polarion/mongoid_relations_dirty_tracking |
Partial fix in #232, please try HEAD! |
No joy with HEAD. This seems an issue for mongoid - Dirty should track changes when embedded documents are changed via the Failing example: parent.posts_attributes= {"0": {subject: "test"}}
parent.save
parent.changes
=> {} the result from |
I'm trying to track all changes to a model object and its embedded documents, as described in the "Include embedded objects attributes in parent audit" section of the README.
My models roughly looks like this:
Changes to fields of the parent are correctly tracked in the parent's history, but not changes to fields of the child.
Am I misunderstanding how the tracking of embedded relations is supposed to work? Does it only track a change to the reference itself, for example if I change
p.child
to a completely different child object?Is there a way for me to accomplish what I'm looking for here, that is to have a single audit trail of parent document that shows all changes to all its fields, and all the fields of its embedded relations?
I was expecting #150 to provide this functionality.
The text was updated successfully, but these errors were encountered: