bookmark_borderDelhi ruby enthusiasts meetup.

Love Ruby. Love Rails. Love Beer. Love Food. Do You?

Jon and I are organizing a ruby/rails enthusiasts meetup on Friday, June 2, 8pm at Piccaddily, PVR Plaza, C.P.

We’ll be exchanging views and experiences on Ruby and Rails over beer and food.

All Ruby enthusiasts who are willing to share the bill are welcome to join us.

A rsvp mail to me or jon would be appreciated.
Coz Picaddily gets crowded at weekends at times … and we might need to make a reservation.

bookmark_borderRailsConf Europe 2006…. here i come.

Signed up for RailsConf Europe 2006, London September 14-15. It’s EXPENSIVE as compared to Chicago RailsConf in June. But I think the airfare will even out things for me. And the chicago conference got booked so fast, that I didn’t even get a chance to think seriously about attending it.

The conference is already packed with exciting speakers, including the creator of Rails, David Heinemeier Hansson, Pragmatic Programmer Dave Thomas, best-selling author and passion maven Kathy Sierra, Rails core developers Jamis Buck, Marcel Molina, Jr., Thomas Fuchs, Rails authors and trainers David Alan Black, and Chad Fowler, Rake author Jim Weirich and many more to be announced.

Sound like it’ll be fun …if you would be there too …. ping me and let’s meetup.

bookmark_bordertesting rails application

Have been busy recently comparing Rails 1.1’s integration testing and WATIR and selenium for end-to-end testing a rails application.

The testing scenario is that the webserver is running on a remote Linux box in US, to which we have ssh access.
And we are testing the application from India. So we cannot run the browser on that machine.

We are writing test cases for various parts of the application using each of these tools and then we would compare notes.

A few things however are clear rightaway.

WATIR will test only for IE on windows. Firefox support for watir called firewatir is being built, and is not robust as of now. Also we might need different sets of test scripts for firefox and IE.

Selenium is the easiest to get started in the BrowseBot FITRunner mode; haven’t tried the “driven” mode yet. FIT mode would not allow us to verify values on the screen against those in the db… but then probably those tests should go into the “controller testing”(functional testing) and not be at this level. When we want to test against db for multiple controllers then rails’ integration testing is the probably the best way.

Running selenium in “driven” mode will take some research, for which i have not been able to spare time.

WATIR requires ruby and watir gems to be installed on the client computer. Hence if we want to share UI tests with our non-technical client, Selenium is better since it requires no installation.

From the aspect of writing test cases, WATIR is better than selenium’s FIT mode. Selenium test cases need to be written in html. And there is no control flow in the selenium core, though there is a user contributed library for this.

For certain kind of testing, such as against db values and checking values in session, Rails functional tests and integration tests are great.

Will share more experiences on this when we are through with the exercise.

How has your experience with these tools been?

***********************************
Received this profound quotation in my email today.

“The greatest good you can do for another is not just to share your riches, but to reveal to him his own.”
Benjamin Disraeli
1804-1881, Former British Prime Minister
***********************************

bookmark_borderDRY in Views

Don’t Repeat yourself while creating Views in Rails.

I was confused when I starting working on Rails … but now I apply these rules.

Use Layouts to create page layouts to be shared by all views rendered by a controller.

Use components to create controller actions whose output needs to be used more than once

Use partials to create views, which need to be rendered more than once, in the same action or in different actions. [see collection option to render partial more than once in the same action]

bookmark_borderruby code block and iterators

Code Blocks and Iterators are a very useful feature in Ruby.

An iterator is a method that executes a block of code.

Say we have a method named each


def each(array)
    #do something
end

The block is specified next to the method call, after the last parameter to the method.


names=['alice', 'bob', 'charlie', 'dave']
each(names) { |s|  puts s }

Our block is the code within the { } braces. (mutiple line block can be written between do….end)

We are passing an array names, in the call each(names).
Inside each() we will iterate through this array names.


def each(arr) 
  for i in 0..arr.size-1 
    yield arr[i] 
  end 
end 

All the code block iterator’s magic is done by the yield statement.

The yield statement invokes the code in the block.
The yield statement can also pass parameters to the block . In our example, the array element at position i is passed to the block. The block receives this parameter between vertical bars or pipes. (|s| in our example)

A block may also return value to the yield. The last expression evaluated in the block is passed back as the value of the yield.

Here is the complete listing of our code so far


def each(arr) 
  for i in 0..arr.size-1 
    yield arr[i] 
  end 
end 
names=['alice', 'bob', 'charlie', 'dave']
each(names) { |s| puts s } 

This code will print all the array elements, as yield is called for each array element. Yield in turn invokes the code block passing it the array elements, the code block puts the element.

We could change the block code to print all the array element in upper case or in reverse.


each(names) { |s| puts s.upcase } 
each(names) { |s| puts s.reverse } 

What we have done so far, is actually provided builtin by ruby for various types of collections.

each is a builtin iterator in ruby which yield the elements of the collection.
To print all elements of array names we could simply do


names.each { |name|  puts name }

Similarly there is a find iterator.
To print all names that begin with m, we could use


m_names = names.find { |name| name.index('m') == 0 } 
m_names.each { |name| puts name } 

Iterators help a lot in keeping ruby on rails code compact. I have been using them a lot, ever since i discovered them.

bookmark_borderRails:join table name

I just came across the naming convention for a has_and_belongs_to_many relation’s join table.

If you have tables products and categories, which have a many to many relationship, a product can belong to many categories and a category can contain many products. What would you name the joining table, products_categories or categories_products.

Rails convention says use categories_products.

Rails assumes that a join table is named after the two tables it joins (with the names in alphabetical order). If you use some other name, you would need an additional declaration.

Unless there is a very compelling reason to do so, one should stick with the convention.
Reason: For maximum productivity don’t go against Rails’ philosophy “Convention over configuration”

bookmark_borderRails: Automatic Time Stamping

I tried this with mySQL. Should work with other databases too.

If you have columns named “created_at” or “updated_at” of type datetime in your database table,
Rails will automatically insert the value of now( ), in the column “created_at” when a model corresponding to the table is saved in the database. Similarly it will automatically update the value of the column “updated_at” to the value of now( ), when a model is updated and saved to the database.

“created_on” and “updated_on”, of type date, display the same functionality.

One of the basic Rails philosophy is “Convention over Configuration”, meaning that rails has sensible defaults for a lot of aspects.

If the programmer follows these naming conventions a lot of functionality in the application gets built-in by default.