Skip to content

Easily track model change history and mantain audit log with mongoid

License

Notifications You must be signed in to change notification settings

rs-pro/mongoid-audit

Repository files navigation

Mongoid::Audit

Gem Version Dependency Status License

MongoidAudit 1.0 is a complete rewrite and might break backwards compatibility

Since 1.0.0, mongoid_audit is no longer a fork, but a wrapper of mongoid-history, providing out-of-the-box Userstamp, RailsAdmin integration and easier setup and configuration.

Migrating from 0.x.x

  1. delete the initializer (if you had default settings).

  2. replace

include Mongoid::Audit::Trackable
track_history track_create: true, track_destroy: true

With:

include Trackable
  1. Database storage format for user / modifier in 1.0.0 was changed, since now glebtv_mongoid_userstamp gem is used to store modifier. To migrate your data you should do something like:

    HistoryTracker.all.each{|ht| ht.rename(modifier_id: :updater_id)}

  2. This gem includes storing modifier, but it is done via glebtv_mongoid_userstamp and not directly.

  3. RailsAdmin auditing adapter is still fully supported.

Installation

Add this line to your application's Gemfile:

# must be after rails_admin, if you use it
gem 'mongoid-audit', '~> 1.0.0'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mongoid-audit

Usage

Include Trackable in your models. That's it, you are done!

include Trackable

Advanced usage

Identical to Mongoid::History. See https://github.com/aq1018/mongoid-history

include Mongoid::History::Trackable
include Mongoid::Userstamp
track_history({
  track_create: true,
  track_destroy: true,
  track_update: true,
  modifier_field: :updater,
  except: ["created_at", "updated_at", "c_at", "u_at", "clicks", "impressions", "some_other_your_field"],
})

Setting modifier class name

Add to an initializer:

Mongoid::History.modifier_class_name = 'Author'
Mongoid::Userstamp.config do |c|
  c.user_reader = :current_admin
  c.user_model = :admin
end

Rails Admin Integration

Add

config.audit_with :mongoid_audit

in config/initializers/rails_admin.rb

Credits

(c) 2013-2014 http://rocketscience.pro MIT License.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

Easily track model change history and mantain audit log with mongoid

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages