Skip to content

austinarchibald/attr_tracker

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AttrTracker

Allows tracking of changes to an attribute.

Suppose you need to track the changes to the price of a product. Each update to the price (or any tracked attribute) creates a new instance of AttrTracker::Changes. Calling product.price_changes returns all of the price changes for the product.

Getting Started

Add AttrTracker to your Gemfile:

gem 'attr_tracker', github: 'iamkevinlowe/attr_tracker'

Run bundle install to download and install the gem, then rake db:migrate to set up AttrTracker's tables.

Note: Reset your server if your application is currently running.

Usage

To start tracking an attribute, add the following to your model's class:

class Product
  tracks :price # accepts any number of valid attributes
end

Class Methods

AttrTracker adds a class method to return all attributes of the class that are currently tracked.

  • ::tracked - Returns an array of the tracked attributes.

    Product.tracked => [:price]

Instance Methods

AttrTracker adds methods to an instance of a class to retrieve its changes.

  • #{attribute}_changes - Returns all AttrTracker::Changes of an attribute

    eggs.price_changes => [{before: 2.50, after: 2.75, created_at: 2014-10-15},
                           {before: 2.75, after: 2.99, created_at: 2014-10-31},
                           {before: 2.99, after: 3.25, created_at: 2014-11-01},
                           {before: 3.25, after: 3.50, created_at: 2014-11-15}]
  • #{attribute}_at(date) - Returns the most recent AttrTracker::Change before the given date.

    eggs.price_at("2014-10-31") => {before: 2.75, after: 2.99, created_at: 2014-10-31}
  • #{attribute}_between(start_date, end_date) - Returns the AttrTracker::Changes between the given dates

    eggs.price_between("2014-10-30", "2014-11-02") => [{before: 2.75, after: 2.99, created_at: 2014-10-31},
                                                       {before: 2.99, after: 3.25, created_at: 2014-11-01}]

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 76.2%
  • HTML 15.9%
  • CSS 4.2%
  • JavaScript 3.7%