Skip to content

vestal_versions with added :only and :except options

License

Notifications You must be signed in to change notification settings

sthapit/vestal_versions

 
 

Repository files navigation

vestal_versions

Finally, DRY ActiveRecord versioning!

acts_as_versioned[http://github.com/technoweenie/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[http://github.com/mmower/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 :only => [:first_name, :last_name]

  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

About

vestal_versions with added :only and :except options

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%