I'm not testing those stupid views!


Over the course of a Rails project, views that started out very simple can evolve into a complex mix of HTML with complicated nested Ruby conditionals. They become hard to understand, hard to read and hard to work with. Fixing an edge case in those views may involve the painstaking process of writing of an end-to-end test to ensure the bug has been squashed. Those kind of tests have quite an impact on the overall runtime of the test suite, so typically you don’t want to write too many of them. Especially not for edge cases. The more end-to-end tests you have, the harder it becomes to make changes to your views because the tests have all kinds of expectations of them.

Continue Reading →

Don't index the kitchen sink!


I was recently cleaning up unused indexes and I found quite a few. These were mostly part of join models from which a reverse association wasn’t required. Consider the following example:

Continue Reading →

Delegating to Query Objects through ActiveRecord scopes


Query objects are a fairly well known concept in Rails these days. They’re useful to extract complex SQL queries into their own classes, which would otherwise clutter ActiveRecord models. What if you turned a complex scope which is already used all over your application into a query object? Do you have to refactor all of these occurences?

Continue Reading →

Resourceful custom user profile theme


Imagine we’ve got a Rails application where users can view each other’s profiles. We’d like to enable them to make their profile personal and unique by customizing the background and text colors used to display their profile. In nearby the future, there will likely be many more adjustments users can make to their profile theme. So what would be a good approach for this problem that conforms to the Rails way?

Continue Reading →

Don't use before_action to load data


Controller filters are a common sight in Ruby on Rails controllers. The before_filter specifically can be used to halt the request cycle. This is useful to prevent unauthorized access to controller actions, but it’s also very often used to load database records.

Continue Reading →

Callbacks and Dirty Objects in Rails


ActiveModel::Dirty is responsible for tracking the changes to attributes in an active model. This often comes in handy with ActiveRecord callbacks to perform certain actions only if some attribute’s value changed.

Continue Reading →

Conditionally map a single route to different controllers


Sometimes it’s useful to have the same route conditionally map to different controllers. To illustrate this, imagine we have a blog with categories and posts, each of which have a similar URL, /some-category for categories and /some-post for posts. Our routes.rb file looks something like this:

Continue Reading →

Rendering XML with Haml


Haml makes it incredibly easy to render your HTML views. It’s cleaner than HTML because it relies on indentation to define the element tree structure rather than closing tags.

Continue Reading →