Ruby/Rails Features and Patterns

Flashcard 6 of 6

Do you know a pattern/refactoring that might clean up this class? In particular, how might we handle the fact that some JobSites don't have a contact?

class JobSite
  attr_reader :contact

  def initialize(location, contact)
    @location = location
    @contact = contact
  end

  def contact_name
    if contact
      contact.name
    else
      'no name'
    end
  end

  def contact_phone
    if contact
      contact.phone
    else
      'no phone'
    end
  end

  def email_contact(email_body)
    if contact
      contact.deliver_personalized_email(email_body)
    end
  end
end

Answer:

Reveal Answer