Ruby on Rails

Testing Rails 3 scopes revisited

In my previous article I told you about how I like to tests my scope. There was a fair amount of criticism on that post and after considering it all (and hearing Corey Haines’ talk on Arrrrcamp last friday), I’m convinced it’s the wrong path. In essence Rails allows you to create a scope to generate a custom database query for you. Now, if you only test your configuration of Rails (as I did in my previous post), you don’t know if that scope works or not.

Properly testing Rails 3 scopes

The content of this post is no longer correct. Please read this article for details. Testing scopes has always felt a bit weird to me. Normally I’d do something like this: class Post < ActiveRecord::Base scope :published, where(:published => true) scope :latest, order("created_at DESC") end describe Post do context 'scopes' do before(:all) do @first = FactoryGirl.create(:post, :created_at => 1.day.ago, :published => true) @last = FactoryGirl.create(:post, :created_at => 4.day.ago, :published => false) end it "should only return published posts" do Post.

Epic TextMate Theme

Okay, I’ve wanted to make a custom TextMate theme since I first installed the thing on my MacBook in 2006. Today I present you with ‘Epic’. Installation Grab the theme here: EpicBlue.tmTheme.zip (1.5k). Unzip it and open it with TextMate. That’s all. Enjoy!

Rails + MySQL: Case-Sensitive strings in your database

When using Rails + MySQL, you’ll find that normal string (or varchar(255)) fields are case insensitive. This can be quite a nuisance, but it’s easy to resolve. You need to set your table to the utf8_bin collation. By using the binary variant, you’re basically enabling case sensitivity. create_table :posts, :options => 'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin' do |t| t.string :title, :limit => 100 end That’s all. The title field is now case sensitive.

Once and for all: Rails migrations integer :limit option

I literally always have to look up the meaning of :limit in migrations when it comes to integer values. Here’s an overview. Now let’s memorise it (oh, this works for MySQL, other databases may work differently):

BaseApp: a quick start for your Rails App

BaseApp is no longer maintained. There is a very good alternative called bort. For the impatient: http://github.com/ariejan/baseapp Got issues? Feature requests or patches? http://baseapp.lighthouseapp.com/ Every Rails developer has at least once developed an application that needed user authentication and some basic UI features like tabs and a sidebar. Ask yourself now: “how often have you installed and extended the restful_authentication plugin?“. Yes, I have done it quite a few times and everytime I find myself writing the same code over and over again.

Useless Ruby Gems for your pleasure

The past few days I’v taken some time to find out how to create a Ruby Gem. This has been on my to-do list for quite a while, but now I’m able to tick it off. Well, what did I make? The first Gem can also be used as a Ruby on Rails plugin and is called ActsAsGold. If you’ve ever played World of Warcraft, you’ll know how the money system works.

Debian Etch: RMagick LoadError

If you’re on Debian Etch, you may encounter the following error libMagickWand.so.1: cannot open shared object file: No such file or directory - /usr/lib/ruby/gems/1.8/gems/rmagick-2.3.0/lib/RMagick2.so This basically means that the libMagickWand.so.1 file cannot be found. However, it is available on your system. All you need to do to fix it, is tell your box to look in the right place for the file. To fix this issue once and for all, open up /etc/ld.

Ruby on Rails plugin: Throttler

For those of you who have missed it: I’ve released a plugin yesterday that allows you to throttle your Rails app. Read the original announcement and installation/usage instructions Read how you can put Throttler to good use in your app

Rails: calculated column caching

Sometimes you’re working on a Rails project and you think: “hey! This should be easy!”. Well, most of the time it is. I’m working on a project that allows people to rate objects (what they really are doesn’t matter at all). I’m using the acts_as_rateable plugin which creates an extra database table containing all ratings. I also have a table with my objects. Using the plugin I’m now able to do the following: