Bruno Miranda's Notebook

Personal Blog about Ruby on Rails, XHTML, CSS, and Design

Since arriving in Portland, OR this past wednesday the weather has cleared up. Today, Saturday it is mighty sunny and 65 degrees outside. The city is beautiful, the light rail (tram) transportation service is awesome, if it wasn’t for the gloomy weather almost all year around I would seriously consider moving here.

Thursday was tutorial day, the meta-programming tutorial by Neil Ford was great, really interesting concepts. The afternoon tutorial of my choice was average, so much so that I already forgot what it was about.


Photo by: Duncan Davidson

Joel Spolsky opening keynote on Friday was great, he talked about writing software that makes people happy. “Blue Chip” software, really motivating. Friday night David Heinemeier Hansson spoke on the Rails Community surplus and how he feels it will not last forever. He adviced the community to use it wisely: “Don’t spend the surplus in hookers and fur coats.”

So far there have been many mentions to Tasty Planner in numerous talks. Josh’s talk about Building a web app in 48 hours happens tomorrow at 10:45am.

The talks have been much better than last years. As the community grows, it also matures and topics seem more advanced. Jeremy Kemper’s talk was great, showcasing what’s new in 2.1. Optimizing Rails by Michael Koziarski was full of goodies and the panel by Blaine Cook, Bradley Taylor, Ezra Zygmuntowicz, Jim Meyer and Kevin Lawyer brought to the surface interesting points about scalability topics (I apologize about the crappy sound).


Scaling Rails - RailsConf 2008 from Bruno Miranda on Vimeo.

We paid a much deserved visit to Henry’s 12th St Brewery and Dough Fir, if you have not been to those places, you haven’t really been to Portland.

I am looking forward to watching Kent Beck’s keynote tonight as I have recently finished reading his great book: Extreme Programming Explained.

Here are some iPhone Conference Pictures.

Conference meals have been great. Attendance seems to be the a record, approximately 2000 I heard. Wifi is below average but stable, the community is great and seems to be more united than ever, the message board has about a million job offer notes as shown in the pics below:

More to come soon.

I can imagine you have seen lots of posts about this subject. Well here is another one. For anyone who has used script/generate rspec_scaffold in the past knows that it generates a lot of specs for your controller and views.

Here is a list of issues I have with the auto-generated specs. They are very wordy. Almost 50 lines for the controller update action. Over 60 lines for testing routes. The majority of testing performed is related to testing the framework’s functionality, which in my opinion is not necessary. Another problem I have with it is the fact that as soon as you start nesting resources most of your specs will fail. You will have to adjust them to understand the new nesting setup you have going on. That is just too much busy work if you ask me. You end up spending a bunch of time chasing failing specs, and your code to test ratio will besomewhat inaccurate at the end.

So what should you spec? Well let’s start with the controller. I generally use rspec_controller instead of scaffold since it does not generate an ocean of framework specs. All I really test in the controller are new methods I write, for example, a search or order method. Important sidenote: if a controller method is broken, write a spec for it and watch it fail, when you fix it and the spec passes you will know if that method ever goes belly up again.

The views can also be a pain. The rspec scaffold creates a ton of view specs based on the scaffolded view, which is generally really unlike the real view you will end up with in your app. I generally stay away from those puppies and only write view specs for important components I’d expect the view to either have or not have such as, admin links.

If you follow the skinny controller fat model golden rule, your model should contain most of the application logic. This is where your specs will rule. Make sure you have high coverage for all methods in your model. Hopefully they will do most of the heavy lifting; these should be well spec’out.

Really spend the time to spec your helpers. They will most likely be used numerous times in your application and this will save you a ton of time otherwise spent chasing bugs.

In closing, check you test coverage with rcov.

gem install rcov
rake spec:rcov

This will generate a coverage report in /coverage.

You can also run rake stats to get your code to test ratio. If you follow the above guidelines, you should expect to see something close to 1:1 or less since you are not spec’ing controller/routes/views as aggressively as rpec_scaffold does.

Comments: 0 (view/add your own) Tags: Rails, rSpec

As many of you are doing, I moved a couple of my source repositories to GitHub. At the moment I have one private and one public.

Here is a simple guide to deploy using vlad. You will need a condig/deploy.rb file:


set :application, "appname"
set :domain,      "domain.com"
set :deploy_to,   "/var/rails/#{application}"
set :scm,         "git"

# use the following with a private repos
# set :repository,  "git@github.com:user/appname.git"

# use the following with a public repos
# set :repository,   "git@github.com:user/appname.git"

set :mongrel_port, 8070
set :mongrel_servers, 2

namespace :vlad do
  desc 'Runs vlad:update, vlad:symlink, vlad:migrate and vlad:start'
  task :deploy => ['vlad:update', 'vlad:symlink', 'vlad:migrate', 'vlad:stop_app', 'vlad:start_app']

  desc 'Symlinks your custom directories'
  remote_task :symlink, :roles => :app do
    run "ln -s #{shared_path}/database.yml #{current_release}/config/database.yml"
  end
end

Possible Gotchas!

If you have Vlad 1.2 installed you will need to add the following to your Rakefile

begin
  require 'vlad'
  Vlad.load :scm => "git"
rescue LoadError
  # do nothing
end

If you run into the following error:

fatal: You must specify an archive format

It is probably because you installed git-core with sudo apt-get install git-core. The reason for that is apt-get install git-core installs a 1.4x version of git on your Ubuntu machine, and a default for git archive was added in a later version. Thankfully the solution is pretty simple. Remove the previously installed version of git-core and install a newer version from source.

sudo apt-get remove git-core
wget http://kernel.org/pub/software/scm/git/git-1.5.5.1.tar.bz2
sudo apt-get build-dep git-core
tar xjf git-1.5.5.1.tar.bz2
cd git-1.5.5.1/
./configure --with-tcltk
make
sudo make install

Use –with-tcltk since there is no need to install GUI on the server.

If you do however want to install GUI for some reason you will need to install gettext and start the the git-core installation from source from the beginning.

sudo apt-get install gettext

Now all you have left is to run setup and then deploy:

rake vlad:setup

and

rake vlad:deploy

Deploying a Private Repos

Make sure you go into the server, generate an ssh key for the deployment user and paste the public key into the Github’s deploy key. You will also need to go into the server and clone the repos so that the ssh key pair is generated. Make sure the public key on the server is owned by the deploy user and the permissions are correct:

chown deploy:deploy id_rsa.pub

and

chmod 600 id_rsa.pub

Let me know if you encounter any issues with the instructions above.

While developing rails applications I find myself starting and stopping mongrel a bunch of times. At Todobebé we have 4 different apps to deal with and sometimes I want to start and stop a couple of them in succession.

I put together a couple of quick aliases in my .bash_profile to help me with that process. All I have to do now is browse to app root, and type

ss
that causes the server to start, brings up a browser window with a new tab and browses to http://0.0.0.0:3000.

I have also create ss1 and ss2, you can have as many as you would. Here is how I did it.

alias ss="sleep 3 && open /Applications/Safari.app 
http://0.0.0.0:3000 & script/server -p 3000"

alias ss1="sleep 3 && open /Applications/Safari.app 
http://0.0.0.0:3001 & script/server -p 3001"

alias ss2="sleep 3 && open /Applications/Safari.app 
http://0.0.0.0:3002 & script/server -p 3002"
Comments: 0 (view/add your own) Tags: Rails, shell