Thursday, October 23, 2008

Converting legacy Rails apps to Grails (with NetBeans) : General Project Setup

It is hard to describe the pleasure of writing the title above, especially the "legacy" part :-) Although lately the jumping up and down of Ruby & Rails fanboys has subsided a little, after people started realizing that Rails is not going to kill anything (much less Java, hell if PHP people start going back to PHP that says A LOT!!!). So, a few years later I got a chance to chime in on the subject.

Anyway, the specific reason for writing this post is that I had a chance to take an existing Rails app, and move it to Grails. Some of the reasons for even attempting is that as much effort and hype has gone into Ruby and Rails, the (C)Ruby is not even close in being able to integrate with everything else like Java can. When you throw into the mix that even JRuby is starting to be faster than (C)Ruby, and that Groovy kicks the pants out of JRuby, there's also a performance story to be told for Grails. Finally, when you throw in the existence of a clear migration path in performance sensitive Groovy/Grails components directly into java (e.g. moving Groovy controllers to Spring MVC controllers, or Groovy services to Java services), migrating from Rails to Grails for integration for performance purposes is just a no brainer.

Anyway, enough of this high level stuff. I'm sure that if some Rails fanboys get a hold of this article, I'll probably get flamed with claims how Ruby & Rails can perform, and who knows what else, but I'll just leave the high level stuff at what I said above, and I'll focus on on comparing what I saw in Rails from the point of view of a Grails (and long term Java) developer. Additionally, I will be using a daily NetBeans 6.5 build (very close to RC1) to illustrate the steps I take along the way.

So, let's get started.

1. First, getting started with a project. In both Grails and Rails it's very simple to get started with the facilities NetBeans provides. For both Grails and Rails , NetBeans 6.5 provides wizards for creating a new project.

Now, a few comments on the contents of the screenshots. First of all, NetBeans 6.5 ships directly with Groovy & Grails support for the "Java" packaged download (e.g. from In contrast, for the Ruby/Rails support, you have to either download the Ruby package from the same location, or after downloading the Java installer / package, you have to go to the plugin center and install the Ruby/Rails plugin (either way, quite easy). Secondly, when you look at the options for Ruby/Rails projects, you might think that there are more options w/ Ruby and Rails, but it is actually quite deceiving. The reason that there are no separate options to create a "new Grails app w/ existing sources" is that NetBeans totally rocks and can directly open an existing Grails project without having to add any additional project data. On the Groovy front, you can just add Groovy scripts and classes into any Java project, so the extra options in the project menu are just not needed.

So, at first glance, very marginally and despite the outstanding NetBeans Ruby/Rails support, Grails scores the first point for me.

2. Second, I had to figure out what the general setup of a Rails app. Here's what a typical project structure looks like in both Grails and Rails:

Now, comparing the two project structures, it's very easy to get around the Rails app if you're familiar w/ a Grails project layout. A couple of things to note in the Rails project structure :
* The root of the web app is in the "Public" directory in the Rails app, while in the Grails project it's in the "Web Application" project folder.
* There is a "helpers" folder in the Rails app, which initially puzzled me. In most general terms, the helpers folder contains "controller helpers". Roughly speaking, the functionality that existed in "helpers" in the Rails app eventually founds its way in taglibs in the Grails app. Although I'm not 100% certain of the convention and usage of these helper methods in the Rails app, it seemed like the Rails active scaffold seemed to use some parts of what's in the helpers (however, i could be wrong).
* There is the "Migrations" folder in the Rails project, which seems to be generally useful and not present in the Grails app. Now, I would imagine that such a database centric functionality might not be that relevant in a Grails app, as a Grails app really isn't as aware of the database (as it deals w/ the domain model and not database columns as the Rails app does). Still, it seems like because Grails apps end up very much data driven, some method of managing the schema modifications could be generally useful (although, I really don't have any specific suggestions of what such a tool might be).
* (although you don't see this in the screenshots) If you had plugins installed into your Grails app, you would have a "Plugins" folder, which is roughly equivalent to the Rails "Vendors" foler.
* Finally, the Grails app has a folder called "Services" for creating transactional services , for which the Rails app doesn't have an explicit counterpart. More on transaction handling later.

All in all, in the rest of the project layout, they're very similar, and if one knows the one framework, it's pretty straightforward to grok the other one.

Alright, this is the general setup stuff so far. I have a lot more to write about : plugins, ajax support, services, transactions, the whole nine yards. Just to give you a sneak preview, the bottom line is that migrating a Rails app to Grails works very nicely, although not without a few minor hurdles to jump over. More on that in the next post, stay tuned...