Skip to content

Instantly share code, notes, and snippets.

@iterion
Forked from adamstrickland/audit.rb
Last active December 11, 2015 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iterion/4676916 to your computer and use it in GitHub Desktop.
Save iterion/4676916 to your computer and use it in GitHub Desktop.
class Audit
include Mongoid::Document
include Mongoid::Timestamps
include RailsAdminImport::Import
class << self
def create_with_type_message_meta(_type, _message, _user)
self.create_without_type_message_meta({
:type => _type,
:content => _message,
:meta => create_meta_hash(_user)
})
end
def create_metric(name, value, meta={})
self.create_without_type_message_meta(
:type => "metric:#{name}",
:content => value,
:meta => meta
)
end
def register_audit_events(mod, categories, events)
_categories = if categories.kind_of?(Array)
categories
else
[categories]
end
_events = if events.kind_of?(Array)
events
else
[events]
end
_categories.each do |cat|
_events.each do |evt|
["#{mod}_#{cat}_#{evt}", "#{evt}_#{cat}_#{mod}"].each do |mthd|
define_singleton_method mthd do |msg, usr|
self.create_with_type_message_meta("#{mod}:#{cat}:#{evt}", msg, usr)
end
end
end
end
end
def create_meta_hash(_user)
{
:user => {
:name => _user.name,
:id => _user.id.to_s,
:account => {
:name => _user.account.name,
:id => _user.account.id.to_s
}
}
}
end
alias_method_chain :create, :type_message_meta
end
field :type, type: String
field :content, type: String
field :meta, type: Hash
register_audit_events :help, :chat, [:begin, :recv]
register_audit_events :trade, :parse, :attempt
end
RailsAdmin.config do |config|
config.authorize_with :cancan
config.actions do
# root actions
dashboard
# collection actions
index
new
import do
visible do
allowed = if Rails.env.development?
["Trade", "Audit"]
else
["Trade"] # only show on Trade model
end
allowed.include?(bindings[:abstract_model].model_name)
end
end
export
history_index
bulk_delete
# member actions
show
edit
delete
history_show
show_in_app
end
end
RailsAdminImport.config do |config|
config.logging = false
config.model Trade do
extra_fields do
[
:tenor_start, :tenor_end, :created_at,
:uom, :account_id, :trader_id,
:location_id, :product_id, :counterparty_id, :tags
]
end
end
config.model Audit do
extra_fields do
[:type, :content, :meta]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment