I am trying to persist virtual attribute changes within object_changes and have been working with the meta option.
has_paper_trail(
meta: {
status_name: proc { |foo| foo.status&.name }
}
)
I added status_name to the versions table as the docs mention, and that attribute does get persisted. However, instead of being saved within the object_changes hash, it is in its separate column, and the value does not include the previous value.
# version attributes:
object_changes:
"{\"updated_at\":[\"2023-01-18T22:50:22.394Z\",\"2023-01-18T22:50:22.408Z\"],\"status_id\":[1,2]}",
status_name: "Status 2"
This is working as the gem intends, but I'd like to push the status_name changes into object_changes and include the previous value. One way I've been able to get the before and after changes stored is by creating a new method using the meta flag. But it seems clunky, and I'm wondering if anyone has an alternative:
has_paper_trail(
meta: {
status_name: :track_status_name_changes
}
)
def track_status_name_changes
if versions.blank?
[nil, status&.name].to_json # "[null,\"Status 1\"]"
else
prev_name = JSON.parse(versions.last.status_name).last
[prev_name, status&.name].to_json # "[\"Status 1\",\"Status 2\"]"
end
end
I'm currently exploring creating my own adapter like mentioned in the docs:
PaperTrail.config.object_changes_adapter = MyObjectChangesAdapter.new
class MyObjectChangesAdapter
# @param changes Hash
# @return Hash
def diff(changes)
# ...
end
end
Going with the custom method in the meta flag I am able to track the changes of the virtual attribute, however, I'd also like to have those changes stored in the object_changes column of the versions table.