Module: Mongoid::SimpleAudit::Model::ClassMethods
- Defined in:
- lib/mongoid/simple_audit/simple_audit.rb
Instance Method Summary collapse
-
#audit(record, action = :update, user = nil) ⇒ Object
:nodoc:.
-
#simple_audit(options = {}, &block) ⇒ Object
Configuration options.
Instance Method Details
#audit(record, action = :update, user = nil) ⇒ Object
:nodoc:
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/mongoid/simple_audit/simple_audit.rb', line 80 def audit(record, action = :update, user = nil) #:nodoc: record_audit = SimpleAudit::Audit.find_or_create_by_auditable( record ) # check current user for typical Authentication systems user = nil if defined?(User) and User.respond_to?(:current) user = User.current elsif defined?(Cms::User) and Cms::User.respond_to?(:current) user = Cms::User.current end current_change_log = self.audit_changes.call(record) # do only log if anything changed record_changed = true if audit_changes_only and last_change_log = record.audit.modifications.last record_changed = last_change_log.symbolized_change_log != current_change_log end if record_changed if user.present? record_audit.modifications.create( :user => {id: user.id, type: user.class.name}, :username => user.try(self.username_method), :action => action.to_s, :change_log => current_change_log) else record_audit.modifications.create( :action => action.to_s, :change_log => current_change_log) end end end |
#simple_audit(options = {}, &block) ⇒ Object
Configuration options
-
username_method => symbol
- Call this method on the current user to get the name
With no block, all the attributes and belongs_to
associations (id and to_s) of the audited model will be logged.
class Booking
# this is equivalent to passing no block
simple_audit do |audited_record|
audited_record.attributes
end
end
If a block is given, the data returned by the block will be saved in the audit’s change log.
class Booking
has_many :housing_units
simple_audit do |audited_record|
{
:some_relevant_attribute => audited_record.some_relevant_attribute,
:human_readable_serialization_of_aggregated_models => audited_record.housing_units.collect(&:to_s),
...
}
end
end
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mongoid/simple_audit/simple_audit.rb', line 50 def simple_audit( = {}, &block) class_eval do class_attribute :username_method class_attribute :audit_changes class_attribute :audit_changes_only self.username_method = ([:username_method] || :name).to_sym self.audit_changes_only = [:audit_changes_only] === true attributes_and_associations = proc do |record| changes = record.attributes as_types = [:belongs_to] as_types << :embeds_one if record.singleton_class.included_modules.include? Mongoid::Document as_types.each do |as_type| record.class.reflect_on_all_associations( as_type ).each do |assoc| changes[assoc.name] = record.send(assoc.name).to_s end end changes end audit_changes_proc = block_given? ? block.to_proc : attributes_and_associations self.audit_changes = audit_changes_proc after_create {|record| record.class.audit(record, :create, nil)} after_update {|record| record.class.audit(record, :update, nil)} end end |