Skip to content

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

Open
@vanboom

Description

@vanboom

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

Activity

added a commit that references this issue on Jun 13, 2020

fix mongoid#244 embedded attributes blocked by parent

112e49c
dblock

dblock commented on Jun 14, 2020

@dblock
Collaborator

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?

added a commit that references this issue on Jul 22, 2020

fix mongoid#244 embedded attributes blocked by parent

55a432a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

      Participants

      @vanboom@dblock

      Issue actions

        A tracker on an embeds_many relation is being limited by the parent configuration · Issue #244 · mongoid/mongoid-history