Like rats deserting the good ship Merb - introducing merb-to-rails3 railtie
by John Cieslik-Bridgen, 31 March 2010
This blog post isn't meant to be a Merb vs Rails discussion, fun though that can sometimes be – instead we'd like to publicise a small utility library that Marcin Kulik and Piotr Solnica have written to help you port a Merb application to Rails 3, by adding several methods from Merb's API to your Rails 3 app. The background to this gem is that Lunar Logic Polska have a mix of deployed applications, some Rails 2, some Merb, and also two distinct camps within the company – the Merb lovers, and those who have yet to try Merb...
Merb fans here pointed to better performance and higher productivity. An architecture that was extensible, that avoided "monkey patching", a clean API for plugin development. It's true to say that we have here a small, vocal Merb fanbase, and they have tended to evangelize enough to convert others along the way!
So whilst some of the team here at LLP are decidedly Merb converts, having looked at the current state of Rails 3 development against Merb, we decided that we would port all of our applications to Rails 3. It seems to us that there is now only very limited development of Merb, and whilst we have recently seen the Merb 1.1 release, this was something we'd been waiting for a year for. Just a few months ago the picture was quite different – but the beta release of Rails 3 changed the landscape dramatically, and prompted our decision. We think that Rails 3 is the future. Indeed, the release notes of Merb 1.1 write that whilst it is intended that Merb should have a future, it is not clear what that future is.
The merb-to-rails3 gem provides proxy methods for Views and Controllers to ease your migration from Merb to Rails 3:
Controller:
- redirect => redirect_to
- before => before_filter
- after => after_filter
Controller/View:
- url(name) => name_path
- resource(...) => ....._path
- submit => submit_tag
- css_include_tag => stylesheet_link_tag
- js_include_tag => javascript_include_tag
- throw_content(name, ...) => content_for(name, ...)
- partial => render :partial
The gem essentially provides a collection of helper methods that will allow you to get an application up and running, taking care of some of the most frequent exceptions you might encounter, logging warnings to the console that you can deal with at your leisure, and also suggesting alternatives for deprecated methods. Here's an example of console output:
The alternative to using our gem would be to generate a fresh Rails 3 application and deal with exceptions one at a time – but by providing proxy methods, we aim to at least have the core of a migrated application in a state where it can be worked on largely without exceptions.
This little gem also allows you to handle Merb-style controller exceptions (ie. raise Forbidden). You just need to add rescue_from MerbToRails3::ControllerExceptions::Base, :with => :my_handler to your ApplicationController. Like this, for example:
In an ideal world, the gem would be all that is needed, but in reality, manual intervention is still required in some instances, as some issues can't be resolved by proxying methods. For example, in deciding how to deal with Merb's behaviour whereby the return value of a controller is the response body returned to the browser. Such differences require human input, and we have intentionally required this, althought we did discuss a rake task that could run regexps over controllers, suggesting replacements as a possible future solution. Consider the example below, contrasting controller behaviour:
Another suggested enhancement would be to create a MerbController class, a subclass of Rails' AbstractController which would handle actions the Merb way – this could be set as a base class for your Application (controller). It could be argued of course that this is short-termism, as we wouldn't propose attempting to replace the Merb API in Rails 3. Also, whilst we considered converting Merb route definitions to Rails 3 routes, we haven't included that for now, as we decided that this was really a one-time, one-file task"
There are then some "nice-to-haves" that our gem doesn't provide – but as ever, we're busy, pragmatic people, who have to balance such decisions with the rest of our workloads! This is Open Source software, and we would encourage any contributions, but in the meantime, we hope that it will be a useful resource for other developers migrating from Merb to Rails 3.
How to set it up? Just add it to your app's Gemfile:
gem "merb-to-rails3"
and run:
bundle install












