Behind the Fan Door

the Fandor engineering blog

Archive for October 2011

Naughty callback

leave a comment »

I recently ran into what at first seemed like an odd problem with Rails while adding a simple before_create filter to one of my models. The code looked something like this:

# gummy_bear.rb
class GummyBear

  before_create :set_cheap_sweetener, :if => :need_to_cut_cost?

  private

  def set_cheap_sweetener
    self.high_fructose_corn_syrup = true
    self.sugar_cane = false
  end
end

After adding this before_create filter, my gummy bears would no longer save (ActiveRecord::RecordNotSaved exception).

Read the rest of this entry »

Written by Martin

October 28, 2011 at 22:57

Posted in Rails

I like them lean

leave a comment »

One of my personal objectives here at Fandor is to improve the overall code quality. This is a lengthy process which requires multiple refactoring iterations spread over time, especially considering that the product itself is constantly evolving. My strategy is simple: whenever I’m looking at an area of the product that I haven’t been exposed to yet, I spend some time analyzing the current design and see if there’s room for improvement. Typically if I’m having a hard time understanding the logic or I find the code too convoluted and not easy to read then it’s time for some refactoring. Here’s one example I recently came across.

Read the rest of this entry »

Written by Martin

October 22, 2011 at 17:49

Posted in Rails, Refactoring

Follow

Get every new post delivered to your Inbox.