Posts in rails

Sending Morning Emails

NOTE: I originally wrote this when I was still working on Stride. I’ve reposted it here for posterity. Here is the original post.

Reliably delivering time-based emails (daily, weekly, etc.) to users has always been a little tricky for web-based applications. There are multiple possible failure points, and we need to contend with different time zones.

Currently, Stride sends two time-based emails:

  1. Your Monday morning weekly recap
  2. Task reminders

We wanted both of these emails to arrive in the user’s inbox in the morning. At first, this seems like a simple specification; we’ll just send the emails off at 7am.

But hold on a minute — if we send the Monday morning email at 7am PST, users in New York won’t get their email until 10am, and even worse, our users in Australia won’t get it until 1am on Tuesday; that certainly isn’t Monday morning. What if we send it early enough so that everyone gets it before Monday morning? Unfortunately, if we send the it at 7am in eastern Australia, our users in Hawaii will get their Monday morning email at 11am on Sunday. That just isn’t going to cut it.

We decided to batch up the emails and send them off depending on each user’s time zone. Here’s how it works:

in rails Read on →

Using I18n and Draper to Render Database Attributes

Using I18n and Draper to Render Database Attributes

TL;DR: Check out my additions to ApplicationDecorator in this gist.

Update: This has been released as a gem: Olson.

When my models have an attribute that matters to the code (like Admin#role or User#status), I like to store the value as a string that makes sense as an identifier. For example, User#status might be 'active' or 'awaiting_approval'. However, when it comes time to render the admin’s role or the users status in the view, we want to show ‘Awaiting approval’ instead of ‘awaiting_approval’. Another example of this sort of thing is the #type attribute for STI.

Ok, this isn’t too hard, we can just use #humanize. But, here’s what happens:

in rails Read on →