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 created_at
    wrapped.created_at ? wrapped.created_at.strftime('%D') : ''
  end

  def tag_names
    tags = wrapped.tag_names
    tags.map do |t|
      h.content_tag('span', t, class: 'label label-default')
    end.join(' ').html_safe
  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 step_text
    markdown(wrapped.step_text)
  end

end