在Rails中审核记录

时间:2014-10-12 17:34:18

标签: ruby-on-rails ruby ruby-on-rails-3 acts-as-audited

我有一个Rails 3.2.14应用程序,其中Call模型具有许多不同的关联。我希望能够跟踪Call模型的更改,并以某种方式显示Call显示视图中的更改列表。

我一直在阅读有关audited宝石的信息,看起来它可能会成功。但在我深入研究之前,我想知道以下内容。

如何在展会视图中调用审核?我假设我可以做一些像传递一个块的事情:

<% @call.audits.each do |a| %> <%= a.action %> <%= a.audited_changes %> <% end %>

当我需要查看针对特定呼叫所做的更改时,这样的内容会在show视图中起作用吗?

audited gem如何处理关联,尤其是has_many_through

我希望尽快实现此功能,但不想在我的应用中引入任何问题。我假设在开发环境中安装可能是最好的路线吗?

如果有人有这个宝石的经验或者可以帮助提供答案,我真的很感激。

更新 所以我尝试安装审计的gem,我能够显示审计操作和audited_changes。但audited_changes的格式是序列化哈希。如何对其进行反序列化并使字段友好?此外,使用has_many_through关系/连接表时,gem似乎不会记录更改。所以我现在拥有的是一个半工作的审计宝石,其数据不是用户友好的。有什么方法可以解决这个问题并使其对用户有意义吗?

call.rb摘录

 has_many :call_units
  has_many :units, through: :call_units
  belongs_to :nature
  belongs_to :service_level
  belongs_to :patient_sex
  belongs_to :insurance
  belongs_to :region
  has_many :call_special_equipments
  has_many :special_equipments, :through => :call_special_equipments
  belongs_to :transferred_from, :foreign_key => :transfer_from_id, :class_name => 'Facility'
  belongs_to :transferred_to, :foreign_key => :transfer_to_id, :class_name => 'Facility'
  belongs_to :parent_call, class_name: "Call"
  has_many :notes
  belongs_to :cancel_reason

2 个答案:

答案 0 :(得分:1)

我已使用paper_trail gem来审核记录更改以下是另一个SO答案,显示如何显示记录增量:Display all versions of individual records in Papertrail

对经审计的宝石没有贬低暗示。

答案 1 :(得分:1)

我们使用这样的东西:

# app/views/audit_trail/index.html.haml
= will_paginate @audits
.table-responsive
  %table.table.table-striped
    %thead
      %tr
        %th Record
        %th Associated With
        %th By
        %th Action
        %th Changes
        %th Version
        %th Remote Address
        %th Timestamp

    %tbody
      = render @audits
= will_paginate @audits

# app/views/audited/adapaters/active_record/audits/_audit.html.haml
%tr
  %td.top
    = "#{audit.auditable_type}:"
    %br
    = "'#{audit.auditable.try(:audited_friendly)}'"
  %td.top= audit.associated.try(:audited_friendly)
  %td.top= audit.user.try(:name)
  %td.top= audit.action
  %td.top= audit.audited_changes
  %td.top= audit.version
  %td.top= audit.remote_address
  %td.top= audit.created_at

请注意,audited_friendly是我们添加到审核模型的方法。例如,User模型返回name属性,但Pattern模型返回label属性。