Paper_trail宝石的能力 [英] Paper_trail gem abilities
问题描述
我想知道下面的使用案例是否可以使用 papertrail 宝石来实现?
维基百科类型的应用程序和登录用户的维基页面可以更改/编辑,其中:
-
版主可以撤消具体的变化:
我理解papertrail允许回滚到以前的版本,但我在这里问的是有些不同。也就是说,撤消特定编辑/更改的功能。假设有一个记录/维基页面有三个编辑/版本。然后,如果要撤消编辑2,则编辑1和编辑3的更改仍应保留。但是,如果您要回滚到编辑2之前的版本,那么编辑3也会撤消,这不是我想要的。 -
贡献)由用户反馈到用户的配置文件中,然后该配置文件将概述该用户所做的更改/贡献:
我相信这可以使用<$ c $结合papertrail注册已进行更改的用户这一事实,可以使用c> -with-changes 选项(该选项记录了除已更改资源的完整转储以外所做的更改)。我的理解是否正确?
在教程 http://samurails.com/gems/papertrail/ 我阅读了关于使用papertrail和gemdiffy
来确定准确更改的内容,但我不明白为什么本教程使用diffy
当papertrail本身已经提供了diffing功能吗? 要使主持人首先接受某些用户的更改,在实际执行变更之前(即在变更实际应用之前):
是否可以使用小菜谱来实现此功能?
1。版主可撤销特定的更改
您可以使用以下模块实现此功能:
模块可恢复的
SKIP_FIELDS = ['updated_at']
def revert_to(版本)
提升'这个模型的版本',除非自己== version.item
changes = version.changeset.select {| k,v |不是SKIP_FIELDS.include?(k)} .map {| k,v | [k.to_sym,v [0]]} .to_h
self.update_attributes(更改)
结束
结束
它向模型中添加 revert_to
方法,允许版主撤消特定编辑中的更改。请注意 SKIP_FIELDS
数组,它排除了几个不应该被还原的系统字段。
我们可以很容易地测试这个模块。让我们创建一个表:
create_table:articles do | t |
t.string:title
t.string:body
t.timestamps null:false
end
及相关模型:
class Article< ActiveRecord :: Base
包含可转换
has_paper_trail
结束
下面的测试案例显示,只有特定于版本的编辑被还原:
class ArticleTest< ActiveSupport :: TestCase
testrollback specific editdo
article = Article.create(title:'My Article 1',body:'first version')
article.update_attributes(title: 'My Article 1',body:'second version')
article.update_attributes(标题:'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没有改变
assert_equal'第一版',article.body#body does change
end
end
2.用户改变h2>
要打开更改跟踪,请将以下方法添加到您的应用程序控制器中:
类应用程序tionController
def user_for_paper_trail
user = current_user
如果user.blank返回'public'
user.username
end
end
特定用户现在可以很容易地跟踪到:
versions = PaperTrail :: Version.where(whodunnit:'dimakura')
version = versions.first
version.item#=> #<文章ID:1,标题:...,body:...>
version.event#=> 创建
version.changeset
Diffy
至于你关于 diffy
的问题。如果您唯一需要的是获得两个相邻版本之间的差异,则实际上并不需要它。但是如果你需要比较几个版本之间的变化,那么你需要 diffy
或任何类似的库。
< h2>版主接受更改
我认为在单个字段中执行并不容易。您可能需要为已接受和原始数据提供两列,甚至可能是两种不同的模型。
我想我已经涵盖了所有的问题,这对于你。
I was wondering whether the following use case can be achieved using the papertrail gem? A Wikipedia-type of application with wiki pages that logged in users can change/edit and where:
Moderators can undo specific changes:
I understand papertrail allows to roll back to a previous version but what I’m asking here is somewhat different. That is, the ability to undo a specific edit/change. Suppose there have been three edits/versions to a record/wiki-page. Then if you want to undo edit 2, then changes from edit 1 and 3 should still remain. But if you would roll back to the version before edit 2, then also edit 3 would be undone, which is not what I want here.Changes made (contributions) by a user feed back into the user’s profile, which would then have an overview of the changes/contributions made by that user:
I believe this is possible using the--with-changes
option (which registers the change that was made in addition to the full dump of the changed resource) in combination with the fact that papertrail registers the user who has made a change. Am I correct in my understanding?
In the tutorial http://samurails.com/gems/papertrail/ I read about using papertrail in combination with the gemdiffy
to establish what was changed exactly, but what I don’t understand is why the tutorial usesdiffy
when papertrail itself already offers a "diffing" functionality?To have moderators first accept a change by some users, before that change is actually implemented (i.e., before the change is actually applied):
Can papertrail also help to achieve this functionality?
1. Moderators can undo specific changes
You can achieve this functionality using the following module:
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
It adds revert_to
method to the model which allows moderator to undo only changes in specific edit. Pay attention to SKIP_FIELDS
array, which excludes several system fields, which should not be reverted.
We can easily test this module. Let's create a table:
create_table :articles do |t|
t.string :title
t.string :body
t.timestamps null: false
end
and associated model:
class Article < ActiveRecord::Base
include Revertible
has_paper_trail
end
The following test case shows, that only version specific edits were reverted:
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 made (contributions) by a user
To turn on changes tracking add the following method to your application controller:
class ApplicationController
def user_for_paper_trail
user = current_user
return 'public' if user.blank?
user.username
end
end
Changes made by a specific user can be easily tracked now:
versions = PaperTrail::Version.where(whodunnit: 'dimakura')
version = versions.first
version.item # => #<Article id: 1, title: "...", body: "...">
version.event # => "create"
version.changeset
Diffy
As to your question about diffy
. You don't actually need it if the only thing you need is to get difference between two adjacent versions. But if you need to compare changes between version separated by several edits, then you do need diffy
or any similar library.
Moderator accepts changes
I don't think it's easy to implement in a single field. You probably need to have two columns for "accepted" and "raw" data, maybe even two different models.
I think I covered all you questions and it was helpful for you.
这篇关于Paper_trail宝石的能力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!