Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PaperTrail Manually create version

I have a spreadsheet of items which I convert to CSV and import using a custom import script into my Rails based application.

The spreadsheet contains a row for each record but some rows hold different versions of previous rows.

When importing the CSV I currently mark the second row using a "past_version" field but I am now thinking that implementing a full versioning gem would be a much nicer way of going about it.

I have been reading through the docs for PaperTrail and it looks perfect for what I am after, however, I need the versions of some rows to be created as part of my import script. Can this be done with PaperTrail?

Basically I need to start an import, say record 1, 2, 3, 4, 5 are added normally, then record 6 is actually a newer version of record 2 and so I now need to manually create a PaperTrail version.

Is this possible?

like image 733
rctneil Avatar asked Jan 06 '15 18:01

rctneil


2 Answers

@Flori's touch_with_version approach worked, but paper_trail deprecated this method when they made touch a recordable event.

On the latest version of paper_trail, you can just do:

model.paper_trail.save_with_version

If you are on an older version, and this is not available, you can use the record_update method (this is what save_with_version uses internally):

model.paper_trail.record_update(force: true, in_after_callback: false)

There is no way to customize the event... you can do create, restore, update and touch but can't do something like record it as 'manual' at least not without a lot of monkey patching or something.

Even doing this is a little treacherous, since you're calling some internals that could change in future versions. It has some required params, also, that have changed from version to version.

In my case, I am using paper_trail and paranoia together and wanted to record an explicit version on restore which seems to bypass paper_trail unless you do something like the above.

like image 166
Brett Green Avatar answered Oct 01 '22 02:10

Brett Green


In case anyone stumble upon this issue: It's possible! You can call touch_with_version on any PaperTrail model like this: mymodel.paper_trail.touch_with_version. It will create a version for you.

like image 41
Flori Avatar answered Oct 01 '22 00:10

Flori