Oct 17 2010
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:
<h2>History</h2> <% 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.
Oct 13 2010
Using Rails’ Flash Messages with AJAX Requests
Have you ever wondered how to get access to the Ruby on Rails' flash message when performing a AJAX or restful web request? You might hit yourself on the head when you discover how easy it is. Simply append the flash message to the Response headers. You could even wrap this in a helper, and using an after_filter to automatically add the header for you on every AJAX response.
class ApplicationController < ActionController::Base after_filter :flash_headers def flash_headers # This will discontinue execution if Rails detects that the request is not # from an AJAX request, i.e. the header wont be added for normal requests return unless request.xhr? # Add the appropriate flash messages to the header, add or remove as # needed, but I think you'll get the point response.headers['x-flash'] = flash[:error] unless flash[:error].blank? response.headers['x-flash'] = flash[:notice] unless flash[:notice].blank? response.headers['x-flash'] = flash[:warning] unless flash[:warning].blank? # Stops the flash appearing when you next refresh the page flash.discard end
And then you just read the header with whatever you happen to be reading it with. For completeness sake here is an example of how to read the header in JavaScript using Prototype:
new Ajax.Request('/your/url', { onSuccess: function(response) { var flash = response.getHeader('x-flash); if (flash) alert(flash); } });
Oct 13 2010
Forget the UML Module for NetBeans!
A while ago, I wrote a blog post on how, with considerable effort, you can get a native UML NetBeans module up and running despite the NetBeans UML module being removed from the standard distribution.
I managed to get mine working, but there is a huge cost - Once you close the project (or the IDE) housing the diagram, you can never reopen it. Out of pure determination desperation and perseverance I managed to get the diagram I needed, printed and done; but I can never open it and make adjustments.
Apparently, we're all supposed to use SDE for NetBeans by Visual Paradigm now as the "official" replacement, but I tried it, and it was simply fail. Proprietary and fail.
Fortunately, after taking a punt, I found a UML modelling tool which is not only more functional and better than the NetBean's module was, but looks better too. It even has the ability to create code from class diagrams (which you can obviously just cut and paste into your NetBeans IDE project of choice. Its called ArgoUML.
ArgoUML is the leading open source UML modeling tool and includes support for all standard UML 1.4 diagrams. It runs on any Java platform and is available in ten languages.
I've used it a bit now, and I just love it. I particularly like the way it can make recommendations on how to improve your diagram using its "critics" system.
It's features boast:
- All 9 UML 1.4 Diagrams supported
- Platform Independent: Java 5+
- Click and Go! with Java Web Start
- Standard UML 1.4 Metamodel
- UML Profile support with profiles provided
- XMI Support
- Export Diagrams as GIF, PNG, PS, EPS, PGML and SVG
- Available in ten languages - EN, EN-GB, DE, ES, IT, RU, FR, NB, PT, ZH
- Advanced diagram editing and Zoom
- OCL Support
- Forward Engineering
- Reverse Engineering / Jar/class file Import
- Cognitive Support
- Reflection-in-action
- Design Critics
- Corrective Automations (partially implemented)
- "To Do" List
- User model (partially implemented)
- Opportunistic Design
- "To Do" List
- Checklists
- Comprehension and Problem Solving
- Explorer Perspectives
- Multiple, Overlapping Views
- Reflection-in-action
I haven't yet worked out how to create object instances from my class diagrams yet, so I'm not sure if it just doesn't support this or it's user error, but in every other conceivable way, it seems to be an excellent UML modelling application for virtually every OS you can name.

Oct 5 2010