DEV Community

Code Salley
Code Salley

Posted on

5 4

Ruby on Rails event invitation add to calendar using icalendar gem

In rails sending an event invitation is simple, But getting an invite recognized by Gmail for easy "add to calendar" feature is tricky. After experimenting with it for a while, this simple config does the trick.

require 'icalendar/tzinfo'
class EventMailer < ApplicationMailer
 def send_invitation(_user, _event)

  # initialize a new icalendar class
  ical = Icalendar::Calendar.new

  # Define default time
  time_zone = 'UTC'
  cal_tz = TZInfo::Timezone.get time_zone

  # add timezone to icalendar 
  ical.add_timezone cal_tz

  # new icalendar event 
  event = Icalendar::Event.new

  # event start date
  event.dtstart = Icalendar::Values::DateTime.new _event.start_time, 'tzid' => cal_tz

  # event end date
  event.dtend = Icalendar::Values::DateTime.new _event.end_time, 'tzid' => cal_tz


  # event organizer
  event.organizer = Icalendar::Values::CalAddress.new("mailto:" + _user.email)

  # event created date
  event.created = DateTime.now

  # event location
  event.location =  _event.venue

  # if there's an external link e.g, google meet
  event.uid = event.url = _event.meeting_link


  # event title
  event.summary = _event.title


  # event description
  event.description = _event.description


  # attach the configured event to icalendar class
  ical.add_event(event)


  # protocol
  ical.append_custom_property("METHOD", "REQUEST")

  # this add's an attachment name `event.ics`, 
  # when clicked, the event gets added to the calendar. 

  mail.attachments['event.ics'] = { mime_type: 'application/ics', content: ical.to_ical }


  # send mail
  mail(to: _user.email, subject:  "#{_event.title} - #{_event.summary} ")
 end
end
Enter fullscreen mode Exit fullscreen mode

enjoy!

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more