mongoid-history
mongoid-history copied to clipboard
A tracker on an embeds_many relation is being limited by the parent configuration
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
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?