marks notifications as emailed after rake

This commit is contained in:
rgarcia
2016-08-03 19:48:43 +02:00
parent 0faff2f01c
commit 63274dfe08
3 changed files with 30 additions and 2 deletions

View File

@@ -7,7 +7,7 @@ class EmailDigest
end
def notifications
user.notifications.not_emailed.where(notifiable_type: "ProposalNotification").to_a
user.notifications.not_emailed.where(notifiable_type: "ProposalNotification")
end
def pending_notifications?
@@ -16,8 +16,12 @@ class EmailDigest
def deliver
if pending_notifications?
Mailer.proposal_notification_digest(user, notifications).deliver_later
Mailer.proposal_notification_digest(user, notifications.to_a).deliver_later
end
end
def mark_as_emailed
notifications.update_all(emailed_at: Time.now)
end
end

View File

@@ -5,6 +5,7 @@ namespace :emails do
User.email_digest.find_each do |user|
email_digest = EmailDigest.new(user)
email_digest.deliver
email_digest.mark_as_emailed
end
end

View File

@@ -94,4 +94,27 @@ describe EmailDigest do
end
describe "mark_as_emailed" do
it "marks notifications as emailed" do
user = create(:user)
proposal_notification = create(:proposal_notification)
notification1 = create(:notification, notifiable: proposal_notification, user: user)
notification2 = create(:notification, notifiable: proposal_notification, user: user)
expect(notification1.emailed_at).to_not be
expect(notification2.emailed_at).to_not be
email_digest = EmailDigest.new(user)
email_digest.mark_as_emailed
notification1.reload
notification2.reload
expect(notification1.emailed_at).to be
expect(notification2.emailed_at).to be
end
end
end