Code Monkey home page Code Monkey logo

mongoid-audit_log's Introduction

Mongoid::AuditLog

Frustrated with the other options for this, I wrote this gem to handle most basic audit logging for Mongoid. It is intended to be stupidly simple, and offers no fancy functionality.

Installation

Add this line to your application's Gemfile:

gem 'mongoid-audit_log'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mongoid-audit_log

Usage

Recording Activity

Include the Mongoid::AuditLog module into your model.

class Model
  include Mongoid::Document
  include Mongoid::AuditLog
  field :name
end

This will not enable logging by itself, only changes made within the block passed to the record method will be saved.

Mongoid::AuditLog.record do
  Model.create!
end

If you want to log the user who made the change, pass that user to the record method:

Mongoid::AuditLog.record(current_user) do
  Model.create!
end

A basic implementation in a Rails app might look something like:

class ApplicationController < ActionController::Base
  around_filter :audit_log

  def current_user
    @current_user ||= User.find(session[:user_id])
  end

  private

  def audit_log
    Mongoid::AuditLog.record(current_user) do
      yield
    end
  end
end

Viewing Activity

When an audited model is changed, it will create a record of the Mongoid::AuditLog::Entry class. Each class responds to some query methods:

Mongoid::AuditLog.record do
  model = Model.create!
  module.update_attributes(:name => 'model')
end

model.audit_log_entries.length == 2 # => true

model.audit_log_entries.first.create? # => true
model.audit_log_entries.first.update? # => false
model.audit_log_entries.first.destroy? # => false

model.audit_log_entries.second.create? # => false
model.audit_log_entries.second.update? # => true
model.audit_log_entries.second.destroy? # => false

# And on update you have the tracked changes
model.audit_log_entries.second.tracked_changes.should == { 'name' => [nil, 'model'] }

There are also some built-in scopes (examples from the tests):

  create = Entry.create!(:action => :create, :created_at => 10.minutes.ago)
  update = Entry.create!(:action => :update, :created_at => 5.minutes.ago)
  destroy = Entry.create!(:action => :destroy, :created_at => 1.minutes.ago)

  Entry.creates.to_a.should == [create]
  Entry.updates.to_a.should == [update]
  Entry.destroys.to_a.should == [destroy]
  Entry.newest.to_a.should == [destroy, update, create]
end

Additional saved data

You can access the attributes of the model saved on the Mongoid::AuditLog::Entry. They are saved on the document in the #model_attributes, and include any changes in the #tracked_changes hash.

Examples:

Mongoid::AuditLog.record do
  model = Model.create!(:name => 'foo bar')
end

model.audit_log_entries.length == 1 # => true

model.audit_log_entries.first.create? # => true
model.audit_log_entries.first.model_attributes # => {"name"=>"foo bar"}

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

mongoid-audit_log's People

Contributors

bencrouse avatar mmartyn avatar

Watchers

Jon Bytof avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.