• Stars
    star
    277
  • Rank 143,935 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 13 years ago
  • Updated about 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Log transitions on a state machine to support business process analytics. Superseded by

StateMachine audit trail <img src=“https://travis-ci.org/wvanbergen/state_machine-audit_trail.png” />

Superceded

This gem has been superceded by state_machines-audit_trail. For more information, see the wiki on converting to the new gem..

Deprecated

This gem is deprecated and no longer maintained due to unmaintained, outdated, and conflicting depdencies.

This plugin for the state machine gem (see github.com/pluginaweek/state_machine) adds support for keeping an audit trail for any state machine. Having an audit trail gives you a complete history of the state changes in your model. This history allows you to investigate incidents or perform analytics, like: “How long does it take on average to go from state a to state b?”, or “What percentage of cases goes from state a to b via state c?”

ORM support

Note: while the state_machine gem integrates with multiple ORMs, this plugin is currently limited to the following ORM backends:

  • ActiveRecord

  • Mongoid

It should be easy to add new backends by looking at the implementation of the current backends. Pull requests are welcome!

Usage

First, make the gem available by adding it to your Gemfile, and run bundle install:

gem 'state_machine-audit_trail'

Create a model/table that holds the audit trail. The table needs to have a foreign key to the original object, an “event” field, a “from” state field, a “to” state field, and a “created_at” timestamp that stores the timestamp of the transition. This gem comes with a Rails 3 generator to create a model and a migration like that.

rails generate state_machine:audit_trail <model> <state_attribute>

(For Rails 2, use rails generate state_machine_audit_trail <model> <state_attribute>  [note the underscore instead of the colon])

For a model called “Model”, and a state attribute “state”, this will generate the ModelStateTransition model and an accompanying migration.

Next, tell your state machine you want to store an audit trail:

class Model < ActiveRecord::Base
  state_machine :state, :initial => :start do
    store_audit_trail
    ...

If your audit trail model does not use the default naming scheme, provide it using the :to option:

class Model < ActiveRecord::Base
  state_machine :state, :initial => :start do
    store_audit_trail :to => 'ModelAuditTrail'
    ...

That’s it! The plugin will register an after_transition callback that is used to log all transitions. It will also log the initial state if there is one.

If you would like to store additional messages in the audit trail, you can do so with the following:

store_audit_trail :context_to_log => :state_message # Will grab the results of the state_message method on the model and store it in a field called state_message on the audit trail model

or

store_audit_trail :context_to_log => [:field1, :field2] # Will grab the results of the field1 and field2 methods on the model and store them in fields called field1 and field2 on the audit trail model

Store virtual attributes

Sometimes it can be useful to store dynamically computed information.

In these situations it’s just a matter of defining a new column on table DeploymentStateTransitions and configure context_to_log.

i.e.

class Model < ActiveRecord::Base
  state_machine :state, :initial => :start do
    store_audit_trail :context_to_log => :version
    ...

  def version
    # Dynamically computed field e.g., based on other models
    ...

class AddVersionToDeploymentStateTransitions < ActiveRecord::Migration
  def change
    add_column :deployment_state_transitions, :version, :string
    ...

About

This plugin is written by Jesse Storimer and Willem van Bergen for Shopify. Mongoid support was contributed by Siddharth (github.com/svs). It is released under the MIT license (see LICENSE).

More Repositories

1

request-log-analyzer

Create reports based on your log files. Supports Rails, Apache, MySQL, Delayed::Job, and other formats.
Ruby
2,245
star
2

chunky_png

Read/write access to PNG images in pure Ruby.
Ruby
1,043
star
3

kafka

Load-balancing, resuming Kafka consumer for go, backed by Zookeeper.
Go
372
star
4

scoped_search

Easily search you ActiveRecord models with a simple query language that converts to SQL.
Ruby
265
star
5

oily_png

Native mixin to speed up ChunkyPNG.
C
174
star
6

adyen

Package to simplify including the Adyen payments services into a Ruby application.
Ruby
132
star
7

kazoo-go

Go library to access Kafka metadata in Zookeeper
Go
100
star
8

vertica

Ruby adapter for Vertica databases
Ruby
79
star
9

active_olap

OLAP extensions for ActiveRecord
Ruby
55
star
10

node-vertica

Pure javascript Vertica database client. Except it is written in CoffeeScript.
CoffeeScript
44
star
11

http_status_exceptions

A Rails plugin to use exceptions for generating HTTP status responses.
Ruby
28
star
12

activerecord-databasevalidations

Add validations to your ActiveRecord models based on your database constraints.
Ruby
26
star
13

clieop

A pure Ruby implementation for writing CLIEOP files to submit payments or direct charges to Dutch banks.
Ruby
14
star
14

sql_tree

Outdated, replaced by a much more complete SQL toolkit: https://github.com/wvanbergen/sql_toolkit
Ruby
14
star
15

kafka-consumer

High-level Ruby consumer for Kafka.
Ruby
12
star
16

ottawa-trash

This is for hoping that Ottawa's Garbage Collection works out better than Ruby's.
Ruby
6
star
17

schema_registry

Ruby client for Confluent Inc.'s schema-registry
Ruby
6
star
18

uuml

Instant Germanification of your web app!
Ruby
6
star
19

SysVIPC

Fork of the SysVIPC gem to fix packaging issues
C
6
star
20

dropbox-sync

Tool to synchronize folders between machiens using Dropbox and symbolic links.
Ruby
6
star
21

selekt

Toolkit to parse an manipulate SQL queries for testing and monitoring purposes.
Ruby
5
star
22

chunky_bits

Collection of image processing algorithms built on top of ChunkyPNG
Ruby
5
star
23

kazoo

Ruby library to access Kafka metadata in Zookeeper
Ruby
4
star
24

blogposts

Staging area for my blogposts
4
star
25

newrelic-vertica

Enable SQL monitoring in NewRelic RPM for the Vertica driver
Ruby
3
star
26

love

Ruby library to access the Tender REST API
Ruby
3
star
27

HackReduce2

3
star
28

tros

Cleaned up AVRO implementation
Ruby
2
star
29

guillaume-consulting

Guillaume Consulting website
Ruby
2
star
30

dreamcatcher

Simple gem to catch exceptions in Ruby code and send notifications.
Ruby
2
star
31

scoped_search-h2

H2 database support for scoped_search
Ruby
1
star
32

scoped_search-oracle_enhanced

Oracle enhanced support for scoped_search
Ruby
1
star