Everything’s a nail
In the past I’ve used a jquery plugin for generating links to add events to a calendar, but it recently broke when I upgraded jquery on TroopTrack. So I started banging around trying to get it to work for about an hour before I realized I was being stupid. The jquery library was 1) building a drop down menu and 2) generating links.
You don’t need to use jquery to do that. It’s pretty easy to make drop down menus (twitter bootstrap) and generate links (helper). In fact, since my event details were all being stored in active record, using javascript made it harder than it needs to be.
Anyway, in the event you ever find yourself in my shoes, here’s what I did:
Build the urls
def google_calendar_url(event)
# https://www.google.com/calendar/render?action=TEMPLATE&text=Joe's+40th+Birthday&details=Joe+turns+40+just+this+once&dates=20111212T190000/20111212T200000&location=Gillette+Stadium&sf=true&output=xml
"http://www.google.com/calendar/event?action=TEMPLATE&trp=false" +
"&text=" + event.title +
"&dates=" + event.activity_at.to_s(:ics) +
"/" + event.end_at.to_s(:ics) +
"&location=" + event.location +
"&details=" + event.description
end
def live_calendar_url(event)
"http://calendar.live.com/calendar/calendar.aspx?rru=addevent" +
"&dtstart=" + event.activity_at.to_s(:ics) +
"&dtend=" + event.end_at.to_s(:ics) +
"&summary=" + event.title +
"&location=" + event.location
end
def yahoo_calendar_url(event)
"http://calendar.yahoo.com/?v=60" +
"&TITLE=" + event.title +
"&ST=" + event.activity_at.to_s(:ics) +
"&in_loc=" + event.location +
"&DESC=" + event.description +
"&URL=" + plan_event_url(event) +
"&DUR=" + event.duration
end
With a little help from some friends
The time formats matter, so I created a time formatter in an intializer:
[Time].map do |klass|
klass::DATE_FORMATS[:ics] = lambda { |date| date.strftime("%Y%m%dT%H%M%S")}
end
and Yahoo expects a duration in the HHMM format, which I did by adding a method on my event class:
def duration "%02d" % ((end_at - activity_at)/3600.floor) + "%02d" % (((end_at - activity_at)/60).modulo(60)) end
Boom shaka laka
At this point adding an event to a calendar is as easy as pie. Here’s my Twitter Bootstrap worthy HAML
%ul.nav.pull-left
%li.dropdown
= link_to icon('icon-calendar'), '#', {'data-toggle' => "dropdown", :class => 'dropdown-toggle'}
%ul.dropdown-menu{'role' => "menu"}
%li= link_to 'Google', google_calendar_url(@event), :target => '_blank'
%li= link_to 'Live', live_calendar_url(@event), :target => '_blank'
%li= link_to 'Yahoo', yahoo_calendar_url(@event), :target => '_blank'
%li= link_to 'iCal', plan_event_path(@event, :format => :ics)
The last link is to a downloadable .ics file, which is a topic for another day.




1 response so far ↓
1 shoo // Mar 12, 2013 at 8:47 am
Thanks for this little snippet. I was looking for something like this the other day. FYI – For a Live calendar event, the details section can be passed by the “description” parameter.
Leave a Comment