paper-trail-gem / paper_trail

Track changes to your rails models
MIT License
6.78k stars 898 forks source link

Ability to conditionally set metadata on a version record depending on the event (create, update, etc) #1463

Closed thatandyrose closed 5 months ago

thatandyrose commented 8 months ago

Is your feature suggestion related to a problem? Please describe.

It is not related to a problem or bug. It's an improvement I believe could be helpful to those using the meta columns feature.

Describe the solution you'd like to build

I would like to be able to conditionally set metadata on a version record depending on the event (create, update, etc).

I believe the best way to do this is to pass an additional parameter to the proc that can be set on the meta attributes. I believe the only change needed would be here:

https://github.com/paper-trail-gem/paper_trail/blob/47dbc2271c7055fde6f0fe7d3c8bf44f62cbef63/lib/paper_trail/events/base.rb#L220

To change:

value.call(@record)

to

value.call(@record, event)

This should be backwards compatible.

Having this change would then enable the user to check the event type, and change what metadata is being stored.

I'd be happy to make this change.. however not sure how to test it 🤔 Probably add a new context in the widget model spec for a model that has an event conditional meta data saving definition in there? 🤷🏽 open to suggestions.

Describe alternatives you've considered

The alternative I'm toying with at the moment is just to always save the metadata.. which for me would be expensive since it's a large json object.. I don't want to do it in every event. OR, I'm thinking of doing an inspect on caller and checking to see what build_version_on_* method is being called. It's not great or 100% but should be enough for my needs.. so feels yuk and brittle :-)

github-actions[bot] commented 5 months ago

This issue has been automatically marked as stale due to inactivity. The resources of our volunteers are limited. Bug reports must provide a script that reproduces the bug, using our template. Feature suggestions must include a promise to build the feature yourself. Thank you for all your contributions.