-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdigest_mailer.rb
More file actions
40 lines (32 loc) · 1.54 KB
/
digest_mailer.rb
File metadata and controls
40 lines (32 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Hackathons::DigestMailer < ApplicationMailer
def digest
@digest = params[:digest]
@recipient = @digest.recipient
@listings_by_subscription = @digest.listings
.includes(:subscription, hackathon: {logo_attachment: :blob})
.group_by(&:subscription)
set_unsubscribe_urls_for @recipient
mail to: @recipient.email_address, subject: "Hackathons near you"
end
def admin_summary(sent_digests)
# This reloads the (possible) sent_digests array as an
# ActiveRecord::Relation so that we can use includes to prevent an N+1.
@sent_digests = Hackathon::Digest.where(id: sent_digests.map(&:id))
@sent_digests_by_hackathons = @sent_digests
.includes(listings: {hackathon: {logo_attachment: :blob}})
.flat_map(&:listings).group_by(&:hackathon)
.transform_values { |listings| listings.map(&:digest).uniq }
@listed_hackathons = @sent_digests_by_hackathons.keys
.sort_by { |hackathon| @sent_digests_by_hackathons[hackathon].count }.reverse!
subject = <<~SUBJECT.squish
📬 Hackathons: #{@sent_digests.count} #{"email".pluralize(@sent_digests.count)}
sent for #{@listed_hackathons.count} #{"hackathon".pluralize(@listed_hackathons.count)}
SUBJECT
mail to: Hackathons::SUPPORT_EMAIL, cc: User.admins.collect(&:email_address), subject:
end
def organizer_summary(hackathon, sent_digests)
@hackathon = hackathon
@count = sent_digests.count
mail to: hackathon.applicant.email_address, subject: "We've just notified #{@count} hackers about #{hackathon.name}!"
end
end