是否可以在PaperTrail中还原版本更改而不会覆盖之后的更改?

时间:2018-08-09 19:46:21

标签: ruby-on-rails ruby paper-trail-gem database-versioning

我有一个Rails项目,该项目具有使用paper_trail gem进行版本控制的模型。

我希望在还原版本时,我只想更改要还原的特定版本中更改的字段,而不希望撤消随后版本的不同字段的更改。

例如,假设我有一个Person模型,其中包含以下字段:名称,favourite_color和age。

# Let's say this doesn't create any version
Person.create(name: 'John', age: 20, favorite_color: 'Green') 

# This creates a version (V1) with the changeset: 20 => 21
Person.update_attributes(age: 21)

# This creates another version (V2) with the changeset: 'Green' => 'Blue'                
Person.update_attributes(favorite_color: 'Blue')

# This creates another version (V3) with the changeset: 'John' => 'James'
# This is also the latest version now
Person.update_attributes(name: 'James')

我要寻找的功能是,如果我恢复到V1,以便使Person的年龄回到20岁,我不想撤消随后的更改(即,喜欢的颜色更改和名称更改)。因此,我想有一种更好的说法是我只想撤消该特定字段的更改。 (在这种情况下,Person包含age: 20favorite_color: 'Blue'name: 'James')。

我觉得这将是PaperTrail已经支持的东西。我仔细阅读了文档和代码中的内容,搜索了Google,看了过去的问题,但没有发现任何问题。

所以我真正的问题是:我是否想念它,还是PaperTrail不支持该功能?

直到我确定这是否是PaperTrail真正支持的功能为止,我已经扩展了PaperTrail::Version来支持我称之为revert的功能。

如果没有其他兴趣,这里是代码:

# app/models/paper_trail/version.rb

module PaperTrail
  class Version < ActiveRecord::Base
    include PaperTrail::VersionConcern

    def revert
      item = self.item
      item_attributes = {}

      self.changeset.keys.each do |k|
        item_attributes[k] = self.changeset[k][0]
      end

      item.assign_attributes(item_attributes)
      item
    end
  end
end

0 个答案:

没有答案
相关问题