使用Rails审核gem的活动历史记录

时间:2012-06-12 10:44:25

标签: ruby-on-rails android-activity history audit

我有一些应用程序与一些型号,如产品和用户。我正在使用“审计”的宝石来跟踪产品的变化,这很简单,很好用。

但我想制作一个特殊页面,我想把每日活动历史记录。我需要像Audits.all.order(“created_at”)这样的东西,但是没有这样的模型。

问题:如何为所有型号获得今天的所有审核?

3 个答案:

答案 0 :(得分:6)

我认为您应该根据gem structure

查询Audited::Adapters::ActiveRecord::Audit.where("created_at >= ?", Date.today)

答案 1 :(得分:3)

能够通过以下方式访问今天的审核:

@audits = Audit.today

audit.rb中创建一个app/models/文件,如:

Audit = Audited.audit_class

class Audit
  scope :today, -> do
    where("created_at >= ?", Time.zone.today.midnight).reorder(:created_at)
  end
end

Audited also provides a few named scopes of its own可能有用:

scope :descending,    ->{ reorder("version DESC") }
scope :creates,       ->{ where({:action => 'create'}) }
scope :updates,       ->{ where({:action => 'update'}) }
scope :destroys,      ->{ where({:action => 'destroy'}) }

scope :up_until,      ->(date_or_time){ where("created_at <= ?", date_or_time) }
scope :from_version,  ->(version){ where(['version >= ?', version]) }
scope :to_version,    ->(version){ where(['version <= ?', version]) }
scope :auditable_finder, ->(auditable_id, auditable_type){ where(auditable_id: auditable_id, auditable_type: auditable_type) }

答案 2 :(得分:1)

我的解决方案只是扩展审计对象,例如

cat lib/audit_extensions.rb
# The audit class is part of audited plugin
# we reopen here to add search functionality
require 'audited'

module AuditExtentions
  def self.included(base)
    base.send :include, InstanceMethods
    base.class_eval do
      belongs_to :search_users, :class_name => 'User', :foreign_key => :user_id
      scoped_search :on => :username, :complete_value => true
      scoped_search :on => :audited_changes, :rename => 'changes'
      scoped_search :on => :created_at, :complete_value => true, :rename => :time, :default_order => :desc
      scoped_search :on => :action, :complete_value => { :create => 'create', :update => 'update', :delete => 'destroy' }
      before_save :ensure_username

    end
  end

  module InstanceMethods
    private

    def ensure_username
      self.username ||= User.current.to_s rescue ""
    end

  end
end

Audit = Audited.audit_class
Audit.send(:include, AuditExtentions)