Testing an abstract controller (like ApplicationController)

Posted by joakimk, Thu Jan 28 15:28:00 UTC 2010

This is one way to test before_filters in your ApplicationController or other abstract controllers. Make sure you don’t miss the “after” part!

One thing to keep in mind though is that re-loading the routes can take some serious time (6 seconds in the app Im working on). I’d like to find a better way of testing this.

require File.dirname(__FILE__) + '/../../spec_helper'

describe ApplicationController do

  class TestController < ApplicationController
    def index
    end
  end

  controller_name :test

  before :each do
    ActionController::Routing::Routes.draw do |map| 
      map.resources :test
    end
  end

  after :all do
    load(RAILS_ROOT + "/config/routes.rb")
  end

  it "should do something" do
    get :index
    # assert stuff
  end

end

0 comments | Filed Under: | Tags:

How to check if a file exists on a remote server (HEAD request)

Posted by joakimk, Thu May 21 14:43:00 UTC 2009

Here is some code to check if a file exists on a remote server by using a HEAD request. It can optionally check if the content type is what you expected (if you for example want to only allow “audio/mpeg” links). Note that this particular implementation only works with http urls.

require 'net/http'

def exists?(file)
  begin
    # Support for both good and bad URI's
    uri = URI.parse(URI.escape(URI.unescape(file)))
    response = nil
     Net::HTTP.start(uri.host, uri.port) {|http|
       response = http.head(uri.path)
     }
     # .. response.content_type == "audio/mpeg"
     response.code == "200"
  rescue
    false
  end
end

puts exists?('http://djspike.se/files/albums/6/31/DJ_SPIKE%20-%20Tight%20(demo).mp3')

I also found a plugin with similar goals but that actually downloads the entire target file instead of only getting the header (not a good idea when dealing with media files): http://www.igvita.com/2006/09/07/validating-url-in-ruby-on-rails/

0 comments | Filed Under: | Tags:

How to disable Globalize's ActiveRecord logging

Posted by joakimk, Wed May 20 09:56:00 UTC 2009

Here is how you disable the sometimes excessive logging that Globalize does:
# Disables Globalize's ActiveRecord logging
module Globalize
 class DbViewTranslator
   alias_method :orig_fetch_view_translation, :fetch_view_translation   
   def fetch_view_translation(key, language, idx, namespace = nil, original_caller = nil)
     ActiveRecord::Base.silence do
       orig_fetch_view_translation(key, language, idx, namespace, original_caller)
     end
   end
 end
end

This tip is originally from http://www.artweb-design.de/2007/9/16/disable-globalize-viewtranslation-sql-logging, but I had to adapt it a bit to work with my globalize version.

If this does not work with your version of globalize, then just check how it’s implemented in “lib/globalize/localization/db_view_translator.rb” and adapt the patch.

0 comments | Filed Under: | Tags:

Using Ruby EE in development

Posted by joakimk, Tue May 19 08:43:00 UTC 2009

It’s common knowledge that using Ruby Enterprise Edition on your server is a good idea, but how many of us use it on our development boxes?

I recently started using Ruby EE in my development environment and it has decreased the time it takes to run my specs with about 20% as compared to Ruby 1.8.6.

0 comments | Filed Under: | Tags:

Installing mysql and the mysql gem on Mac OS X Leopard

Posted by joakimk, Fri May 15 10:52:00 UTC 2009

I had some trouble getting the mysql gem to work in Leopard on my new macbook and it turned out that I had installed the 64bit version of mysql and even though the mysql gem compiles against that, it does not work.

To begin with I got alerted to this by the warning message:

DEPRECATION WARNING: You’re using the Ruby-based MySQL library that ships with Rails…

The kinds of errors you might see when using the 64 bit version of mysql is:

LoadError: Failed to lookup Init function /usr/lib/ruby-ee/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle

How to install a version that works:

1) Get the 32 bit x86 version from http://dev.mysql.com/downloads/mysql/5.0.html#macosx-dmg and install.

3) Make sure you got it installed propertly (open a terminal and type mysql -v).

4)

env ARCHFLAGS="-arch i386" sudo gem install mysql --
--with-mysql-config=/usr/local/mysql/bin/mysql_config

5) Done :)

0 comments | Filed Under: | Tags:

Script to show CruiseControl status with lava lamps

Posted by joakimk, Fri Apr 03 18:11:00 UTC 2009


We've been using this script at work to show the build status using lava lamps =).

To control the lamps we use a TellStick which is a transmitter that plugs into USB and lets us control wireless remote switches.

http://gist.github.com/89810:

0 comments | Filed Under: | Tags:

Acts as state machine not saving to the database?

Posted by joakimk, Tue Jan 20 18:18:00 UTC 2009

Just wasted a bit of time figuring out why my tests ran just fine, while trying to enter a state when actually using the app failed. Turns out that because I do not touch the database in my state machine specs I had no chance of discovering that I had written “aasm_event :foo! do” instead of “aasm_event :foo do” (see that extra “!”?)... D’oh. Hope this can be of help to anyone else that accidentally adds an ! to a aasm_event :).

0 comments | Filed Under: | Tags:

Increasing the maximum build log size for CruiseControl.rb

Posted by joakimk, Thu Jan 15 18:12:00 UTC 2009

I used the following to allow logs larger than 100KB in CruiseControl.rb:

 # Number of times more logging than the default 
 MORE_LOGGING = 5

 Build.class_eval do

   def contents_for_display(file)
     return '' unless File.file?(file) && File.readable?(file)
     if File.size(file) < 100 * 1024 * MORE_LOGGING
       File.read(file)
     else
       contents = File.read(file, 100 * MORE_LOGGING)
       "#{file} is over #{100 * MORE_LOGGING} kbytes - too big to
        display in the dashboard, output is truncated\n\n\n#{contents}"
     end
   end

 end

0 comments | Filed Under: | Tags:

Temporaraly disable Kernel.warn (stderr)

Posted by joakimk, Thu Dec 18 14:02:00 UTC 2008

If you call some code that print warnings to stderr (like Kernel.warn) that can be safetly ignored, you can use this trick to temporaraly turn off stderr:

  $stderr = File.open('/dev/null', 'w')
  code_that_prints_to_stderr
  $stderr = STDERR

Or you cound wrap it in a block:

  def disable_warnings
    $stderr = File.open('/dev/null', 'w')
    yield
    $stderr = STDERR
  end

  disable_warnings do
    code_that_prints_to_stderr
  end

Update: Just learned that if you’re in the context of a rails app you can do:

  silence_warnings do
    code_that_prints_to_stderr
  end

0 comments | Filed Under: | Tags: