active_snapshot
active_snapshot copied to clipboard
How to exclude certain attributes from parent record when creating a snapshot
Hi, I would like to exclude certain attributes from parent record (not snapshot children) when creating a snapshot and I would like to know if there is an easy way to accomplish that without monkey-patching the gem. The line of code involved in this issue is https://github.com/westonganger/active_snapshot/blob/master/lib/active_snapshot/models/snapshot.rb#L29. I was thinking about making instance.attributes
to return all attributes except the ones I define, but I didn't find any ActiveRecord method to unset an attribute accessor.
Maybe should I create a wrapper class that rewrites the attributes
method?
Or maybe it would be better that the gem allowed an optional argument to explicitly pass the attributes you want to include in the snapshot, instead of assuming that you always want all instance.attributes
. Is it a change you would be open to include in the gem?
Thanks
Yes I suppose we could add the capability to handle this case
has_snapshot_children do
{
comments: {
records: self.comments,
attributes: [:name, :number],
}
}
end
We don't read has_snapshot_children
upon restore though, so the attributes handling will have to be handled upon save of the snapshots only. Therefore, If you ever change the list of attributes to restore after existing snapshots have been created, you would have to run a script to modify the db attributes of your old snapshot items.
Thanks for the details👍 For now I just want to define attributes when creating a snapshot item record, not when restoring, but both approaches make sense.
What I see is that you are defining the attributes of the children, but I would like to define the ones of the parent. I guess I could add an optional argument attributes
to create_snapshot!
method.
Yeah I suppose that would be what is required for this.
Also I suppose to be full featured we would probably want to support only
/except
I would probably want to add a class model method snapshot_options only: [:foo, :bar], children: ->(){ ... }
to handle the parents attributes. Might as well allow the :children
argument here too instead of has_snapshot_children
if this is used.
I dont think that attribute level tracking fits in the current vision of this project. We currently have a methodology of delete/import for restores, adding attribute tracking would break this mold. I also feel the complexity added would not be worth the benefit for most generic use-cases.
As this gem is meant to be easily extracted and customized, anyone can feel free to implement the necessary logic for this in their app. Seems to me the logic surrounding this could be quite complex and custom per app.
Closing.