Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Rails Gem change log
Ruby
Tag: v0.0.5

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
Gemfile
MIT-LICENSE
README.textile
Rakefile
change_log.gemspec

README.textile

Change Log

A gem to keep all maintenances in database

Install Change Log Gem

1. by command:

# gem install change_log

Then in your environment.rb:
config.gem 'change_log'

2. by bundler:

# Gemfile in your application
gem 'change_log'

Then:

bundle install

  • Note: Currently if you use bundler to load gems and phusion-passenger as your web server,
    you may get “enable_change_log” method missing error.
    I am still looking for reason for that, helps will be pleased!

Create a table to keep all maintenance logs

Generate a migration file

    class AddChangeLog < ActiveRecord::Migration
      def self.up
        create_table :change_logs do |t|
         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

  • Note: It is ok if you want to use other table name 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
    Maintenance.set_table_name('hr_maintenances')
    
  • Note: Maintenance model is core ActiveRecord model used by change_log.

Use Change Log

1. Include change_log in environment.rb
config.gem 'change_log'
2. Add current_user method in application_controller.rb

# used by change log 
def current_user 
  return session[:user] # replace this with your own code to tell change_log who is the current user
end
3. Models

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

Testing

TODO

Author
--

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

Copyright © 2011 Peter Zhang, released under the MIT license

Something went wrong with that request. Please try again.