refactors email digest

This commit is contained in:
rgarcia
2016-08-03 19:33:37 +02:00
parent 6281af2a7d
commit b087fe8fb7
4 changed files with 26 additions and 13 deletions

View File

@@ -60,8 +60,8 @@ class Mailer < ApplicationMailer
end end
end end
def proposal_notification_digest(user) def proposal_notification_digest(user, notifications)
@notifications = user.notifications.where(notifiable_type: "ProposalNotification") @notifications = notifications
with_user(user) do with_user(user) do
mail(to: user.email, subject: t('mailers.proposal_notification_digest.title', org_name: Setting['org_name'])) mail(to: user.email, subject: t('mailers.proposal_notification_digest.title', org_name: Setting['org_name']))

View File

@@ -2,9 +2,11 @@ class Notification < ActiveRecord::Base
belongs_to :user, counter_cache: true belongs_to :user, counter_cache: true
belongs_to :notifiable, polymorphic: true belongs_to :notifiable, polymorphic: true
scope :unread, -> { all } scope :unread, -> { all }
scope :recent, -> { order(id: :desc) } scope :recent, -> { order(id: :desc) }
scope :for_render, -> { includes(:notifiable) } scope :not_emailed, -> { where(emailed_at: nil) }
scope :for_render, -> { includes(:notifiable) }
def timestamp def timestamp
notifiable.created_at notifiable.created_at

View File

@@ -1,13 +1,22 @@
class EmailDigest class EmailDigest
def initialize attr_accessor :user, :notifications
def initialize(user)
@user = user
end end
def create def notifications
User.email_digest.each do |user| user.notifications.not_emailed.where(notifiable_type: "ProposalNotification").to_a
if user.notifications.where(notifiable_type: "ProposalNotification").any? end
Mailer.proposal_notification_digest(user).deliver_later
end def pending_notifications?
notifications.any?
end
def deliver
if pending_notifications?
Mailer.proposal_notification_digest(user, notifications).deliver_later
end end
end end

View File

@@ -2,8 +2,10 @@ namespace :emails do
desc "Sends email digest of proposal notifications to each user" desc "Sends email digest of proposal notifications to each user"
task digest: :environment do task digest: :environment do
email_digest = EmailDigest.new User.email_digest.find_each do |user|
email_digest.create email_digest = EmailDigest.new(user)
email_digest.deliver
end
end end
end end