stricte / ar-audit-tracer

Automatic persist modifiers of AR models (same as timestamping)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ar-audit-tracer

<img src=“https://badge.fury.io/rb/ar-audit-tracer.png” alt=“Gem Version” /> <img src=“https://secure.travis-ci.org/verticonaut/ar-audit-tracer.png” /> <img src=“https://codeclimate.com/github/verticonaut/ar-audit-tracer.png” />

Summary

ar-audit-tracer patches ActiveRecord so modifiers of a record can be traked on saving (insert/update). It works exactly like ‘timestamps’ (see usage below).

The new version 2.0.0 works now for Rails 4.0 with Ruby 1.9.3 and 2.0.0. Use the version 1.0.2 for Rails ~3.0 with Ruby 1.8.7 or higher.

Note

The migration helpers changed. The column statement is now named t.authorstamps instead of t.authors. Similar the changing a table is named add_authorstamp. The columns generated are still named the same - so they are calles created_by and updated_by.

Installation

Add below to your Gemfile and run the bundle command

gem 'ar-audit-tracer'

Usage

Migration

In a models migration add:

t.authorstamps

This will add columns created_by and updated_by of type :string to your model.

In case you want to use another type, simply pass the type as argument, e.g.

t.authorstamps(:integer)

By default the columns are mandatory (:null => false). If you have existing models you want to change you have to pass the option :null => true, update the values in the new attributes columns and add another migration to change the column to :null => false if required. Note: If you pass options you have to pass the type as well - sample migration statments:

add_authorstamps(:your_table_name, :string, :null => true)

or

change_table :your_table_name do |t|
  t.authorstamps(:integer, :null => true)
end

Note

The authorstamps methods are simple conveniance methods (as regular timestamp methods are). You can simply add columns named created_by and updated_by using regular migration statements.<p/> You can name the attributes created_by_id and updated_by_id. If these columns are detected they are filled as well.

Configuration

All you need to do is to set the current author such as e.g:

Concern::Audit::Author.current="bad_man"

Each ActiveRecord save or update then will set the respetive attributes created_by and updated_by automatically, whereas the modifier is set to the same value as the creator on model creation.

In a Rails Application you would set the author as described above in a before_filter. Concern::Audit::Author stores the author in a Thread-Local variable.

Additional Notes

In case you need associations to a respective Author Model you have to set them up yourselfs.

Changelog

Version 2.0.0

  • Works for Rails4 with Ruby1.9.3 and Ruby 2.0.0

  • Works now not only for created_by and updated_by, but as well for attributes created_by_id and updated_by_id

Version 1.0.2

  • Fixed migrations so option can be passed

About

Automatic persist modifiers of AR models (same as timestamping)

License:MIT License