@@ -16,11 +16,16 @@ class ProtipMailer < ActionMailer::Base
16
16
endorsements : 'endorsements' ,
17
17
protips_count : 'protips'
18
18
}
19
+ ACTIVITY_SUBJECT_PREFIX = '[Coderwall]'
19
20
20
21
#################################################################################
21
22
def popular_protips ( user , protips , from , to )
22
- fail "Protips are required." if protips . nil? || protips . empty?
23
- headers [ 'X-Mailgun-Campaign-Id' ] = 'coderwall-popular_protips'
23
+ fail 'Protips are required.' if protips . nil? || protips . empty?
24
+ fail 'User is required.' unless user
25
+ fail 'From date is required.' unless from
26
+ fail 'To date is required.' unless to
27
+
28
+ headers [ 'X-Mailgun-Campaign-Id' ] = 'protip_mailer-popular_protips'
24
29
25
30
@user = user
26
31
@protips = protips
@@ -31,13 +36,21 @@ def popular_protips(user, protips, from, to)
31
36
@star_stat = star_stat_for_this_week
32
37
@star_stat_string = STARS [ @star_stat ]
33
38
34
- @most = star_stats ( stars ) . sort_by { |star | -star [ @star_stat ] } . first
39
+ @most = star_stats ( stars ) . sort_by do |star |
40
+ -star [ @star_stat ]
41
+ end . first
35
42
@most = nil if @most && ( @most [ @star_stat ] <= 0 )
36
43
37
- mail to : 'mike@just3ws.com' , subject : 'Popular Protips on Coderwall'
44
+ mail ( to : @user . email , subject : "#{ ACTIVITY_SUBJECT_PREFIX } Popular Protips on Coderwall" )
45
+ rescue Exception => ex
46
+ abort_delivery ( ex )
38
47
end
39
48
#################################################################################
40
49
50
+ def abort_delivery ( ex )
51
+ Rails . logger . error ( "[ProtipMailer.popular_protips] Aborted email '#{ ex } ' >>\n #{ ex . backtrace . join ( "\n " ) } " )
52
+ end
53
+
41
54
def campaign_params
42
55
{
43
56
utm_campaign : 'coderwall-popular_protips' ,
@@ -65,7 +78,7 @@ def get_team_and_job_for(user)
65
78
teams = teams_for_user ( user )
66
79
teams . each do |team |
67
80
best_job = team . best_positions_for ( user ) . detect do |job |
68
- ( job . team_document_id == user . team_document_id ) || ! already_sent? ( job , user )
81
+ job . team_document_id == user . team_document_id
69
82
end
70
83
return [ team , best_job ] unless best_job . nil?
71
84
end
@@ -79,16 +92,12 @@ def teams_for_user(user)
79
92
end
80
93
end
81
94
82
- def already_sent? ( mailable , user )
83
- SentMail . where ( user_id : user . id , mailable_id : mailable . id , mailable_type : mailable . class . name ) . exists?
84
- end
85
-
86
95
module Queries
87
96
def self . popular_protips ( from , to )
88
97
search_results = ProtipMailer ::Queries . search_for_popular_protips ( from , to )
89
98
public_ids = search_results . map { |protip | protip [ 'public_id' ] }
90
99
91
- Protip . eager_load ( :user , :comments ) . where ( " public_id in (?)" , public_ids )
100
+ Protip . eager_load ( :user , :comments ) . where ( ' public_id in (?)' , public_ids )
92
101
end
93
102
94
103
def self . search_for_popular_protips ( from , to , max_results = 10 )
0 commit comments