Ash FrameworkAF
Ash Framework9mo ago
12 replies
aidalgol

Rendering AshPaperTrails

Is there a good way to render the change history for a resource recorded by AshPaperTrail?
Solution
In my Member ressource, I have:
paper_trail do
    primary_key_type :uuid
    only_when_changed? true
    change_tracking_mode :changes_only
    store_action_name? true
    store_action_inputs? true
    ignore_attributes [:inserted_at, :updated_at]
    ignore_actions [:destroy]
    # for multitenancy
    attributes_as_attributes [:organization_id]
    belongs_to_actor :actor, CauseBeacon.Accounts.User, public?: false
  end

Note:
- in :actor the person who modify the resource is stored
- :store_action_name is very usefull to know which action has been called

The status of my member is an Enum:
  use Ash.Type.Enum,
    values: [
      invited: gettext("Invited"),
      validated: gettext("Pending (Connection)"),
      connected: gettext("Pending (Validation)"),
      waiting_list: gettext("Waiting List"),
      activated: gettext("Active"),
      left: gettext("Left")
    ]


Now, when I load my member data in my liveview, I do:

member =
      Ash.load!(
        assigns.member,
        [
          :connections,
          paper_trail_versions:
            CauseBeacon.Organizations.Member.Version
            |> Ash.Query.load(:actor)
        ],
        actor: assigns.current_user
      )


and after I am doing the processing of the version like:

membership_history =
      member.paper_trail_versions
      |> Enum.filter(fn version -> Map.has_key?(version.changes, "status") end)
      |> Enum.map(....) 
Was this page helpful?