[LRUG] To debug the impossible bug

Tom Stuart tom at therye.org
Wed Aug 3 05:55:47 PDT 2011


On 3 Aug 2011, at 13:41, Murray Steele wrote:

> However, I might be convinced to change page_description to take an argument and then use a traditional case along the lines of:
> 
> def page_description(for_object)
>   case for_object
>   when Question then for_object.background
>   when User
>     if for_object.bio.present?
>        for_object.bio
>     elsif for_object.sports.present?
>        for_object.sports_list
>     elsif !for_object.new_record?
>        "Tribesports user: #{for_object.short_name}"
>     end
>   [SNIP OTHER CONDITIONS]
>   when nil
>     raise "LOL, WTF, ZOMG!!!!1111"
>   end
> end
> 
> Although this is arguably harder to parse, so maybe not.
> 
> Muz

Since we're all bamboozled by the bug and would rather discuss code style... ;)

My preference is to favour polymorphism and have all the objects which a page can be about, implement a method providing that description, something like:

class Question
  def description_for_page
    self.background
  end
end

class User
  def description_for_page
    if self.bio.present?
      self.bio
    elsif self.sports.present?
      self.sports_list
    elsif !self.new_record?
      "Tribesports user: #{for_object.short_name}"
    else
      nil
    end
  end
end

# ... etc for other page subjects

module LayoutHelper
  def page_description(subject)
    description = subject.description_for_page || "Tribesports: Connect with sports people who share your interests & create your complete sports profile. We want to inspire you to get more active and improve at your sports."
  strip_tags(description)  
  end
end

To avoid doing the switching on what the 'subject' argument should be based on the presence of instance variables, controllers could implement a before_filter or the like to set a subject instance variable.

Cheers,

Tom


More information about the Chat mailing list