class Eventifier::Delivery

Attributes

notifications[R]
user[R]

Public Class Methods

deliver() click to toggle source
# File lib/eventifier/delivery.rb, line 2
def self.deliver
  unsent = Eventifier::Notification.unsent
  unsent.group_by(&:user).each do |user, notifications|
    new(user, notifications).deliver
  end
end
deliver_for(user) click to toggle source
# File lib/eventifier/delivery.rb, line 9
def self.deliver_for(user)
  new(user, user.notifications.unsent).deliver
end
new(user, notifications) click to toggle source
# File lib/eventifier/delivery.rb, line 13
def initialize(user, notifications)
  @user, @notifications = user, notifications
end

Public Instance Methods

deliver() click to toggle source
# File lib/eventifier/delivery.rb, line 17
def deliver
  if anything_to_send? && user_receives_emails?
    Eventifier.mailer.notifications(user, notifications_to_send).deliver
  end

  notifications.each do |notification|
    notification.update_attribute :sent, true
  end
end

Private Instance Methods

anything_to_send?() click to toggle source
# File lib/eventifier/delivery.rb, line 31
def anything_to_send?
  !notifications_to_send.empty?
end
include_notification?(notification) click to toggle source
# File lib/eventifier/delivery.rb, line 45
def include_notification?(notification)
  return true if settings.preferences['email'].nil?

  specifics = notification.relations.collect { |relation|
    key = [
      notification.event.verb,
      notification.event.eventable_type.underscore.pluralize,
      'notify',
      Eventifier::Relationship.new(self, relation).key
    ].join('_')
    settings.preferences['email'][key]
  }.compact

  return specifics.any? unless specifics.empty?

  default  = settings.preferences['email']['default']
  default.nil? || default
end
notifications_to_send() click to toggle source
# File lib/eventifier/delivery.rb, line 35
def notifications_to_send
  @notifications_to_send ||= notifications.select { |notification|
    include_notification? notification
  }
end
settings() click to toggle source
# File lib/eventifier/delivery.rb, line 41
def settings
  @settings ||= Eventifier::NotificationSetting.for_user user
end
user_receives_emails?() click to toggle source
# File lib/eventifier/delivery.rb, line 64
def user_receives_emails?
  return true unless user.respond_to? :eventifier_emails?

  user.eventifier_emails?
end