note_observer.rb 954 Bytes
Newer Older
1 2 3
class NoteObserver < ActiveRecord::Observer

  def after_create(note)
4 5 6 7 8 9
    send_notify_mails(note)
  end

  protected

  def send_notify_mails(note)
10
    if note.notify
11
      notify_team(note)
12 13 14 15 16 17 18 19 20
    elsif note.notify_author
      # Notify only author of resource
      Notify.note_commit_email(note.commit_author.id, note.id).deliver
    else
      # Otherwise ignore it
      nil
    end
  end

21 22 23
  # Notifies the whole team except the author of note
  def notify_team(note)
    # Note: wall posts are not "attached" to anything, so fall back to "Wall"
24
    noteable_type = note.noteable_type.presence || "Wall"
25
    notify_method = "note_#{noteable_type.underscore}_email".to_sym
26 27 28

    if Notify.respond_to? notify_method
      team_without_note_author(note).map do |u|
29
        Notify.send(notify_method, u.id, note.id).deliver
30 31 32 33 34 35 36 37
      end
    end
  end

  def team_without_note_author(note)
    note.project.users.reject { |u| u.id == note.author.id }
  end
end