Module: SimpleAudit::Model::ClassMethods
- Defined in:
- lib/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:
71 72 73 74 75 76 77 78 |
# File 'lib/simple_audit/simple_audit.rb', line 71 def audit(record, action = :update, user = nil) #:nodoc: user ||= User.current if User.respond_to?(:current) record.audits.create(:user => user, :username => user.try(self.username_method), :action => action.to_s, :change_log => self.audit_changes.call(record) ) 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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/simple_audit/simple_audit.rb', line 46 def simple_audit( = {}, &block) class_eval do write_inheritable_attribute :username_method, ([:username_method] || :name).to_sym class_inheritable_reader :username_method attributes_and_associations = proc do |record| changes = record.attributes record.class.reflect_on_all_associations(:belongs_to).each do |assoc| changes[assoc.name] = record.send(assoc.name).to_s end changes end audit_changes_proc = block_given? ? block.to_proc : attributes_and_associations write_inheritable_attribute :audit_changes, audit_changes_proc class_inheritable_reader :audit_changes has_many :audits, :as => :auditable, :class_name => '::SimpleAudit::Audit' after_create {|record| record.class.audit(record, :create)} after_update {|record| record.class.audit(record, :update)} end end |