Refactoring views with Ruby on Rails’ ActiveSupport helpers

Scott Matthewman
6 min readMar 8, 2018
You have some great support when running on Rails. Original photo by Rucksack Magazine on Unsplash

Ruby on Rails (strictly speaking, the ActiveSupport gem which is installed as a prerequisite for all Rails projects, but which can be installed and used independently of Rails) has some pretty useful features built in.

One such feature is something that this week helped me rip out some tortuous code that was introducing a subtle HTML rendering bug. It’s something that only surfaced when we sent affected emails to specific mail clients, but it’s always nice to stop bugs occurring, however few people may have been affected.

The scenario

Each of our events can have many sponsors. There are multiple levels of sponsorship available, each of which is represented in the database as a numeric value where 1 is the highest level of sponsorship, followed by level 2, level 3, and so on. For simplicity’s sake, we’ll focus on levels up to 4 in this worked example.

At the end of emails concerning a sponsored event, a series of sponsor logos is included. The rules for implementing this are:

  • Each level of sponsorship is separated from the level below by a separating rule
  • The maximum number of sponsor logos per line is the same as the level number — 1 per row for level 1, 2 per row for level 2, etc. up to level 4. From level 5…

--

--

Scott Matthewman

Scott is a software developer during the day and a theatre critic & director of an evening. Which is the worst superhero identity ever.