Rails Gem change log
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
Gemfile
Gemfile.lock
MIT-LICENSE
README.textile
Rakefile
change_log.gemspec

README.textile

Change Log

A gem to keep all changes about the data in database.
It automatically saves who made the changes at what time and what has been changed.
You can choose to skip the column which you do not want to keep change logs.
For example: ‘updated_at’, ‘created_at’ and ‘password’ etc.

  • Note:
    Use Change Log version 1.0.5 if your application is using Rails 2.×.

Install Change Log Gem

1. by command:

# gem install change_log

2. or by bundler:

# Gemfile in your application
gem 'change_log'

Then:

bundle install

3. Create a table to keep all changes

Generate a migration file similar to this:

    class AddChangeLog < ActiveRecord::Migration
      def self.up
        create_table :change_logs do |t|       # feel free to choose another table name
         t.integer :version, :null=>false      # store version of each change
         t.string :record_id,:limit=>30        # store the actual record id 
         t.string :table_name, :limit=>60      # store the table name 
         t.string :attribute_name,:limit=>60   # store the column name
         t.string :user, :limit=>20            # store the user who made the change
         t.string :action, :limit=>6           # store the change action: create, read, update, delete
         t.text :old_value                     # the value before change
         t.text :new_value                     # value after change
         t.string :field_type, :limit=>30      # the column type eg. date, text, varchar, int etc
         t.timestamps
        end
      end

      def self.down
        drop_table :change_logs
      end
    end

Then:

rake db:migrate

Use Change Log Gem

1. Add current_user Method in application_controller.rb

This method will tell change_log who is the current user


def current_user 
  return session[:user] # replace this with your own code
end

2. ActiveRecord Models

Enable change_log for Active Record Model,
just put following line in very beginning of model file.


enable_change_log :ignore=>[:updated_at]    

Put any columns you do not want to keep in the change log table in :ignore option.
eg. the password hash

Then the system should record every changes the user made to change_log table.
If you are making changes within Model file:
For Example:

# this is a model file
def making_some_changes
  user = User.first
  user.email = 'peterz@ncs.co.nz'
  user.save
end

An attribute called ‘whodidit’ is automatically available.
So if you want to keep the changes in this scenario, do following:

# this is a model file
def making_some_changes
  user = User.first
  user.email = 'peterz@ncs.co.nz'
  user.whodidit = 'Peter'
  user.save
end

3. About the ChangeLogs Model

ChangeLogs model is core ActiveRecord model used by change_log gem.

You can use it directly in your model, controller even in helper.

For example:

# List all changes
ChangeLogs.all

# List all changes made by user 'peterz'
ChangeLogs.where('user = ?', 'peterz')
# List all changes for table 'accounts'
ChangeLogs.where('table_name = ?', 'accounts')
4. Turn ChangeLogs off in testing environment

You can globally turn it off for your testing.

# config/environment.rb
ChangeLog.enabled = false if Rails.env.test?

5. Database and table name

change_log gem can save changes into separate database from the main application.
The database could be MySQL, SQLite or any other database that active record is happy to connect with.

Here is an example of database.yml when using separate database for ‘change_logs’:

change_logs:
  adapter: mysql2
  encoding: utf8
  database: change_logs
  username: username
  password: ********
  host: hostname
  port: 3306

And also you need to tell change_log gem to establish the connection.

# config/environment.rb
ChangeLogs.establish_connection(:change_logs)

Table name is also configurable. Instead of ‘change_logs’, choose your preferred table name and run the migration.
Just remember in your environment.rb file, you need to tell change_log gem
what is your table name:

# config/environment.rb
ChangeLogs.table_name :hr_maintenances

Wish List
Please email me if you have any enquiry.

Author
--

Peter Zhang at NCS Software Ltd New Zealand.
Email: peterz@ncs.co.nz

Copyright © 2013 Peter Zhang and NCS Software LTD, released under the MIT license