Giter Club home page Giter Club logo

track_history's Introduction

TrackHistory

Introduction

Sometimes you want to track changes in a model, but in larger tables its really inefficient to query against a polymorphic relationship in a single table like 'audits'. TrackHistory is a way to do this in a performant way, and its still easy!

Imagine you want to track how the name of users change over time:

# add a mix-in to your model (yes, that's all)
track_history

Then create a migration for a table with the following structure (generator coming soon):

id, user_id, email_before, email_after, created_at

If you want to also record creations and deletions, you should add a column like:

action ENUM ('create', 'update', 'destroy') # action can also be a varchar

You will automatically get:

user.histories
user.histories.first.class # User::History

user.histories.first.modifications # ["email"]

You can do this with any field or method.


There's more

But wait, you say! I want to use this to annotate some more information when there's changes, about the current state of the object.

# add the field, ex: 'name' in a migration
track_history do
  annotate :name
  annotate :name, :as => :the_name
end

# or you can pass a block
track_history do
  annotate(:name) { "#{name} !!!" }
end

If you need to change the field names to work with legacy tables you can do that too:

track_history do
  field :name, :before => :name_from, :after => :name_to
end

And if you don't want the reference field maintained for whatever reason:

track_history :reference => false

Adding methods to *::History

To add methods to your History classes, or create additional relationships that you may need, you can work directly in the track_history block:

track_history(:model_name => 'WorkflowChange', :reference => false) do
  annotate :object_id
  self.belongs_to :object
end

Installation (in your Gemfile)

gem 'track_history', '0.0.10'

Other options

If you need to change the name of the model, you can do something like:

track_history :model_name => 'UserAudit'
track_history :table_name => 'user_audits'

License

The MIT License (see attached)

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.