Saturday, October 29, 2011

Getting Growl-like notifications on Ubuntu 11.10 in Rails 3.1

I'm doing Michael Hartl's Rails Tutorial book this weekend and I wanted to get the notifications right. Took me a while to get it to work. Since this is a fresh install of Ubuntu 11.10 Oneiric Ocelot I'm running into a few issues. I'm running ruby 1.9.2 using rvm since Ubuntu only included 1.9.1. I'm also using the Gnome 3 Shell as I prefer it to Unity.

1) The first issue was that the execjs gem that is installed by default by Rails 3.1 requires the Spider Monkey Javascript engine to be installed in Ubuntu. (sudo apt-get install libmozjs185 libmozjs185-dev or use Synaptic and search for "spidermonkey" which is what I did). Or you can comment it out of your Gemfile as it doesn't seem to affect anything in the tutorial.

2) The second issue was that I wanted the nice Growl-like notifications for autotest. The instructions on the automate everything website got me there but don't install the gem 'redgreen' it's buggy. Here is the process I followed:

First you need to
sudo apt-get install libnotify-bin
if you don't already have it.

Second install a few gems:
gem install autotest autotest-notification autotest-rails autotest-standalone
Then issue this command
an-install 

Then modify the .autotest file with the code from automate everything. First I copied the existing autotest file to .autotest.bak and created a new one with this code:


#!/usr/bin/env ruby
require 'autotest/timestamp'
module Autotest::GnomeNotify
  def self.notify title, msg, img
    system "notify-send '#{title}' '#{msg}' -i #{img} -t 3000"
  end
  Autotest.add_hook :ran_command do |at|
    image_root = "~/.autotest_images"
    results = [at.results].flatten.join("\n")
    results.gsub!(/\\e\[\d+m/,'')
    output = results.slice(/(\d+)\sexamples?,\s(\d+)\sfailures?(,\s(\d+)\spending?|)/)
    full_sentence, green, failures, garbage, pending = $~.to_a.map(&:to_i)
    if output
      if failures > 0
        notify "FAIL", "#{output}", "#{image_root}/fail.png"
      elsif pending > 0
        notify "Pending", "#{output}", "#{image_root}/pending.png"
      else
        notify "Pass", "#{output}", "#{image_root}/pass.png"
      end
    end
  end
end


I actually like the stock images of autotest-notification so I just copied them from their directory here: ~/.rvm/gems/ruby-1.9.2-p290/gems/autotest-notification-2.3.3/images
to the ~./autotest_images/ folder. [Note: I mean ~/.autotest_images/ ]

And it worked. Now I want to figure out if the normal .autotest file would have worked.

# ~.autotest
require 'rubygems'
require 'autotest_notification'
SPEAKING = false
DOOM_EDITION = false
BUUF = false
PENDING = false
STICKY = false
SUCCESS_SOUND = ''
FAILURE_SOUND = ''

It seems to be able to. Not sure what it does though. If you want to see something fun dig in to the images directory above for DOOM.

Gnome 3 shell Issues: All the notifications seem to remain present on the bottom bar so you can see a sort of notification history; to remove them you have to right click on them.


There is a way to make this notifications transitory which when you're running autotest can be more useful. Go to your ~/.autotest file that you've configured for the Rails tutorial. And add "--hint=int:transient:1" to the notify-send command like below.


3) The third issue was an error regarding the "has_selector" command used on the Static Pages Chapter, this command seems to depend on the gem "webrat" so: gem install webrat 

and change the Gem file section of test to include it:


group :test do
  gem 'rspec'
  gem 'webrat'
end


And that fixed it.

4) The directory for the css style sheets is no longer /public/stylesheets/ but /app/assets/stylesheets/ so when putting blueprint it should be under /app/assets/stylesheets/blueprint.

5) In the Class section apparently there is a class called Word defined already so I think I broke it when I redefined it, trying to do Hartl's Word example.
I got this error: TypeError: superclass mismatch for class Word

which is pretty nicely explained in this blog. And it means that Word is elsewhere defined (probably further back in the example).
<

Another cool thing I just noticed is that String#reverse, that only worked for ASCII under Rails 1.8.7 now under 1.9.2p290 works for UTF-8 characters as well so the weird code you see in the screenshot is unnecessary.

3 comments:

  1. To solve the problem of notifications that do not disappear just change line 5 of ~/.autotest from

    system "notify-send '#{title}' '#{msg}' -i #{img} -t 3000"

    to

    system "notify-send --hint=int:transient:1 '#{title}' '#{msg}' -i #{img} -t 3000"

    It worked for me.

    ReplyDelete
  2. Thanks Dan, I updated the blog to reflect this, I actually found it on another blog post about fedora, wish I'd just red your comment it would've been quicker!! :)

    ReplyDelete
  3. I just got the funniest message apparently there is a typo above: it's not ~./autotest_images but ~/.autotest_images, darn decimal point.

    ReplyDelete