Revenge of the Groovy Guys

Okay, okay, I’ll humbly admit I was wrong. A little bit. About ehm… that Groovy thing. An inspiring session by … about the Grails framework made me see things from a different perspective. There’s actually some quite nice features in Grails that could well be of use in Rails (or in some cases, Rails on JRuby).

For example, I’ve been looking for some time now for a system that would allow custom html tags in Rails’ rhtml files. The idea is to allow for all sorts of html tags, for example

  <r:for_each list="some_list" var="item">
    <li><%= @item %></li>
  </r:for_each>

This would then trigger a method named for_each (e.g. in a app/helpers/application_tags class) which could process the passed inner html block in any way you want (in this example, probably looping through some_list). Even though Ruby code in rhtml is usually not hard to read, it would still clean things up a bit and get rid of some duplicate code.

In short, it turns out that Grails already has this. Grails also has deployment to war (which is still experimental in JRuby, it’s the first thing I’ll try out once I’m back home from Belgium). And because Grails is built on top of Spring and Hibernate (amongst others), it’s easy to script-prototype your model classes that could be Hibernate-mapped or EJB3s. To be honest, that one isn’t any more difficult in JRuby if you see how easy it is to call any EJB.

Another interesting Groovy/Grails language construct I saw:

Book.get(params.id)?.true

The interesting part here is the question mark operator. This will (apparently) check if get() returns null, and if so, prohibit an error being thrown. There was an interesting discussion about this some time ago on rubyenrails.nl (in Dutch; I’ll post the link when I’m home), looking for ways to do this in Ruby.

All in all, I’m still not comfortable with the Groovy syntax (too many curly braces and arrows; and I’m missing semicolons at the end-of-lines–it’s not Ruby after all!) but there’s definitely things to learn and shamelessly steal from those Groovy and Grails guys.

2006-12-14. 6 responses.

Comments

  1. Are you kidding?!

    <r:for_each list=”some_list” var=”item”>
    <li><%= @item %></li>
    </r:for_each>

    is almost exactly the syntax that is used by Radius, the tag library used by Radiant!

  2. @Erik: thanks! Just what I meant. I had seen Radiant, but never realized that the tag library thing can be used separately. Do you know of any integration of Radius with Rails (before I start trying that myself)?

  3. Groovy has optional semicolons. If you like them put them in.

    Glad you like Grails 🙂

  4. Ruby’s Nitro Web framework has a nice custom tag mechanism, as well. Worth peeking at.

  5. Sorry, I have never read something about Radius-Rails integration.

  6. The “inspiring session” was given by Graeme Rocher, whose blog is certainly worth reading: http://graemerocher.blogspot.com/ .