Reputation: 7210
I have quite an odd usecase for PaperTrail. In my scenario a version is based on a physical event happening. Due to this, versions of my model are only created when I say.
Because of the above. I wish those versions to be capable of the following things:
When I run something like Model.all
, this should ignore versions as expected but in some cases I wish versions to be returned as normal so I need to be able to do something like Model.withVersions
to be returned all the current records and all associated versions. Is this possible?
Also, I need to be able to build a page for versions. Ie: Rails Show Action and View.
How would I accomplish this? I need to be able to craft a link to one of the pages that would output something similar to /model/23
which would take you to a page about a particular version of that item.
I know this is sort of an odd issue but wondering if anyone would have any input on this strange usecase and how I can sometimes bring versions to the foreground to act like existing things and othertimes to stay in the background as normal.
Upvotes: 1
Views: 1015
Reputation: 27961
Basically what we're doing here is using the fact that PaperTrail adds a new method to your model called versions
which returns an array of PaperTrail::Version
objects, one for each version of the model. On any of those version objects you can call .reify
to get the model at that version.
Now, I'm not sure how you'd want those versions to be returned, so let's say we create an array of versions for each model and store them in a hash keyed by the primary keys of each model:
hash = {}
Model.all.each { |m| hash[m.id] = m.versions.map(&:reify) }
# so to see the array of all versions of model with id=1
hash[1]
Re your show page, you will need to pass the version as a parameter, eg. /model/23?version=5
and then in your show
action you'll do something like:
@model = Model.find(params[:id])
if params[:version]
@model = @model.versions[params[:version]-1].reify
end
Upvotes: 1