class RecipeDecorator < BaseDecorator

  NAME_CUTOFF = 35

  def short_name
    if wrapped.name.length >= NAME_CUTOFF
      wrapped.name[0...NAME_CUTOFF] + '...'
    else
      wrapped.name
    end
  end

  def source_markup
    uri = begin
            URI.parse(self.source)
          rescue URI::InvalidURIError
            nil
          end

    if uri.is_a? URI::HTTP
      h.link_to(uri.host, uri.to_s)
    else
      h.content_tag('span', self.source)
    end
  end

  def average_rating
    @average_rating ||= (Log.for_recipe(wrapped).for_user(self.user).where('rating IS NOT NULL').average(:rating) || 0)
  end

end