Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Keep a DRY history of your ActiveRecord models' changes

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 generators
Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 VERSION
Octocat-spinner-32 init.rb
Octocat-spinner-32 vestal_versions.gemspec
README.rdoc

vestal_versions

Finally, DRY ActiveRecord versioning!

acts_as_versioned by technoweenie was a great start, but it failed to keep up with ActiveRecord's introduction of dirty objects in version 2.1. Additionally, each versioned model needs its own versions table that duplicates most of the original table's columns. The versions table is then populated with records that often duplicate most of the original record's attributes. All in all, not very DRY.

simply_versioned by mmower started to move in the right direction by removing a great deal of the duplication of acts_as_versioned. It requires only one versions table and no changes whatsoever to existing models. Its versions table stores all of the model attributes as a YAML hash in a single text column. But we could be DRYer!

vestal_versions keeps in the spirit of consolidating to one versions table, polymorphically associated with its parent models. But it goes one step further by storing a serialized hash of only the models' changes. Think modern version control systems. By traversing the record of changes, the models can be reverted to any point in time.

And that's just what vestal_versions does. Not only can a model be reverted to a previous version number but also to a date or time!

Installation

In environment.rb:

Rails::Initializer.run do |config|
  config.gem 'laserlemon-vestal_versions', :lib => 'vestal_versions', :source => 'http://gems.github.com'
end

At your application root, run:

$ sudo rake gems:install

Next, generate and run the first and last versioning migration you'll ever need:

$ script/generate vestal_versions_migration
$ rake db:migrate

Example

To version an ActiveRecord model, simply add versioned to your class like so:

class User < ActiveRecord::Base
  versioned

  validates_presence_of :first_name, :last_name

  def name
    "#{first_name} #{last_name}"
  end
end

It's that easy! Now watch it in action…

>> u = User.create(:first_name => 'Steve', :last_name => 'Richert')
=> #<User first_name: "Steve", last_name: "Richert">
>> u.version
=> 1
>> u.update_attribute(:first_name, 'Stephen')
=> true
>> u.name
=> "Stephen Richert"
>> u.version
=> 2
>> u.revert_to(:first)
=> 1
>> u.name
=> "Steve Richert"
>> u.version
=> 1
>> u.save
=> true
>> u.version
=> 3
>> u.update_attribute(:last_name, 'Jobs')
=> true
>> u.name
=> "Steve Jobs"
>> u.version
=> 4
>> u.revert_to!(2)
=> true
>> u.name
=> "Stephen Richert"
>> u.version
=> 5
Something went wrong with that request. Please try again.