mongoid-history icon indicating copy to clipboard operation
mongoid-history copied to clipboard

A tracker on an embeds_many relation is being limited by the parent configuration

Open vanboom opened this issue 4 years ago • 1 comments

In tracker.rb...

    def tracked_changes
        @tracked_changes ||= (modified.keys | original.keys).inject(HashWithIndifferentAccess.new) do |h, k|
          h[k] = { from: original[k], to: modified[k] }.delete_if { |_, vv| vv.nil? }
          h
        end.delete_if { |k, v| v.blank? || !trackable_parent_class.tracked?(k) }
      end

The !trackable_parent_class.tracked?(k) is causing tracked attributes on the child relation to be removed if the attribute is not whitelisted in the parent. This seems to be a logic error.

For example:

class User
   field :name
   embeds_many :posts, cascade_callbacks: true
   track_history
end
class Post
   field :body
   embedded_in :user
   track_history
end

> user.posts.first.history_tracks.first.tracked_edits 

tracked_edits returns nothing unless body is whitelisted in the parent model like this...

class User
  field :name
  embeds_many :posts, cascade_callbacks: true
  track_history on: [:body]
end

Attempting to whitelist the attribute for the embedded child does not work...

class User
  field :name
  embeds_many :posts, cascade_callbacks: true
  track_history on: [:body, posts: [:body]]
end

vanboom avatar Jun 13 '20 21:06 vanboom

This exists for a reason I am sure, but will need a closer look. PR a fix with some specs and let's see what existing specs fail?

dblock avatar Jun 14 '20 01:06 dblock