简体   繁体   English

使用paper_trail gem创建审核跟踪

[英]Create an audit trail using paper_trail gem

I'm pretty new to rails, and brand new to using the paper_trail gem. 我是Rails的新手,并且是使用paper_trail gem的新手。

I would like to show a list of each change made to the location or tag number of my assets model. 我想显示对资产模型的位置或标签号所做的每项更改的列表。 I have installed paper_trail and it seems to be working correctly. 我已经安装了paper_trail,它似乎可以正常工作。 In my view I have this: 在我看来,我有:

<% @asset.versions.order('created_at DESC').each do |version| %>
    On <%= version.created_at %> <%= version.whodunnit %> updated this asset.<br>
    <ul>
        <li>Asset relocated from "<%= version.last.locaton %>" to "<%= version.location %> "</li>
        <li>User changed from "<%= version.last.tag_number %>" to "<%= version.tag_number %> "</li>
    </ul>
<% end %>

This gives me the correct information for when the change was made and who made it, but I cannot figure out to display the lines that say (for instance): Asset relocated from "223" to "258 or Tag Number changed from "1173" to "1175". 这为我提供了有关何时进行更改以及进行更改的正确信息,但是我无法弄清楚显示以下行的内容(例如):资产从“ 223”迁移到“ 258”或标记号从“ 1173”更改了至“ 1175”。

Ideally I would only show information for things that have changed. 理想情况下,我只会显示发生变化的信息。 So if the location changed but the tag number did not, I would show only the location line. 因此,如果位置发生了变化,但标签号没有变化,我将仅显示位置线。

I don't really even know where to start. 我什至不知道从哪里开始。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

From The PaperTrail Documentation: PaperTrail has an optional table column that can be used when creating versions called object_changes . 来自PaperTrail文档:PaperTrail有一个可选的表格列,可在创建称为object_changes版本时使用。 This will store ONLY the changes made between versions. 这将存储版本之间所做的更改。 Which will allow you to just display the changes between each version. 这样您就可以只显示每个版本之间的更改。 It works like this. 它是这样的。

>> widget = Widget.create :name => 'Bob'
>> widget.versions.last.changeset                # {'name' => [nil, 'Bob']}
>> widget.update_attributes :name => 'Robert'
>> widget.versions.last.changeset                # {'name' => ['Bob', 'Robert']}
>> widget.destroy
>> widget.versions.last.changeset                # {}

If you recently added paper_trail to your model then you can run a migration to add this column. 如果您最近将paper_trail添加到模型中,则可以运行迁移以添加此列。 This will track future changes. 这将跟踪未来的变化。

class AddObjectChangesColumnToVersions < ActiveRecord::Migration
  def self.up
    add_column :versions, :object_changes, :text
  end

  def self.down
    remove_column :versions, :object_changes
  end
end

I recently had a need to get the changes, however, we have years of version data for 20+ models and adding that migration would only help in the future and not give me all of the data changes in the past. 我最近需要进行更改,但是,我们拥有20多个模型的多年版本数据,并且补充说,迁移只会在将来有所帮助,而不能提供过去的所有数据更改。

I wound up creating a patch for PaperTrail that added methods like diff_previous , diff_next , diff_live which would give me the difference between versions. 我为diff_previous创建了一个补丁,添加了diff_previousdiff_nextdiff_live类的方法,这会给我带来版本之间的差异。 It was based off the idea of this patch of paper_trail( https://github.com/jeremyw/paper_trail/blob/master/lib/paper_trail/has_paper_trail.rb#L151-L156 ) but I implemented it quite differently. 它基于paper_trail( https://github.com/jeremyw/paper_trail/blob/master/lib/paper_trail/has_paper_trail.rb#L151-L156 )的补丁思想,但我实现起来却完全不同。 If you want more details I will see if I can open source that patch. 如果您需要更多详细信息,我将查看是否可以开源该补丁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM