I have made my pledge for Firefox 3 Download Day.
Running Autotest with Rspec gem
Recently I have decided to use the gem version of rspec instead of the plugin version. When I tried run autotest within the project root folder I got the following error.
script/spec:3:in `require': no such file to load spec (LoadError)
from script/spec:3
The error was caused by line 3 of file script/spec.
1 #!/usr/bin/env ruby
2 $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../vendor/plugins/rspec/lib"))
3 require 'spec'
4 exit ::Spec::Runner::CommandLine.run(::Spec::Runner::OptionParser.parse(ARGV, STDERR, STDOUT))
It’s trying to load spec file from the project plugins directory which is not available. To fix this problem just change the second line of the code to:
1 #!/usr/bin/env ruby
2 require 'rubygems'
3 require 'spec'
4 exit ::Spec::Runner::CommandLine.run(::Spec::Runner::OptionParser.parse(ARGV, STDERR, STDOUT))
Then start autotest from the project root directory and it should now run without errors.
Wrong argument error when running Rails 1.1.6 with Ruby 1.8.6
If you encounter the following error when you start your rails version 1.1.6 application and you are using ruby 1.8.6.
ArgumentError (wrong number of arguments (1 for 0)): /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/vendor/mysql.rb:551:in `initialize' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/vendor/mysql.rb:551:in `new' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/vendor/mysql.rb:551:in `scramble41' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/vendor/mysql.rb:141:in `real_connect' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/mysql_adapter.rb:330:in `connect' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/mysql_adapter.rb:87:in `initialize'
it’s actually due to changes in Digest initialization method. The new method no longer supports instantiation with parameters. The offending code is in
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/vendor/mysql.rb at line 551 within method 'scramble41(password, message)'
s1 = Digest::SHA1.new(password).digest
s2 = Digest::SHA1.new(s1).digest
x = Digest::SHA1.(message + s2).digest
To resolve, just change the code to the following
s1 = Digest::SHA1.digest(password)
s2 = Digest::SHA1.digest(s1)
x = Digest::SHA1.digest(message + s2)
Alternatively you can upgrade your rails version to 1.2.x.
Building a Rails application - Part 1 Getting Started
I am going to write a series of post as I embark on my journey to create my own web 2.0 start up. I will not disclose what I’m working on at this moment, but I will write as much as I can on the tools, libraries and methods I will be using. Where possible I’ll list down the steps and my though process. I hope this can serve as a reference for anyone who wants to build his/her own web application using Rails.
Getting started
-
Domain name
This is one of the hardest part. To find a good domain name is difficult nowadays. Instant Domain Search is a very nice tool for searching domain. Lucky for me, I’m able to get hold of a good domain name for my app. (I will disclose my domain when it’s ready, in the mean time stay tuned) -
Hardware and OS
I will be using my MacBook which is still running on Tiger at the moment. I would not be upgrading to Leopard in the near future and I think Tiger is good enough to do my work. -
Text editor
I have been hacking in TextMate for some time and I love it. It’s shipped with Ruby and Rails bundles. Go ahead and download a trial version and give a try (only for Mac). If you are already hacking in TextMate, you may want to download the Textmate Vibrant Ink Theme and Prototype Bundle
Ajax autocomplete highlighting
Autocomplete text field is a very useful feature to speed up form completion and help minimize errors. It’s even better if you can highlight the keywords within the dropdown list to help user search for the right choice. Here’s how to implement keyword highlighting in rails.

1. First add the necessary javascripts to make the text field into an autocomplete text field.
form
<%= text_field_tag 'country', '', :id => 'country' %>
<div id="country_autocomplete" style="display:none"></div>
<script type="text/javascript">
//<![CDATA[
var country_auto_completer = new Ajax.Autocompleter('country', 'country_auto_complete',
'/countries/auto_complete_for_country', frequency:0.2})
//]]>
</script>
2. Then in the controller add in the method to find the list of items base on the keywords and assign the keyword to an instance variable (this is needed for the highlighting).
controllerdef auto_complete_for_country @keyword = params[:country] @countries = Country.find(:all, :conditions => ['name like ?', "%params[:country]%"]) render(:partial => 'auto_complete') end
3. In the view, create the highlighting effect by replacing all keywords within the country names with ‘keyword’ using the ‘strong’ tag.
view (_auto_complete.rhtml)
<ul>
<% for country in @countries do -%>
<li><%=h(country.name).gsub(Regexp.new("(#{@keyword})", true), '<strong>\1</strong>') %></li>
</ul>
Installing Git on Mac OS X
Recently there’s a trend in the rails community switching from SVN to Git. If you want to give that a try and you are using a Mac, the easiest way to install Git is using macports.
sudo port install git-core
Code Xtreme Apps Part 2
The competition is over and I’m impressed what my team, Jason and JT has developed within the 24hrs code jamming. You can try out the application here. The objective of the application is to allow anyone to share their itineraries and let anyone going to any cities to plan for their own itinerary based on the information they get from our site. It’s simple and easy to use. Currently there are limited data and you can search for itineraries for Singapore, 1or 2 days with tags ‘arts’. Overall it’s a good experience and we were lucky to be short listed for presentation today. I do hope we can win but even if we don’t I would still like to improve on it and launch it.
Code Xtreme Apps
I will be heading for my first coding competition (Code Xtreme Apps) tomorrow at SMU. It’s a 24hrs coding compeition and it’s the first of it’s kind in Singapore. I’m quite excited about it and looking forward to it. There are total of 73 teams and I believe it will be a tough fight with some of the best rails developer from Singapore and around the regions. Well I do hope I can win but mostly I’m taking it as more of an experience and fun kind of feeling to the competition.
A document database server, accessible via a RESTful JSON API.
Couchdb seems like an very interesting alternative data storage server. What interests me is it uses a RESTful JSON api to retrieve and update data. There are no database schema and changes to the data structure can be make without effecting previous records. It uses Javascript to build a view model. Moreover the database is implemented in Erlang, the next language I’m going to pick up.
Convention over configuration - the smarter choice
I was debugging a Java legacy system and found out that the error was due to configuration. Yeah.. I forgot to change the configuration that maps to the database and I cost me 1 day to debug!. I guess I’m too used to rails way of doing things now. Convention over configuration, definitely the smarter choice!
Using raspell with your own custom dictionary
A good search engine should be smart enough to detect spelling errors and provide suggestions. For example if you misspelled a word when using Google search, it will ask Do you mean:.... It will be cool to have such feature for our own search engine. So I started to explore possible solutions what works will rails and came upon this blog post by Evan Weaver. He has implemented a ruby gem raspell to interface with Aspell. That sounds like a good solution and I decided to check it out.
1. Installing Aspell
As mentioned in Evan’s post to install Aspell just type the following command from terminal:
sudo port install aspell aspell-dict-en
This will install both the aspell library and aspell English dictionary.
2. Install raspell
This is straight forward.
sudo gem install raspell --with-opt-dir=/opt/local
3. Setting up your own dictionary
For my case I needed aspell to check against my own custom dictionary. I create a text file containing the list of words with each word on a new line save it as wordlist. e.g.
John
Simon
Mae
Tim
Louis
Then run it with the following command.
aspell --lang=en create master ./en_SG-name.rws < wordlist
Note that for the language option I’m using ‘en’ which is the standard code for English. If you are generating a word list in other languages you will have to change lang option accordingly. The name of the output file is important because Aspell only pick up dictionary file in a certain naming format. The dictionary file should start with the language code then follow by a country code which is optional. For my case I have chosen SG because I do not want to override the default en dictionaries from Aspell. The ‘name’ after the ’-’ is actually the jargon. So if you are generating a word list of street names in US you might want to name you dictionary as en_US-street.rws. Once you have generated the dictionary you will have to place it in Aspell default dictionary directory.
sudo mv en_SG-name.rws /opt/local/share/aspell/
Because Aspell will only look for dictionaries with .multi file extension in the default directory. I’ll need to create a en_SG-name.multi file for my new dictionary
sudo vi /opt/local/share/aspell/en_SG-name.multi
and add the following line.
add en_SG-name.rws
You can add multiple dictionary to this file. If you needed the common English dictionary together with your custom one. Just add it accordingly.
4. Using your own dictionary in ruby.
Finally to use the dictionary I have just created.
require 'rubygems'
require 'raspell'
sp = Aspell.new('en_SG', 'name')
sp.suggest('Mea')
=> ["Mae"]
The first parameter is the language part of the file name and the second is the jargon. So if you have named your dictionary as en_US-street.multi then you will need to initialize it with
sp = Aspell.new('en_US', 'street')
Setting up Rspec, Autotest and Growl on Mac OS X
What follows are the instructions to install the software to setup your own if you like to try out.
Installing Growl
- Download the dmg package from Growl
- Open the disk image, and double click in the Growl.prefPane icon.
- Next is to install growlnotify . Double click on the Growl disk image again if you have close it and execute the following commands from a terminal
cd /Volumes/Growl/Extras/growlnotify
sudo ./install.sh
cd
hdiutil detach /Volumes/Growl
Installing ZenTest
ZenTest provides 4 different tools and 1 library: zentest, unit_diff, autotest, multiruby, and Test::Rails. For our purpose we will be using autotest. The latest version of ZenTest has added rspec support and it’ll auto detect rspec and run tests in the background whenever there’s any changes to the specs.
Installing ZenTest is easy just run the following command:
sudo gem install ZenTest
Installing Rspec
To test rails with rspec you will need to install rspec and rspec_on_rails plugins . Run the following command from your project root directory.
script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec
script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec_on_rails
After you have installed the plugins you will need to run following command once to bootstrap your Rails app with RSpec.
script/generate rspec
Customize Autotest
The last step is to customize Autotest. Download the following 2 images and save them under ~/.autotest_images (create this directory if it does not exist). You can also create your own images if you want.
Open your favorite editor and copy and paste the code, then save the file as .autotest under your user home directory.
module Autotest::Growl
def self.growl title, msg, img, pri=0, sticky=""
system "growlnotify -n autotest --image #{img} -p #{pri} -m #{msg.inspect} #{title} #{sticky}"
end
Autotest.add_hook :ran_command do |at|
results = [at.results].flatten.join("\n")
output = results.slice(/(\d+)\s+examples?,\s*(\d+)\s+failures?(,\s*(\d+)\s+not implemented)?/)
if output
if $~[2].to_i > 0
growl "FAIL", "#{output}", "~/.autotest_images/fail.png", 2
else
growl "Pass", "#{output}", "~/.autotest_images/pass.png"
end
end
end
end
Configure Growl
Finally you can configure growl to with different display style via System Preference -> Growl. Try out all the different display style to see which one you prefer. Personally I like “Music Video”.
Once you have setup all the above. Run autotest under your project root directory and you will see Growl in action. Have fun!!
Internet Wasteland
Check out this geek song Internet Wasteland by Eddy Boston. It has Ruby on Rails in the lyrics.
Sending emails using GMail smtp with ActionMailer
If you have been following our blog you should have read the post Swiss Amry Knife 2.0 . We have recently switch our mail server to Google Apps. This is nice but that also means we have to change our ActionMailer server settings to GMail’s smtp server and this pose a problem. Because default ActionMailer does not support SSL authentication which is required by GMail. I tried searching for a solution and lucky enough I managed to came across this post and got to work! Check out the link if you are facing the same problem.

