module Highrise
  module Taggable

    def tags
      unless self.attributes.has_key?("tags")
        person_or_company = self.class.find(id)
        self.attributes["tags"] = person_or_company.attributes.has_key?("tags") ? person_or_company.tags : []
      end
      self.attributes["tags"]
    end

    def tag!(tag_name)
      self.post(:tags, :name => tag_name) unless tag_name.blank?
    end

    def untag!(tag_name)
      to_delete = self.tags.find{|tag| tag.attributes['name'] == tag_name} unless tag_name.blank?
      self.untag_id!(to_delete.attributes['id']) unless to_delete.nil?
    end
    protected
    def untag_id!(tag_id)
      self.delete("tags/#{tag_id}")
    end
  end
end
