← Back to Upcase

Should I use a listener/Observer that listens whenever some things change


(charlieanna) #1

I have a calendar that can be either created, edited or destroyed. And every time any of these actions happen I call a method send_email to send the notification to all the registered users.

Should I instead place an observer for the calender class that listens for any changes and which then calls send_email by asking the another class to send the email?


(Jose Alvarez) #2

Hi @ankur_kothari, I you can take advantage of ActiveRecord Callbacks like after_save, and after_destroy on the calendar model.

I.E.

class Calendar < ActiveRecord::Base
  after_save :send_email
  after_destroy :send_email

  def send_email
    mailer.send_email
  end
end

I propose calling the send_email on mailer, because calendar doesn’t need to know about mailing stuff (Single Responsability Principle)


(charlieanna) #3

Thanks @Jose_Alvarez. Will try this.