# lifted from http://readlist.com/lists/lists.rubyonrails.org/rails/7/38240.html
# originally by Derek Neighbors
# see http://vpim.rubyforge.org/ for more info
controller
# This allows us to get a contact back as html, xml or vcard.
def show
respond_to do |format|
format.html
format.xml { render :xml => @contact.to_xml }
format.vcf do
send_data @contact.to_vcard, :filename => "#{@contact.id}.vcf",
:type => 'text/directory'
end
end
end
model
# Then in the contact model you can add something like:
def to_vcard
card = Vpim::Vcard::Maker.make2 do |maker|
maker.add_name do |name|
name.prefix = prefix unless self.prefix.blank?
name.given = first_name
name.family = last_name
name.suffix = suffix unless self.suffix.blank?
end
if preferred_location
maker.add_addr do |addr|
addr.preferred = true
addr.location = 'work'
addr.street = preferred_location.address_line_1 unless preferred_location.address_line_1.blank?
addr.locality = preferred_location.city unless preferred_location.city.blank?
addr.postalcode = preferred_location.postal_code unless preferred_location.postal_code.blank?
addr.country = preferred_location.country unless preferred_location.country.blank?
end
maker.add_tel(preferred_location.voice_number) do |tel|
tel.location = 'work'
tel.preferred = true
end unless preferred_location.voice_number.blank?
maker.add_tel(preferred_location.fax_number) do |tel|
tel.location = 'work'
tel.capability = 'fax'
end unless preferred_location.fax_number.blank?
maker.add_email(preferred_location.email) do |e|
e.location = 'work'
e.preferred = 'yes'
end unless preferred_location.email.blank?
end
end
card.to_s
end