Skip to content

Instantly share code, notes, and snippets.

@paulmederos
Last active December 22, 2015 09:39
Show Gist options
  • Save paulmederos/6453716 to your computer and use it in GitHub Desktop.
Save paulmederos/6453716 to your computer and use it in GitHub Desktop.
Create events with ri_cal
# We have this in the Calendar.rb model
def create_events
ical_file = open(url)
cal = RiCal.parse(ical_file).first
cal.events.each do |event|
e = Event.find_or_initialize_by_url(event.url)
e.update_attributes({
name: event.summary,
event_start: event.dtstart.utc,
event_end: event.dtend.utc,
location: event.location,
description: event.description,
category: default_category,
user_id: user_id
})
end
end
################
# In the view for downloading the .ics
link_to "Add to Calendar", download_event_path, class: "add-to-calendar"
################
# In the Event.rb, we have this to create the .ics via ri_cal and pass it off
def download
@event = Event.find(params['id'])
cal = RiCal.Calendar do |cal|
cal.event do |event|
event.summary = @event.name if @event.name
event.description = @event.description if @event.description
event.dtstart = @event.event_start if @event.event_start.xmlschema
event.dtend = @event.event_end if @event.event_end.xmlschema
event.location = @event.location if @event.location
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment