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.
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'
3. Create a table to keep all changesGenerate 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
1. Add current_user Method in application_controller.rb Use Change Log Gem
This method will tell change_log who is the current user
2. ActiveRecord Models
def current_user return session[:user] # replace this with your own code end
Enable change_log for Active Record Model,
just put following line in very beginning of model file.
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:
# this is a model file def making_some_changes user = User.first user.email = 'firstname.lastname@example.org' user.save end
An attribute called ‘whodidit’ is automatically available.
So if you want to keep the changes in this scenario, do following:
3. About the ChangeLogs Model
# this is a model file def making_some_changes user = User.first user.email = 'email@example.com' user.whodidit = 'Peter' user.save end
ChangeLogs model is core ActiveRecord model used by change_log gem.
You can use it directly in your model, controller even in helper.
# List all changes ChangeLogs.all
# List all changes made by user 'peterz' ChangeLogs.where('user = ?', 'peterz')
4. Turn ChangeLogs off in testing environment
# List all changes for table 'accounts' ChangeLogs.where('table_name = ?', 'accounts')
You can globally turn it off for your testing.
5. Database and table name
# config/environment.rb ChangeLog.enabled = false if Rails.env.test?
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
Please email me if you have any enquiry.
Peter Zhang at NCS Software Ltd New Zealand.
Copyright © 2013 Peter Zhang and NCS Software LTD, released under the MIT license