Acts As Versioned Plugin
The acts as versioned plugin allows you to manage different versions of a model record so that you can revert to a previous version or create new ones. To install execute the following command:
script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_versioned
Make your model act as versioned.
class Post < ActiveRecord::Base acts_as_versioned end
Versioned post are saved in a separate database table. You can create the versioned table that will contained older posts by invoking the following statement.
Post.create_versioned_table
Normally you can create and drop (via the drop_versioned_table function) the version tables in a ActiveRecord Migration. A side effect of invoking the create_versioned_table is that a new version integer column will be added to your original posts table. The version column will be added automatically by the create_versioned_table.
There is a slight issue with this plugin that I should note. If you have a table posts, you can’t have a post column, you will get a ActiveRecord::AssociateionTypeMismatch error if you do. This has been an issue with my models that I haven’t used it as much as I would like. I don’t want to use this plugin until I, or most likely someone else, solves this. If you want to or need to use this plugin you will need to rename the post column to something else.
So once you have your tables correctly setup, you can start creating versions of your post just by saving your them.
post = Post.new(:txt_post => 'My Post') post.save
Every time you save a record of your model a copy of that instance will be stored in the post_versioned table, even if no attributes or date has been modified from the current version.
To find the current version of a post you can use the version propterty.
post.version
To revert to a previous version use the revert_to method on a post instance.
post.revert_to(1) # original post
You can save a previous version as the current on by using the save method. The save on a reverted posts will just create a new version.
Technorati Tags: ruby, rails, ruby on rails, plugin, rails plugin, activerecord, acts_as_versioned