Category Archives: Ruby on Rails

Migrating RSpec to Mocha

I have been using the builtin RSpec mocking framework for a couple of years and haven’t had any issues with it. The one feature I have been missing though is the any_instance method of the Mocha framework. What this method lets you do is mock a method on all instances of a given class. This [...]

PostgreSQL Unreliable Default Sort Order and Random Rails Test Failures

We have been struggling with intermittent Rails test failures for quite some time. There were times when we blamed our search engine Ferret, sometimes we blamed database interference between tests, and sometimes higher powers. Lately we have realized that all this time the ghost haunting us might have been the unreliable PostgreSQL default sort ordering. [...]

Rails 2.3.2 Bug - Sending Multipart Email Breaks When Current Directory is not RAILS_ROOT

We had the debugging session of the year here at Newsdesk the other day and we thought it would be prudent to warn other people about it. It turns out that ActionMailer in Rails 2.3.2 fails in sending multipart emails when the current working directory is not Rails.root. This happened to us when invoking the [...]

Enabling/Disabling the Rails Cache

We needed a way to globally enable/disable the Rails cache so we came up with this patch:

By default ActionController will use the Rails.cache (RAILS_CACHE) object configured by config.cache_store for its fragment caching (action caching is an around filter that utilizes the fragment cache). However, it is possible to configure a different cache store via ActionController::Base.cache_store. [...]

Announcing Cachable Model - A New Plugin For Caching Your Rails Models

When you scale a Rails website the database layer tends to become the bottleneck sooner or later as it’s more difficult to scale horizontally with hardware than the Ruby/web server layer. At MyNewsdesk we have started looking at database caching in the model layer to offload our PostgreSQL database. Unfortunately caching does no longer seem [...]

Keep Your I18n Translation Files Tidy

I added the Rake task translate:remove_obsolete_keys that lets you remove all obsolete translations from your YAML files. How is a translation rendered obsolete? Suppose you are translating from english to a number of other languages. Over time you may end up removing a number of I18n keys from the english translation file as your application [...]

Configuring Exception Handling in RSpec and Controller Tests

The ActionController::Rescue module in Rails defines a number of exceptions that will trigger a 404 response, namely ActionController::RoutingError, ActionController::UnknownAction, and ActiveRecord::RecordNotFound. However if you trigger one of those exceptions in a controller test (Test::Unit or Rspec) then rescue and display of the 404 page will not happen and instead the exception will just be propagated. [...]

acts_as_bitfield Plugin now Radio Button Compatible

I’ve patched the acts_as_bitfield plugin to recognize “true” as true so that now instead of having to write:
<%= f.radio_button(:email_activated, true, :checked => (”checked” if @item.email_activated?)) %> Yes
<%= f.radio_button(:email_activated, false, :checked => (”checked” unless @item.email_activated?)) %> No
you can omit the checked argument just like with ActiveRecord boolean attributes:
<%= f.radio_button(:email_activated, true) %> Yes

<%= f.radio_button(:email_activated, false) %> No

Rails I18n and 404/500 Error Pages

If you have a multilingual site it makes sense for your 404 and 500 error pages to be multilingual as well. Rails supports multilinguality by serving up error pages with a path like public/500.sv.html, where sv is the locale. In the case where Rails itself is fundamentally broken or fails to respond your webserver will [...]

Simplest bit.ly API implementation possible?

To use an URL shortener in your project, I would strongly recommend bit.ly. It let you set a custom URL (name) and track traffic, clicks and conversations on micro blogs. But best of all, it should be super-fast and super-stable, since Twitter recently chose bit.ly as their default URL shortening service.
Using the bit.ly REST API [...]