module Challah::Audit

The audit methods are included into ActiveRecord::Base automatically and add basic audit trail functionality for your models. Certain columns will be updated with the current user's id if provided at save time.

For new records, the following fields will be updated if current_user is provided:

For updating existing records, the following attributes will be updated:

To save the user id that changed a record, just set the current_user attribute of the model in your controller. For example:

class WidgetsController < ApplicationController
  ...

  def update
    @widget = Widget.find(params[:id])
    @widget.current_user = current_user
    @widget.update_attributes(params[:widget])
    ...
  end

Public Instance Methods

current_user=(value) click to toggle source
# File lib/challah/audit.rb, line 43
def current_user=(value)
  @current_user_id = (Object === value ? value[:id] : value)
end
current_user_id() click to toggle source
# File lib/challah/audit.rb, line 51
def current_user_id
  unless @current_user_id
    @current_user_id = 0
  end

  @current_user_id
end
current_user_id=(value) click to toggle source
# File lib/challah/audit.rb, line 47
def current_user_id=(value)
  @current_user_id = value
end
initialize_dup(other) click to toggle source

@private

Calls superclass method
# File lib/challah/audit.rb, line 38
def initialize_dup(other)
  super
  clear_audit_attributes
end

Private Instance Methods

all_audit_attributes() click to toggle source

@private

# File lib/challah/audit.rb, line 89
def all_audit_attributes
  audit_attributes_for_update + audit_attributes_for_create
end
audit_attributes_for_create() click to toggle source

@private

# File lib/challah/audit.rb, line 84
def audit_attributes_for_create
  [ :created_by, :created_user_id ]
end
audit_attributes_for_update() click to toggle source

@private

# File lib/challah/audit.rb, line 79
def audit_attributes_for_update
  [ :updated_by, :modifed_by, :updated_user_id ]
end
before_save_audit() click to toggle source
# File lib/challah/audit.rb, line 61
def before_save_audit
  if new_record?
    all_audit_attributes.map(&:to_s).each do |column|
      if respond_to?(column) && respond_to?("#{ column }=")
        write_attribute(column, current_user_id)
      end
    end
  else
    audit_attributes_for_update.map(&:to_s).each do |column|
      if respond_to?(column) && respond_to?("#{ column }=")
        next if attribute_changed?(column) # don't update the column if we already manually did
        write_attribute(column, current_user_id)
      end
    end
  end
end
clear_audit_attributes() click to toggle source

Clear attributes and changed_attributes

# File lib/challah/audit.rb, line 94
def clear_audit_attributes
  all_audit_attributes.each do |attribute_name|
    if respond_to?(attribute_name) && respond_to?("#{ attribute_name }=")
      write_attribute(attribute_name, nil)
    end
  end

  @changed_attributes = changed_attributes.reduce(ActiveSupport::HashWithIndifferentAccess.new) do |result, (key, value)|
    unless all_audit_attributes.include?(key.to_sym)
      result[key] = value
    end

    result
  end
end