Last week, I started a Rails3 project and considering I am sucker for TDD. I added the current stable versions of rspec-rails and cucumber-rails to the Gemfile. Now, this is a simple project (and honestly speaking I could have just used Sinatra) for the same, but I wanted to get a Rails3 app up and running.
When I say simple, I mean no “ActiveRecord”. Some might say I am better off using Sinatra with so little codebase to load but like I said it was just to get some hands on experience with Rails3. Developers mostly use rspec-rails and cucumber-rails with activerecord and since I wasn’t going to use activerecord, I hit a few road blocks.
Rspec-rails
First, let’s have a look at changes one needs to do use rspec-rails with activerecord. I am assuming that you have installed rspec in your rails app using the new rails3 generators and you have a spec_helper.rb in your spec folder. Just comment out the following lines to make it work with activerecord:
spec_helper.rb file does have directions to remove these files in the absence of activerecord.
Cucumber-rails
Similarly, cucumber-rails actually relies on the fact that activerecord is always used. When you run rake:cucumber, you will probably see the following errors:
** Invoke cucumber:all (first_time)
** Invoke cucumber:ok (first_time)
rake aborted!
Don't know how to build task 'db:test:prepare'
Reason, because cucumber depends upon the db:test:prepare task to run. But in the absence of activerecord this causes issues since there is no db. I added a patch for the same which checks for the presence of database.yml and takes action accordingly. Here’s the lighthouse ticket and patch. You can monkey patch it in your app by modifying the rake task in lib/tasks/cucumber.rake.
In the end it was a good experience and taught me a bit more about cucumber and rspec.
RVM is no doubt an amazing piece of code. It just makes life so much easier. Ruby 1.9.2 is out. Woo-hoo! and I tried installing it with rvm only to encounter an error. I fixed the error and this post outlines my shenanigans leading to an easy solution.
Mark has done a nice post on rvm with ruby 1.9.2 and rails3. I used it as my starting point and ran the following command in the Terminal.
It downloads the ruby 1.9.2 and errors whilst running make. Here’s the rvm error message:
info: Installing Ruby from source to: /Users/andhapp/.rvm/rubies/ruby-1.9.2-p0
info: /Users/andhapp/.rvm/src/ruby-1.9.2-p0 has already been extracted.
info: Configuring ruby-1.9.2-p0, this may take a while depending on your cpu(s)...
info: Compiling ruby-1.9.2-p0, this may take a while depending on your cpu(s)...
error: Error running 'make ', please check /Users/andhapp/.rvm/log/ruby-1.9.2-p0/make*.log
error: There has been an error while running make. Aborting the installation.
This points at a make error log. Further investigation of the make.error.log file revealed the following error message (only the last 10 lines):
_sdbm.c: In function ‘chkpage’:
_sdbm.c:864: warning: comparison between signed and unsigned
raddrinfo.c: In function ‘nogvl_getaddrinfo’:
raddrinfo.c:140: warning: passing argument 3 of ‘ruby_getaddrinfo__darwin’ discards qualifiers from pointer target type
bytecode.re: In function ‘sycklex_bytecode_utf8’:
bytecode.re:442: warning: implicit conversion shortens 64-bit value into a 32-bit value
rubyext.c: In function ‘mktime_do’:
rubyext.c:278: warning: comparison between signed and unsigned
/Library/Ruby/Gems/1.8/gems/rdoc-2.4.3/lib/rdoc/rdoc.rb:51: warning: already initialized constant GENERATORS
[BUG] cross-thread violation on rb_gc()
ruby 1.8.6 (2009-06-08) [universal-darwin9.0]
make: *** [rdoc] Abort trap
There’s a very strange thing in this error message. For some reason, it is referencing ruby 1.8.6. But, why? Well, I am not sure what causes it but here’s how I fixed it. My default system ruby is version 1.8.6 and that was the current ruby in the context when I ran the rvm command. I used rvm to change the current ruby to 1.9.2-preview1 and then re-ran the command. Volia! It worked.
I was very confused the first time I heard this phrase - “Read Open source code”. I could not really grasp the concept of reading someone else’s code. How can that improve your own programming skills? But, I can safely say that it does help. Two things that I found out recently, both reading some open source code. Here they are:
1. I found this trick used in Paperclip’s code. rescue is basically to catch the exceptions and do the needful but one can use them inline. Let us look at an example:
# Usual Way
begin
1/0
rescue => e
puts "Exception"
end
# Cool way
1/0 rescue "exception"
We all know what the first approach does. The second one is quite simple as well. Execute the code followed by rescue if there is an exception in the code before rescue. Now, I will agree that both approach has it merits but still the second one is much cooler and neater.
2. Passenger is an amazing, cutting-edge library that I usually read through and there is something very interesting I found in the code. There is a library in ruby called Etc that allows one to query stuff like the current user and so on. I had no idea one could do it.
Well, every programmer should read other’s code and his own code from time to time to see what he could improve and where we went wrong.
I wrote about Bundler a couple of months ago. With newer versions of bundler out two of my issues at that time, using engines and issues with time_zone have been resolved. I am currently using bundler 1.0.0.beta.5 with a rails 2.3.8 application. I know I should just move it to Rails3 and yes that is in the pipeline and hopefully very soon.
Just started another blog to talk about programming related stuff using Publisha. And the best thing is it will be on iPhone straight-away. I don’t have to even move my finger.