Monday, January 28, 2008

Ruby, Rails, Groovy, Grails, What's a programmer to do?

I'm doing some prototyping on a possible project for a client and have been getting my feet wet with the Grails Framework. I feel like know my way around Rails fairly well as I've done a few different small web applications in the framework and although there was a learning curve and Rails does have it warts, overall Rails really has saved me a TON of time and made me write better and well tested code.
For this project with my client, I'll be leaving it in their possession and feel that selling them on Groovy and Grails was a slightly easier sale as they already are a 100% Java shop. I think making the switch to writing Groovy, using their existing IDE (IntelliJ) and deployment in a J2EE container, would make them a little more comfortable than having to learn Rails. The JRuby guys are doing awesome work and if it was entirely up to me, I'd take the Rails route and consider using JRuby for production deployment if needed. But, getting them even to consider another framework was enough of a strain and if I told them I was leaving them an application in Ruby, I think I would most likely have been shown the door.

So, alas, I walk the Grails line. I must admit, it has been an interesting experience so far and one that I'd like to write more about in a future posting. I did however come across this blog posting in my research and have found it sums up very well some of the feelings I have for the whole Grails / Rails debate I see ensuing. I'd highly suggest reading the article.

Overall, I think each has it's place. I would agree that with Grails I find myself spending more time learning the framework because of poor or outdated documentation, and picking p Groovy the language relatively quickly. Where with Rails, I find myself learning the framework quickly, but taking more time to learn Ruby. It might however have to do with the fact that I learned Rails first and in some ways have to break my mind of some of the differences between what I learned in Rails and how it's done in Grails.

Regardless of which side you land on, I think it's worth your time to atleast investigate and learn about these frameworks as these are going to become more and more popular methods in developing applications as time marches on.

Tuesday, January 1, 2008

Upgrade to Rails 2.0

Well, to usher in the New Year, I bit the bullet today and tried upgrading one of the applications I built for my client to Rails 2.0 . The process to do the upgrade was relatively painless. The whole idea of Rubygems is awesome and is something I feel that the Java community is horribly lacking.
Installing Rails 2.0.2 was a simple as:

>sudo gem install rails

And then changing the line in the environment.rb to:

RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION

The issues though came in when I actually tried to run the application. Here's a brief rundown of the refactoring that I had to do and some gotchas that maybe other will run into.

1. Pagination - I had heard all of the warnings that the default pagination was going to be taken out of Rails 2.0, but like more programmers I am somewhat lazy ( or too busy ) to implement changes until I really NEED to. Well, if you upgrade to Rails 2.0 and you use the default pagination functions in Rails 1.2, you will NEED to do something else. It has been ripped out for Rails 2.0. I must say I agree with the the idea to pull it out as pagination is something that really should be done by a plug-in. After some searching I found a two options for people in my same situation.

a. classic_pagination - This is the plugin that will give you the same functionality of the old Rails 1.2 pagination. Many in the Rails community frown on the use of the old code for performance reasons, flexibility and good object oriented design.

b. will_paginate - This is the plugin that is seems like the Rails community is backing going forward for pagination. It offers some advantages of the old pagination methods and doesn't require too much refactoring to implement. Here is a good screencast on using it.
I decided to go with will_paginate since this is more the standard and new projects going forward this is what I will probably use, so it made sense for me to get aquainted with how it works. It turned out to be actually very easy to make the changes. He is an example of a change in the controller that I needed to make for "Jobs".

This:
@job_pages, @jobs = paginate(:jobs, :order => 'deadline')

Was changed to:
@jobs = Job.paginate(:page => params[:page], :order => 'deadline')

and in my view this code:
Page Number: <%= pagination_links(@job_pages, :params => {'job[filter]', @job_filter }) %>

was changed to simply:
<%= will_paginate @jobs %>

Much easier to read, understand and maintain. I'd suggest watching the screencast I mentioned above to learn more.

2. Nested RESTful routes - This was very annoying and was a little difficult for me to find much information on how to fix it. It seems like when you define a nested route in the routes.rb in Rails 2.0, the helper methods that are created don't behave the same or work the same as they did under Rails 1.2 This is what I had previously.

We have a relationship with jobs containing artifacts. So in our routes.rb:

map.resources :jobs do |job|
job.resources :artifacts
end

and in the a show.rhtml view had:

artifacts_path(@job)

When running under Rails 1.2 this works fine and generates the url

/jobs/1/artifacts URL.

Under Rails 2.0 we get an error:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.to_sym

Annoying. Well it turns out you have 2 options.
a. Modify your routes.rb to be

map.resources :jobs do |job|
job.resources :artifacts, :name_prefix => nil
end

b. Change the call to artifacts_path to instead be:

jobs_artifacts_path(@job)

There is information on this helper methods here. Thank god I found this reference. That is one thing that I do find frustrating about Rails is the lack of the documentation I was able to find when going through an upgrade. It seems like this issue I ren into should have beein BIG BOLD letters warning people that old RESTful methods don't work the same with out the changes that I mention. Anyway, I digress. I decided to go with the more simpler change of adding :name_prefix => nil to my routes.rb since I was worried about making sure I found all of the helpers. Hopefully this is the right choice and will work in all my cases. It seems to work for the time being.

3. Running unit tests in Textmate - It seems like there is some issues with a file called Builder.rb and it conflicting with Builder.rb in Rails 2.0. I couldn't run my tests in Textmate until I did what was described here. Not a big deal, but again... annoying and nothing that I have seen other documentation. With 75% of the Rails community using TextMate I'm surprised that at the very least DHH didn't mention it in the release notes for Rails 2.0

4. Capistrano - Capistrano 2 is what is now used by default with Rails 2.0. I tried running a simple 'cap deploy' after upgrading an have been getting some odd errors. I need to track down exactly what is going on. I had thought that deploy.rb was backward compatible, but I guess not. For now I have been doing `cap _1.4.1._ deploy` and forcing it to use the old version for now. I'll track this down shortly.

5. Flash.now - So, I re-ran by functional tests and started getting failures. I have the following line in my application.rb

flash.now[:error] = "You are not authorized."

When running under 1.2.3 and my functional test I have an assert to validate that the error returns in the flash.

assert_equal "You are not authorized", flash[:error]

However, when running under Rails 2.0.2, THE FLASH IS NOT RETURNED! Why is that?

If I change the line in the application.rb and remove the .now to be

flash[:error] = "You are not authorized."

THE TEST PASSES! Not sure why this is in the case. I haven't seen anything in the release notes that mention an upgrade would cause this. Maybe I'll send a post to the RUM group to see if others have seen something similar.

All in all though, the upgrade to 2.0.2 hasn't been too bad. I guess I was hoping that it would be trivial, but I guess what can I expect? I did come from 1.2.3 all the way to 2.0.2 and I can't expect everything to just automatically work right out of the box. I has however only been 8 months since I started work on this system and am surprised the versions that we are up to in Rails. I'm excited to see where things are in at in another 8 months!

Sunday, December 9, 2007

Adhearsion

I've been spending a fair amount of time working with the open source PBX system called Asterisk. Calling it powerful in the world of telecommunication doesn't do it justice. It's what I would call a "disruptive technology" that in coming years is going to shake up and open the way that we develop application that interact between our phone and internet. It's a solution to problems that haven't even been thought of yet and the best part that I'm finding is that it's FUN to develop applications in this space!

Well let me re-phrase that... It's FUN when you use Adhearsion! With Asterisk, as it is with other PBX systems for the system to be of any use there must be routing logic on phone calls that are coming in. Asterisk has it's own macro language you can use, but it also provides a way to call out to another process to perform the tasks. This is where Adhearsion steps in. Using Ruby, you are able to create some very neat and sophisticated applications and abstract yourself away from the internals of Asterisk. On top of that, you can easily integrate in Ruby on Rails to provide the functionality of using the internet for your application.

As I mentioned in previous post, I heard Justin Gehtland speak at No Fluff Just Stuff and he talked about contributing to open source and sometimes just reporting a bug or two or working on documentation can provide a great help to a project. Well, a few weeks ago I realized that I have found my project. The Adhearsion project is an open source project lead by Jay Phillips who has written some extremely beautiful Ruby code. Looking over the code is inspiring in itself and I have learned so much in the way of the Ruby language just by reviewing the source. I therefore decided to take it on myself and contribute to the documentation WIKI. I submitted an article on how to combine Adhearsion with Rails and create what I call the "Quiz Show" application. It's my first attempt at writing documentation so it's got some rough edges, but it shows just how powerful and fun working in Adhearsion and Rails can be. Check out the documentation here.

I am also working with JRuby and am planning to contribute to the documentation a section on Adhearsion and JRuby as well. I'm planning in the coming months to do a few a different presentation about Adhearsion at the local Twin Cities Asterisk and Ruby Users of Minnesota Groups. I hope people get as excited about this project as I am. It's going to be really interesting in the coming years to see how open source PBX like Asterisk and open source frameworks like Adhearsion bring the barrier of entry to telecom applications down to a point where a small company like Localtone Interactive can provide competitive solutions for business and integration with other products and services.

Tuesday, November 13, 2007

Localtone Radio Redesign Launched


After many hours of work and help from Mykl Roventine, Localtone Radio has a spiffy new look and some exciting new features.
Check out the new look and sign up if you haven't already done so. Localtone Interactive is very proud to be the only technology company to build and internet radio station where the listeners contribute and vote on the music that is to be played. It's a fresh and new idea in media consumption. Please feel free to contact us if you have any needs like this in your organization.

Sunday, October 21, 2007

Sun Midwest Java Technology Day

Sun had their Midwest Java Technology Day on Tuesday to tout a number of the new technologies they are introducing to the market. I'm excited the Sun has an interest in promoting dynamic languages in the JVM and the keynote by Tim Bray was no exception. He talked about how right now is a perfect time to start you own company. The barriers to entry are the lowest they have ever been in terms of open source software and if you have an idea and are willing to do hard work, you can do it. It was very inspirational for an entrepreneur myself. In not so many words he said, "Leave your boring day job and work in technologies that interest you!". Great advice. I've been in business for myself for over a year now and have absolutely loved it. He also had a number of slides discussing the great advantages of using a Ruby on Rails for your next web application (or start up).

The remainder of the conference was pretty typical with EJB 3.0, Glassfish and the new features in JDK7. I does seem that Sun is getting the message of a more light and agile stack in the frameworks and creating web services in NetBeans running Glassfish is pretty neat. However, the fact that the head technology evangelist at Sun is talking Ruby to CEO's at companies makes me feel good that this Ruby thing might be more than just a fad. I know I've found some great productivity gains when working in Rails and once the managers see the benifits in terms of cost, the services Localtone Interactive provides will be very much in need.

Monday, October 15, 2007

Open Source Project - Walk The Walk

While at the No Fluff Just Stuff Conference, Justin Gehtland had an excellent talk on JRuby and Open Source. He went though some of the really cool features of JRuby and for someone like me know has been doing Java for 10+ years and would like to leverage some of the libraries I know in Java from Ruby, JRuby is very exciting.

What I also found excellent about Justin's talk was that he went though how one contributes to an open source project. From checking out the code from SVN, to using a bug tracking tool, to submitting a patch. He used the JRuby project as an example, but really the process is the same with others. He also at the end encouraged people to put aside say, 4 hours a week to work on an Open source project. At his company, Friday afternoons from 12-4 are considered "open source time" where everyone in the company spends their time working on open source.

What a great idea! I use open source software everyday, from Open Office, to Firefox, to Ruby on Rails to countless number of libraries in my projects. Why not create this rule in my company? Why not create something similar in your company? I've already found a project that I'm going to download and look at working on. I challenge you to do the same. Find a project that you find interesting and contact the owner and see how you can help. It doesn't have to be code. Documentation, testing and even at times, donations of money can go a long way to improving a project. Sourceforge and Codehaus are two places that host some great open source projects. Have fun!

Saturday, October 13, 2007

No Fluff, Just Stuff

I attended the No Fluff Just Stuff conference this past weekend in Bloomington, MN. I'm not sure if the conference itself was sold out, but it seemed to be very well attended. It started on Friday at noon and concluded on Sunday evening around 6:00. I had a great time seeing some familiar faces and catching up with a few people that I hadn't seen in a number of years. A few things that I took away from the conference.
  • Dynamic languages are hot and are only going to get hotter. Frameworks like Ruby on Rails and Grails are going to continue to grow in popularity, even for larger organizations. This was a great reality check for me. Working in Rails and Grails is where I have been focusing my business efforts for this past year. It's good to see that I am somewhat ahead of the curve on thee awesome technologies.
  • There were a great number of people who are managers at this conference - read: the people who make the decisions on the software to use. The fact that there were so many sessions on groovy and grails and the people that actually make the decisions on the software technology to use in their organization were there and were listening, means that we will most likely see tangible projects and needs from business asking for people with Groovy and Grails skills.
Overall I'd recommend checking out the No Fluff Just Stuff Conference when it comes to your town. It usually comes through Minneapolis every 6 months or so. While I wouldn't go every time it comes through, probably one a year to 18 months is worth the cost for admission.