views:

591

answers:

4

Cron solutions in rails are numerous and pretty good. That's not what I'm struggling with here.

Instead, what I'm having trouble with is letting users create their own recurring tasks (like reminders) - specifically how to model and store these in the DB (a good UI for it is non-trivial too - would be awesome if there was code out there for that). Google calendar is a great example here (the UI to add an event, not the entire calendar)...they should be able to do daily at 1pm CST, or mon/wed/fri, or weekly, etc. Whatever cron solution is being used would then need to poll the database to see which reminders needed to be sent at that hour, etc.

Anyone seen a good plugin/gem for this in rails? Seems like there would be something out there for it, but I haven't found it yet.

Thanks!

+1  A: 

Currently thinking about using a plugin like this to store recurrences in a table http://github.com/fnando/recurrence/tree/master

each reminder would have one recurrence object, and the reminder would also keep a datetime field when it's supposed to send it's next one. Then the cron could...

get all reminder's whose "next_send" date has passed
for each reminder
  send it
  update the "next_send" field using the recurrence object
end

If there are better solutions or I'm going down the wrong path, input always appreciated.

Brian Armstrong
A: 

I have always found ical (the RFC, not the program) solutions to be the best approach for working with recurring events. There are a few good Ruby libraries for dealing with ical, and the newest kid on the block is ri_cal.

Benjamin Curtis
rical looks great and full features, I'm sort of confused on how it might integrate with rails. Does it store events or calendar objects to the database? How would you looking past due events or do queries? Thanks!
Brian Armstrong
looking => look up
Brian Armstrong
unfortunately looks like rical doesn't let you save anything to the database....it's just for reading/generating ical files
Brian Armstrong
A: 

I ended up rolling my own solution since I didn't need anything super fancy.

Basically I added a next_run datetime and interval string column to the db which was one of (day, week, etc). Then setup a cron job to run that looks for any next_run dates that have passed. It runs them and then sets next_run to some point in the future based on the interval column.

Simple but worked for my needs.

Brian Armstrong