Adding Wiki Style Functionality to Your Rails Site Using 'acts_as_versioned'

If you need to add basic wiki style functionality to your Ruby on Rails models, there is a really easy way to get similar model versioning without having to resort to cutting the code yourself.

The acts_as_versioned ‘plugin’ has been available for quite some time, but its been made far better by it now becoming a gem instead of an old-school plugin. The authors have gone to considerable effort to make it as painless as possible to use.

This post, is designed to give you a brief over-view into how to get up and running with with models which ‘acts_as_versioned’.  Because its the current version (at time of posting) and because its awesome, this walk-though assumes that you are using Rails 3, not 2.  The instructions for Rails 2 sites are similar, but you’ll need to tweak this for it to work.

First, you need to grab the gem:

sudo gem install acts_as_versioned

Next, add the dependency to the ‘Gemfile’, it doesn’t matter too much where it goes, I stuck it somewhere in the middle:

gem 'acts_as_versioned', '0.6.0'

Next, just under the ‘ActiveRecord::Base’ line in the model’s class file, instruct the class that its to act as a versioned model.

class Article < ActiveRecord::Base
  acts_as_versioned
end

Then, in the migration file you need to execute the model's method to create the version table.  This is key because the acts_as_versioned gem actually creates an additional database table to house all the previous versions of a given record.  Obviously, you need to delete the table is the schema is taken down.  My migration now looks like:

class CreateArticles < ActiveRecord::Migration
  def self.up
    create_table :article do |t|
      t.string :title
      t.string :body
      t.integer :user_id

      t.timestamps
    end
    Article.create_versioned_table
  end

  def self.down
    drop_table :articles
    Article.drop_versioned_table
  end
end

The key method is the

Article.create_versioned_table

which creates the version table of the model. Now, get rake to create the database:

 rake db:migrate

Thats it!  Its done.  Using acts_as_versioned is simple. I'll provide some examples, where '@article' represents an instance of a model setup 'acts_as_versioned'. To find the current version of an article you can use the version property:

@article.version 

But just performing a normal ActiveRecord lookup returns the most current version anyway, so to revert to a previous version use the revert_to method on an article instance:

@article.revert_to(version_number)

You can save (just like you've done a hundred times before) a previous version as the current on by using the save method. The save on a reverted articles will just create a new version.

To get the number of versions:

@article.versions.size

Since '@article.versions' returns an array of versions, you can do neat things like this:

History

<% for version in @article.versions.reverse %> Version <%= version.version %> <%= link_to '(revert to this version)', :action => 'revert_to_version', :version => version.id, :id => @article %>
<% end %>

Obviously for this to work, you'd need to create a 'revert_to_version' action in the appropriate controller, but you get the idea.

acts_as_versioned is an amazing piece of work, and aside from the wiki-like functionality it gives you for very little effort, I can imagine scenarios such as audit and trace logs and "undo" features which could really benefit from this little gem.

10 Absolute *must have* WordPress Plugins

Akismet

Akismet checks your comments against the Akismet web service to see if they look like spam or not and lets you review the spam it catches under your blog’s “Comments” admin screen.

3

Google Sitemap Generator

This plugin will generate a special XML sitemap which will help search engines like Google, Bing, Yahoo and Ask.com to better index your blog. With such a sitemap, it’s much easier for the crawlers to see the complete structure of your site and retrieve it more efficiently. The plugin supports all kinds of WordPress generated pages as well as custom URLs. Additionally it notifies all major search engines every time you create a post about the new content.

2

Echo

Echo is the next generation commenting system. It’s the way to share your content, and watch the live reaction. You can quickly embed Echo on WordPress, Blogger, or any website and turn your static pages into a real-time stream of diggs, tweets, comments and more.  It’s not free, but it *is* cheap and worth every dollar.

echo

WordPress Super Cache

This plugin generates static html files from your dynamic WordPress blog. After a html file is generated your webserver will serve that file instead of processing the comparatively heavier and more expensive WordPress PHP scripts.

12

Easy AdSense

Easy AdSense provides a very easy way to generate revenue from your blog using Google AdSense. With its full set of features, Easy AdSense is perhaps the first plugin to give you a complete solution for everything AdSense-related.

11

Google Analytics for WordPress

The Google Analytics for WordPress plugin automatically tracks and segments all outbound links from within posts, comment author links, links within comments, blogroll links and downloads. It also allows you to track AdSense clicks, add extra search engines, track image search queries and it will even work together with Urchin.

10

Add to Any: Share/Bookmark/Email Button

Help readers share, save, bookmark, and email your posts and pages using any service, such as Facebook, Twitter, Digg, Delicious, and over 100 more social bookmarking and sharing sites. The button comes with AddToAny’s customizable Smart Menu, which places the services visitors use at the top of the menu, based on each visitor’s browsing history.

6

Twitter Tools

Twitter Tools is a plugin that creates a complete integration between your WordPress blog and your Twitter account.

17

Broken Link Checker

This plugin will monitor your blog looking for broken links and let you know if any are found.

14

FD Feedburner Plugin

The Feedburner Plugin redirects the main feed and optionally the comments feed to Feedburner.com. It does this seamlessly without the need to modify templates, setup new hidden feeds, modify .htaccess files, or asking users to migrate to a new feed. All existing feeds simply become Feedburner feeds seamlessly and transparently for all users. Just tell the plugin what your Feedburner feed URL is and you’re done.

If you used blogger, you no doubt integrated your feed with Feedburner, seeing as Google bought it and integrated it, as it adds some wonderful
functionality to your feed with the ‘Add to Del-ici-ous… Digg… Email…. Technorati’ etc, but also its AUTOMATIC pinging to different news services, and sites to tell them you have new content. It also splices in flickr, del-ici-ous or furl, it adds Google Adsense to your posts and so much more. So you will more than likely want to keep this and so it lets you redirect your wordpress feed to your own unique wordpress feed, therefore keeping the AUTOMATED updates and subscribers are none the wiser it is coming from a different blogging platform.

Just need to go into feedburner configuration under appearance to set the feeds and redirects (took me a while to find it, prob should have read
installation. ;o)

Extending Explorer with Band Objects using .NET and Windows Forms

One of the great things about Firefox, is how easy it is to extend. Internet Explorer and Windows Explorer, not so much.  If you’re a ATL or COM guru then you might not find it so hard, but for those of us from a Managed Code background; its like splitting wood in your eyes.

A while ago I found a great article on CodeProject by Pavel Zolnikov, which walks you through the implementation of an Explorer bar with the help of BandObject base class. Describes implementation details of the BandObject class.

This was excellent, but it really didn’t work using the .Net 2.0 framework – that is until I found this little gem Band Objects – .NET 2.0 Redux.