CATEGORY: Ruby On Rails

It has been a while since I last posted a blog on rails. I have been tied up with lots of stuff lately. And I just got my time back to move on with my first Rails project to build QuoteJetty. The ride has been fun, though many times I still have to google to find answers for issues that are not covered in Rails books.

One thing that I bumped into is the usage of flash[:notice].

I found out that flash[:notice] is meant only for redirect action, because the message is only cleared after at the end of redirected view request. So if you don’t redirect the request, and you click on the next request, that message will still be displayed.

But sometimes, we just want to display custom error messages only for current request without the redirection (esp. for non-ActiveRecord error messages). So the option is to use flash.now[:notice]. It clears the flash message at the end of current request (without redirection), e.g.

def some_method
    .....
    flash.now[:notice] = 'some message'
end

But there is an issue with functional testing using flash.now[:notice]. Since the message is cleared at the end of the request, you won’t be able to retrieve the flash[:notice] value in your functional test. But no worries, here is the alternative check, use assert_tag.

assert_tag compares the actual html output of your test in a convenient way, just like most of Ruby methods. Click here for the API doc.

def test_method
    ....
    ....
    assert_tag :tag => 'div', 
        :child => /[replace with your message]/
end

Windows command console is quite primitive, it is good to have the power of Unix console on your Windows. If you are like me, you could have installed Cygwin and you probably have installed Ruby using One-Click Ruby Installer, but encountered some problems running ruby, rails and other ruby-related commands on Cygwin.

Well, same here, and I think it is easier to install a seperate copy of Ruby under Cygwin. Fixing the path differences between Windows and Cygwin consoles can be messy, here are some tips to install RoR on Cygwin:

  1. Install a seperate copy of Ruby using Cygwin setup wizard. Expand All > Devel and select ruby and proceed with the setup.

  2. After you have installed Ruby, download Ruby Gems, extract the setup files to a temporary directory, and when you try to run the setup, you probably will encounter an error.

    ruby setup.rb
    ruby: No such file to load -- ubygems (LoadError)

    Tips:

    When you install Ruby using One-Click installer, it will set an environment variable RUBYOPT=rubygems. It tells ruby to load rubygems library when running a ruby file. You have not installed Rugy Gems, thus the setup.rb execution failed.

    Just unset RUBYOPT variable and re-run the Ruby Gems setup:

    unset RUBYOPT
    ruby setup.rb

  3. Then you can install Rails using Ruby Gems by running:

    gem install rails --include-dependencies

  4. You have irb configuration file (.irbrc file) under your windows home directory, and you still want to use it in Cygwin, create a symbolic link to the file in Cygwin home directory

    cd $HOME
    ln -s [/path/to/windows-user/homepath/].irbrc .irbrc

I’m a New Ruby Convert

MON, 27 MAR 2006

Human being always craves for something better, if you are staying in rented room, you wish for a flat, when you have the flat, you wish for a condo.

It’s the same with software development, we, the developers, mere mortals that have becomed slaves of computers, always wish for a better programming language. A more efficient and easier to maintain codes. Object oriented (OO) programming language especially Java and C# have been gaining lots of adoption for the past few years because it is easier to maintain your codes in object oriented ways.

Though OO programming language is good, it still lacks the agility of scripting language. With customers demand more for each dollar spent, I too, was pushed to yearn for something better. And today I find Ruby; it somehow manages to combine the goods of OO and agility of scripting language. And in today’s context, it simply makes sense, and I’ve decided to be a new Ruby convert.

The DRY (Don’t Repeat Yourself) principle is just too right to be true. It’s amazing to see how few lines your codes can be. The other principle “Convention Over Configuration” saves much of your trouble if you are to try to figure them out yourself. And the best part is all Rubyists speak the same conventions.

Though Ruby is still young, it has been gaining lots of momentum and it will absolutely be the NEXT BIG THING.

Testing with Ruby On Rails

WED, 22 MAR 2006

Remember those days when you fixed a bug only to introduce more bugs in return? That’s what testing is all about, to be more specific is incremental testing. When you write some procudures, you should also write the test procedures. So when a procedure is modified, you retest all the procedures available, then your codes will be less breakable.

Ruby On Rails (RoR) is built with incremental testing in mind, it supports unit testing, functional testing, mock object, performance testing, benchmarking and test data loading support. In java you will have to look at different components such as JUnit, Canoo WebTest, JMock/Easy Mock, JMeter, and DBUnit, not to mention many other libraries trying to do similar things. The problem is the learning curve is high and the components are not as cohesive as RoR test framework.

Here are some points that I like most about RoR test framework compared to Java framework:

Test Templates
It supports auto creation of test templates for controller and model class.
Test Data Format:
Thank God it’s not xml, it’s called YAML (YML Ain’t XML) or “fixtures” the term used in RoR. It’s very easy and intuitive, and hell ya, you can even include ruby codes in there, e.g.
user_supervisor:
        id:             supervisor
        name:           Clark Kent
        date_available: <%= 1.day.from_now.strftime("%Y-%m-%d %H:%M:%S") %>

<% for i in 1..20 %>
user_<%= i %>:
        id:             <%= i %>
        name:           User <%= i %>
<% end %>
Unit Test
In your test code, you can refer to the test data using @user_supervisor, which is very important to create a more flexible test code that caters to changes in test data without breaking your test procedures. e.g.
test_load_supervisor:
        ....
        assert_equal user.name, @user_supervisor.name
end
Rake Command
Rake command is Ant’s brother in Java, they do similar things, only Rake has built-in useful options for developer. I love “rake recent” to test recent updated unit tests (updated in last 10 mintues).
Fewer Lines of Codes
I just can’t resist repeating this point!!!

Ruby On Rails Maybe God Sent

MON, 20 MAR 2006

Last weekend I decided to get my hands dirty on Ruy On Rails. I bought Agile Web Development with Rails book and got hooked immediately.

I studied Visual Basic during my Uni days, but fell in love with Java after my first job. And I’ve been faithful every since, Java has tought me the beauty of object oriented programming, design patterns and frameworks. But due to its flexibility and large number of open source projects. It prones to entangle developers into a mesh best practices to adopt.

For web framework itself, it ranges from request-based (Struts, Web Work, Spring MVC) to component based frameworks (JSF, Tapestry, Wicket), and the combination of both (Stripes) to full-stack framework like RIFE and Appfuse. I used Struts before and when I was looking for a better framework I have to read up JSF, Tapestry and Spring MVC. And I finally settled with Spring MVC, though each framework have its own good and bad points. How about persistence framework (Hibernate, EJB, JDO, iBATIS)?

Learning Ruby On Rails is a different experience, it felt  like sailing through a calm sea with warm sun light and breezy wind. Maybe my bakground in Java and web development industry helps. But it is also mainly due to Ruby’s main guiding principles,  “Don’t Repeat Yourself” and “Convention Over Configuration.” So things are laid for you, chosen and decided. No questions asked. The codes are short, clean, and there are lots of shortcuts. It may look weird at first, but you’ll get used to it.

Compared to Java, Ruby is still very young, but being young, it is able to learn from the mistakes made by its predecessors Java and other languages. And coming from different perspectives to improve developers productivity, and with higher customer expectation each day for each dollar spent, especially in Asia, Ruby may be God sent. I’ve yet to further explore Ruby and to complete the other half of the book; there could be some quirks, but I hope there won’t be many.