ModelsAuditor
Installation
Add ModelsAuditor to your Gemfile
gem 'models_auditor'Then execute
$ bundle installAdd the gem settings to your project
rails g models_auditor:installIt added models_auditor initializer. Make necessary settings in this file.
Generate the audit database config
rails g models_auditor:db_configIt appended separate sections to config/database.yml. Edit them.
Create the audit database
rake db:audit:createGenerate models_auditor migrations for audit_database
rails g models_auditor:migrationsMigration files will be putted into db/audit_migrate.
Apply migrations to the audit database
rake db:audit:migrate
Usages
Mount the route for read logs json api
Rails.application.routes.draw do mount ModelsAuditor::Engine => '/audit' endTo looking at routes list do
$ rake routesmodels_auditor_requests GET (/index)(/page/:page)(.:format) models_auditor/audit#index {:page=>/\d+/} root GET / models_auditor/audit#indexTo enable audit you have to add into each logged models
enable_audit ModelsAuditor::Audit::AUDIT_MODE_JSONAUDIT_MODE_JSON - Serialization by #as_json AUDIT_MODE_SERIALIZER - Serialization by using a ActiveModel Serializer, specifyied by :serializer option AUDIT_MODE_METHOD - Serialization by a method, specifyied by :method option AUDIT_MODE_CHANGES_ONLY - Serialization ActiveRecord changes of model onlyclass Post < ActiveRecord::Base enable_audit ModelsAuditor::Audit::AUDIT_MODE_JSON endclass Author < ActiveRecord::Base enable_audit ModelsAuditor::Audit::AUDIT_MODE_JSON endAdd to each association models
enable_audit ModelsAuditor::Audit::AUDIT_MODE_JSON, bridge: {author_id: Author.name, post_id: Post.name}class AuthorsPost < ActiveRecord::Base enable_audit ModelsAuditor::Audit::AUDIT_MODE_JSON, bridge: {author_id: Author.name, post_id: Post.name} end
Audit database management
Creation of the audit database
rake db:audit:create
Dropping of the audit database
rake db:audit:drop
Apply migrations to the audit database
rake db:audit:migrate
Rollback migrations to the audit database
rake db:audit:rollback
If you want to use a database prefix not from config. You may specify it as an argument in square brackets.
rake db:audit:create[audit_shmaudit]
Capistrano
Add line to the Capfile
require 'models_auditor/capistrano/rails/audit_migrations'
Migrate
The capistrano migrate task
cap deploy:audit_migrate
will be executed automatically after deploy:migrate cap task
Rollback
cap deploy:audit_rollback
This project rocks and uses MIT-LICENSE.