Alexander Kuzmin
Alexander Kuzmin

Reputation: 1120

Creating reminders/notifications in Rails

We're building an intranet in Ruby on Rails and now we want to add functionality for having reminders when you should have done something but haven't.

For example, say you have a model, Meeting, I want to send reminders to everyone who has had a meeting but where the meeting report is empty (meeting.date < Date.today && meeting.report == ""). And a project model where you have some other criteria (maybe project.last_report.date < lastMonday && !project.closed)

Now, I can imagine creating these reminders with some kind of rake task and then removing them by some event trigger when you update Meeting or whatever, but that seems like I'll get spaghetti code everywhere.

My second idea is to make a separate module that, on each page load, fetches all the entries that could be related and runs all these checks, and then returns Reminders, however, this will probably be hella slow to hit the database like this. (Maybe caching would be an option but still)

So, anybody done something like this and have any ideas on how to solve our problem?

Thanks!

Upvotes: 0

Views: 2102

Answers (2)

AndyV
AndyV

Reputation: 3741

I can't see any issue with spaghetti code if you let each object that requires a Reminder to manage it's own reminders. If you want to be an OOP purist you could probably create a separate class (e.g., MeetingReminderManager in your example) to manage the reminders but that seems like overkill here. Consider...

class Reminder
  belongs_to :source, polymorphic: true
  belongs_to :user
end

class Meeting
  has_many :reminders, as: :source
  has_many :users

  after_create :build_reminders, if: ->{|meeting| meeting.report.blank? }
  after_update :destroy_reminders, if: ->{|meeting| !meeting.report.blank? }

  private
    def build_reminders
      users.each{|user| self.reminders.create user_id: user.id, due_on: self.date }
    end

    def destroy_reminders
      self.reminders.delete_all
    end
end

Upvotes: 1

Jakub Kuchar
Jakub Kuchar

Reputation: 1665

I don't see a problem with spaghetti and background job in ruby on rails. I think making them is the path to go. Check whatever is suit you: http://railscasts.com/?tag_id=32

Upvotes: 0

Related Questions