Paper_trail gem 能力

Paper_trail gem abilities

我想知道使用papertrail gem是否可以实现以下用例? 具有登录用户的维基页面的维基百科类型的应用程序可以 change/edit 并且其中:

  1. 版主可以撤消特定更改:
    我知道 papertrail 允许回滚到以前的版本,但我在这里问的有点不同。即,撤消 特定 edit/change 的能力。假设已经有三个edits/versions到一个record/wiki-page。那么如果你想撤消编辑 2,那么编辑 1 和 3 的更改应该仍然存在。但是如果你回滚到编辑 2 之前的版本,那么编辑 3 也会被撤消,这不是我想要的。

  2. 用户所做的更改(贡献)反馈到用户的个人资料中,然后将概述该用户所做的 changes/contributions:
    我相信这可以使用 --with-changes 选项(它注册除了已更改资源的完整转储之外所做的更改)结合 papertrail 注册已进行更改的用户这一事实。我的理解正确吗?
    在教程 http://samurails.com/gems/papertrail/ 中,我读到有关将 papertrail 与 gem diffy 结合使用来确定确切更改的内容,但我不明白的是为什么教程使用 diffy 当 papertrail 本身已经提供了“diffing”功能时?

  3. 让版主首先接受一些用户的更改,在更改实际实施之前(即在实际应用更改之前):
    papertrail 也可以帮助实现这个功能吗?

1。版主可以撤消特定更改

您可以使用以下模块实现此功能:

module Revertible
   SKIP_FIELDS = [ 'updated_at' ]

   def revert_to(version)
     raise 'not version of this model' unless self == version.item
     changes = version.changeset.select{ |k, v| not SKIP_FIELDS.include?(k) }.map{ |k,v| [k.to_sym, v[0]] }.to_h
     self.update_attributes(changes)
  end
end

它向模型添加了 revert_to 方法,允许版主仅撤消特定编辑中的更改。注意SKIP_FIELDS数组,其中排除了几个系统字段,不应该还原。

我们可以很容易地测试这个模块。让我们创建一个 table:

create_table :articles do |t|
  t.string :title
  t.string :body

  t.timestamps null: false
end

和关联模型:

class Article < ActiveRecord::Base
  include Revertible
  has_paper_trail
end

以下测试用例显示,仅还原了特定于版本的编辑:

class ArticleTest < ActiveSupport::TestCase
  test "rollback specific edit" do
    article = Article.create(title: 'My Article 1', body: 'first version')
    article.update_attributes(title: 'My Article 1', body: 'second version')
    article.update_attributes(title: 'My Article 3', body: 'third version')

    assert_equal 3, article.versions.count
    assert_equal 'My Article 3', article.title
    assert_equal 'third version', article.body

    article.revert_to article.versions[1]

    assert_equal 4, article.versions.count
    assert_equal 'My Article 3', article.title # title haven't changed
    assert_equal 'first version', article.body # body did change
  end
end

2.Changes 由用户

做出(贡献)

要打开更改跟踪,请将以下方法添加到您的应用程序控制器:

class ApplicationController
  def user_for_paper_trail
    user = current_user
    return 'public' if user.blank?
    user.username
  end
end

现在可以轻松跟踪特定用户所做的更改:

versions = PaperTrail::Version.where(whodunnit: 'dimakura')
version = versions.first
version.item # => #<Article id: 1, title: "...", body: "...">
version.event # => "create"
version.changeset

迪菲

关于你关于 diffy 的问题。如果您唯一需要的是获得两个相邻版本之间的差异,那么您实际上并不需要它。但是,如果您需要比较由多个编辑分隔的版本之间的变化,那么您确实需要 diffy 或任何类似的库。

版主接受更改

我认为在单一领域实施起来并不容易。您可能需要为 "accepted" 和 "raw" 数据设置两列,甚至可能需要两个不同的模型。

我想我已经回答了你所有的问题,对你很有帮助。