PaperTrail手动创建版本 [英] PaperTrail Manually create version
问题描述
我有一个项目电子表格,我将其转换为CSV并使用自定义导入脚本导入到基于Rails的应用程序中.
电子表格中的每条记录都包含一行,但是有些行保留了以前各行的不同版本.
在导入CSV时,我目前使用"past_version"字段标记第二行,但我现在认为实现完整版本的gem将是一种更好的解决方法.
我一直在阅读PaperTrail的文档,它看起来很适合我的需求,但是,我需要创建一些行的版本作为导入脚本的一部分.可以用PaperTrail做到吗?
基本上,我需要开始导入,例如,正常添加记录1、2、3、4、5,然后记录6实际上是记录2的较新版本,因此我现在需要手动创建PaperTrail版本./p>
这可能吗?
@Flori的touch_with_version方法有效,但是当他们将touch
设为可记录事件时,paper_trail弃用了此方法.
在最新版本的paper_trail上,您可以执行以下操作:
model.paper_trail.save_with_version
如果您使用的是旧版本,并且此版本不可用,则可以使用record_update
方法(这是save_with_version
在内部使用的方法):
model.paper_trail.record_update(force: true, in_after_callback: false)
无法自定义事件...您可以创建,还原,更新和触摸,但至少在没有大量猴子补丁或其他操作的情况下,不能做为手动"记录之类的事情.
即使这样做也有些背叛,因为您调用的某些内部组件可能会在将来的版本中发生变化.它还具有一些必需的参数,这些参数已随版本的不同而变化.
就我而言,我正在一起使用paper_trail和妄想症,并希望在还原时记录一个明确的版本,除非您执行上述操作,否则似乎绕过了paper_trail.
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?
@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.
这篇关于PaperTrail手动创建版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!