nisanth074
6/22/2014 - 1:05 PM

gistfile1.rb

class CommentCreated < Action
  include WatchersToBeNotified

  watchers_to_be_notified {
    ticket_attr_reader :ticket

    categories {
      agents to_be_notified_if: :all_comments
      user_assignees to_be_notified_if: :comment_assignment
      group_assignees to_be_notified_if: :comment_group_assignment
    }

    blacklist_if { |watcher| watcher.user == @comment.author }
  }

  watchers_to_be_notified {
    agents to_be_notified_if: :all_comments
    user_assignees to_be_notified_if: :comment_assignment
    group_assignees to_be_notified_if: :comment_group_assignment

    inputs {
      ticket_attr_reader :ticket
      blacklist_if { |watcher| watcher.user == @comment.author }
    }
  }

  watchers_to_be_notified {
    agents to_be_notified_if: :all_comments
    user_assignees to_be_notified_if: :comment_assignment
    group_assignees to_be_notified_if: :comment_group_assignment

    blacklist_if { |watcher| watcher.user == @comment.author }

    inputs {
      ticket_attr_reader :ticket
    }
  }

  recipients :watchers_to_be_notified
  recipients :mentioned_users

  # Global blacklist
  blacklist_recipient_if :system_user?, :app_user?

  # mail_renderer :mail_renderer
  mail_renderer { |recipient| TicketNotifications.comment(@comment, recipient) }
  
  attr_reader :comment
  
  def initialize(@comment)
    @comment = comment
  end  

  def mentioned_users
    @comment.mentioned_users
  end
  
  def system_user?(user)
    user.system?
  end

  def app_user(user)
    user.app?
  end
end

class Action
  def send_notifications
    recipients.uniq.each(&:notify)
  end
end