refactors email digest
This commit is contained in:
@@ -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']))
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ class Notification < ActiveRecord::Base
|
|||||||
|
|
||||||
scope :unread, -> { all }
|
scope :unread, -> { all }
|
||||||
scope :recent, -> { order(id: :desc) }
|
scope :recent, -> { order(id: :desc) }
|
||||||
|
scope :not_emailed, -> { where(emailed_at: nil) }
|
||||||
scope :for_render, -> { includes(:notifiable) }
|
scope :for_render, -> { includes(:notifiable) }
|
||||||
|
|
||||||
|
|
||||||
def timestamp
|
def timestamp
|
||||||
notifiable.created_at
|
notifiable.created_at
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -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?
|
|
||||||
Mailer.proposal_notification_digest(user).deliver_later
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def pending_notifications?
|
||||||
|
notifications.any?
|
||||||
|
end
|
||||||
|
|
||||||
|
def deliver
|
||||||
|
if pending_notifications?
|
||||||
|
Mailer.proposal_notification_digest(user, notifications).deliver_later
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user