自己写个gem叫change_log 水平有限欢迎斧正
这几天把公司项目上的一些东西整理了整理,把能单独拿出来的东西都拿出来。写成gem,这样公司其他的项目就可以方便使用了。
change_log,也叫maintenance log. 意思就是保存所有表中数据的修改。包括谁在什么时间创建/修改/删除了哪些东西。
应用环境:
例如,公司有个会计系统。如果哪天你看见有一个账目变的非常奇怪,好像跟你之前看到的不大一样。
可以调出所有的change_log。一看,原来是小谁家的小谁把某个数改了。找到了元凶。
可能应用面不是很广,但是放在这里还是请大家斧正斧正。
rubygems 里面有类似的gem. 在这里我不想讨论谁抄了谁的理念。
只想把自己的东西越弄越好,说不定能帮助其他人。
谢谢
下面是用法:
1 安装:
# console window gem install change_log # environment.rb config.gem 'change_log'
# Gemfile gem 'change_log' # console window bundle install
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
#console window db:migrate
enable_change_log :ignore=>[:updated_at,:user_password]
def current_user return session[:user] # replace this with your own code end
# this is a model filedef making_some_changes user = User.find(:first) user.website = 'www.iteye.com' user.saveend
# this is a model filedef making_some_changes user = User.find(:first) user.website = 'www.iteye.com' user.whodidit = 'javaeye' # 这样就可以了 user.saveend
# List all changesChangeLogs.find(:all)# List all changes for table 'accounts'ChangeLogs.find(:all,:conditions=>['table_name = ?', 'accounts'])
# config/environment.rbChangeLogs.set_table_name('maintenance_logs')