Code Monkey home page Code Monkey logo

expressive_record's Introduction

Expressive Record

Add meaningful names for belongs_to associations to Rails 2.1 model object dirty changes (dev.rubyonrails.org/changeset/9127) to help with rolling your own history/audit logger.

Install

script/plugin install git://github.com/lukewendling/expressive_record.git

or

sudo gem install lukewendling-expressive_record --source http://gems.github.com

create a migration for the audit table (see USAGE)

Usage

ActiveRecord 2.1 introduces dirty changes; the ability to query a model object to track attribute changes before the record is saved using #changes, #changed, etc.

However, belongs_to associations are cached simply as foreign key changes, so Ticket#changes yields something like {“status_id” => [“111”, “222”]}, which isn’t especially helpful when rolling your own audit logger.

Expressive Record turns association (foreign key) changes into meaningful terms when the changes are saved to your audit table.

class Ticket < ActiveRecord::Base
  include ExpressiveRecord

  has_many :ticket_changes

  # pass in the name of your audit table (optional)
  express_changes :ticket_changes

end

express_changes adds a before_update callback to your model that inserts changes into an audit table with the following schema:

class CreateTicketChanges < ActiveRecord::Migration
  def self.up
    create_table "ticket_changes", :force => true do |t|
      t.integer  "ticket_id",      :null => false
      t.string   "attribute_type", :null => false
      t.text     "old_value"
      t.text     "new_value"
      t.timestamps
    end
  end
end

If using a before_update callback in subclasses, be sure to call ‘super’ as needed:

class A
  # a before_update callback is auto-magically added
  express_changes
end

class A < B
  def before_update
    self.status = 'New' # do this before auditing
    super # now insert changes into audit log
  end
end

Limitations

  • the current implementation assumes that all belongs_to foreign keys use Rails conventional names, like user_id or status_id. if you are connecting to a legacy db with fk’s like customerID or whatever, this won’t work.

TODO

  • add migration generator to create conventionally-named log table (i.e. ticket_changes) to schema

expressive_record's People

Contributors

lukewendling avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  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.