izelnakri / paper_trail

Track and record all the changes in your database with Ecto. Revert back to anytime in history.
https://hex.pm/packages/paper_trail
MIT License
565 stars 92 forks source link

Is there a way to pass originator_id instead of originator struct? #154

Closed rlopzc closed 2 years ago

rlopzc commented 3 years ago

First off, nice repo very helpful!

Use case: I have the user_id inside a Merchant struct (belongs_to) and most of the times the user isn't loaded but I do have the originator_id (user_id) available. It would be better to not preload an association that is only going to use the .id when passing the originator: struct

izelnakri commented 3 years ago

Hi @romariolopezc , sorry for late response, summer times ;)

originator supposed to be "the person who does the action" so if the web request is fired by a certain individual, you should already have the person available within that request. However a developer should be able to insert a new PaperTrail.Version by just just providing originator_id instead of originator during PaperTrail.insert or update or delete, you are indeed right this might not be supported currently: https://github.com/izelnakri/paper_trail/blob/main/lib/paper_trail/serializer.ex#L27

I'm willing to merge a PR that includes this functionality if one provides with a test.

jkmrto commented 2 years ago

It looks like it is possible to pass a map with only the originator ID instead of the whole struct like originator: %{id: id}.

You can see it here -> https://github.com/izelnakri/paper_trail/blob/main/lib/paper_trail/serializer.ex#L26

izelnakri commented 2 years ago

as @jkmrto pointed out, this feature is currently possible, thus I'm closing this issue. Please let me know if you'd like it to be reopened @rlopzc !