1

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

我一直在阅读有关经过审核的gem,它看起来可能会奏效。但在我深入研究之前,我想知道以下内容。

如何从显示视图中调用审核?我假设我可以做一些事情,比如传递一个块:

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

当我需要查看为特定呼叫所做的更改时,类似的东西会在显示视图中起作用吗?

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

我希望尽快实现此功能,但不想在我的应用程序中引入任何问题。我认为在开发环境中安装可能是最好的途径?

如果有人对此宝石有经验或可以帮助提供答案,我将不胜感激。

更新 所以我尝试安装审核的 gem,我能够显示审核操作和 audited_changes。但是audited_changes的格式是一个序列化的hash。我怎样才能反序列化它并使字段友好?has_many_through此外,在使用关系/连接表时,gem 似乎没有记录更改。所以我现在拥有的是一个半工作的审计 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
4

2 回答 2

1

我已经使用paper_trail gem 来审核记录更改这是另一个 SO 答案,它显示了如何显示记录增量:在 Papertrail 中显示单个记录的所有版本

没有贬低经过审核的宝石的意思。

于 2014-10-13T20:27:38.687 回答
1

我们使用这样的东西:

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

    %tbody
      = render @audits

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

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

更新

User以下是具有审计 ( ) 的对象和与记录关联的对象 ( ) 的审计记录的相关控制器方法Client

# app/controllers/audits_controller.rb
  def index
    load_user if params[:user_id]
    load_client if params[:client_id]

    @audits = find_audits
  end

  def load_user
    @auditable = User.find(params[:user_id])
  end

  def load_client
    @associated = Client.find(params[:client_id])
  end

  def find_audits
    if @auditable
      scope = @auditable.audits
    elsif @associated
      scope = Audited::Audit.where(associated_type: @associated.class.name, associated_id: @associated.id)
      scope = scope.where(auditable_type: params[:auditable_type]) if params[:auditable_type]
    end
    scope.reorder("id DESC, version DESC")
  end
于 2015-10-08T17:38:56.920 回答