• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / oawaiver / 11c952aa-d8e8-4073-941a-ba08eee9b0bb

24 Sep 2024 03:41PM UTC coverage: 70.281% (-28.5%) from 98.795%
11c952aa-d8e8-4073-941a-ba08eee9b0bb

push

circleci

jrgriffiniii
wip

525 of 747 relevant lines covered (70.28%)

9.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.15
/app/mailers/waiver_mailer.rb
1
# frozen_string_literal: true
2

3
class WaiverMailer < ApplicationMailer
1✔
4
  class_attribute :initialized, default: false
1✔
5
  class_attribute :parameters
1✔
6
  class_attribute :options
1✔
7
  class_attribute :contact
1✔
8
  class_attribute :url
1✔
9
  class_attribute :from
1✔
10
  class_attribute :reply_to
1✔
11
  class_attribute :cc
1✔
12
  class_attribute :bcc
1✔
13
  class_attribute :send_to_author
1✔
14
  class_attribute :mail_templates
1✔
15

16
  def self.validate_email(address)
1✔
17
    email_valid = URI::MailTo::EMAIL_REGEXP.match(address)
1✔
18

19
    raise("Invalid email address: '#{address}'") unless email_valid
1✔
20
    email_valid
1✔
21
  end
22

23
  # initialize global variables based on Rails.application.config.waiver_mailer_parameters hash
24
  def self.bootstrap
1✔
25
    return if initialized
3✔
26

27
    parameters = Rails.application.config.waiver_mailer_parameters
1✔
28
    self.parameters = ActiveSupport::HashWithIndifferentAccess.new(parameters)
1✔
29
    env = self.parameters.fetch(Rails.env)
1✔
30
    self.options = ActiveSupport::HashWithIndifferentAccess.new(env)
1✔
31

32
    self.url = options.fetch("url", "")
1✔
33
    self.from = options.fetch("from", "")
1✔
34

35
    from_entries = Mail::FromField.new(from)
1✔
36
    from_entries.addresses.each { |e| validate_email(e) }
2✔
37

38
    self.reply_to = options.fetch("reply_to", "")
1✔
39
    reply_to_entries = Mail::ReplyToField.new(reply_to)
1✔
40
    reply_to_entries.addresses.each { |e| validate_email(e) }
1✔
41

42
    self.cc = options.fetch("cc", "")
1✔
43
    cc_entries = Mail::CcField.new(cc)
1✔
44
    cc_entries.addresses.each { |e| validate_email(e) }
1✔
45

46
    self.bcc = options.fetch("bcc", "")
1✔
47
    bcc_entries = Mail::BccField.new(bcc)
1✔
48
    bcc_entries.addresses.each { |e| validate_email(e) }
1✔
49

50
    self.send_to_author = options.fetch("send_to_author", "")
1✔
51

52
    mail_templates = parameters.fetch("mail_templates")
1✔
53
    raise("Failed to resolve the e-mail templates for: #{parameters}") if mail_templates.blank?
1✔
54

55
    mail_templates = ActiveSupport::HashWithIndifferentAccess.new(mail_templates)
1✔
56
    self.mail_templates = mail_templates
1✔
57

58
    required_keys = ["granted"]
1✔
59
    required_keys.each do |key|
1✔
60
      raise("Must define a '#{key}' mail template") unless mail_templates.key?(key)
1✔
61
    end
62

63
    self.mail_templates.keys.each do |k|
1✔
64
      key = k.to_sym
1✔
65

66
      parts = %w[body subject]
1✔
67
      parts.each do |part|
1✔
68
        fname = self.mail_templates[k][part]
2✔
69
        raise("Must define #{part} for #{key} mail template") if fname.blank?
2✔
70

71
        template_path = Rails.application.root.join("config", fname)
2✔
72
        template = File.read(template_path)
2✔
73
        raise("The file #{template_path} is empty") if template.empty?
2✔
74

75
        part_key = part.to_sym
2✔
76
        partial = ERB.new(template.strip)
2✔
77
        self.mail_templates[k][part_key] = partial
2✔
78
      end
79
    end
80

81
    self.initialized = true
1✔
82
    ActiveRecord::Base.logger.info("WaiverMailer has been initialized.")
1✔
83

84
    self
1✔
85
  end
86

87
  def self.default_author_email
1✔
88
    "author@princeton.edu"
×
89
  end
90

91
  # create WaiverMail from given waiver_info
92
  def initialize(waiver_info)
1✔
93
    self.class.bootstrap
2✔
94
    @waiver_info = waiver_info
2✔
95

96
    super()
2✔
97
  end
98

99
  def author_email
1✔
100
    return @waiver_info.author_email if self.class.send_to_author
×
101

102
    self.class.default_author_email
×
103
  end
104

105
  # compute to field for mail
106
  # depending on mail_to_authors use "author@princeton.edu" or waiver_info's author_email
107
  def to
1✔
108
    [
109
      @waiver_info.requester_email,
×
110
      author_email
111
    ]
112
  end
113

114
  def url
1✔
115
    @url ||= self.class.url
×
116
  end
117

118
  def waiver_info_url
1✔
119
    return if @waiver_info.nil? || !@waiver_info.persisted?
2✔
120

121
    @waiver_info_url ||= begin
×
122
                           pattern = /ID/
×
123
                           model_id = @waiver_info.id
×
124
                           value = url.sub(pattern, model_id.to_s)
×
125
                           value
×
126
                         end
127
  end
128

129
  def mail_templates
1✔
130
    @mail_templates ||= self.class.mail_templates
×
131
  end
132

133
  def granted_mail_template
1✔
134
    raise("No \"granted\" template is specified within the e-mail templates: #{mail_templates}") unless mail_templates.key?(:granted)
×
135

136
    @granted_mail_template ||= mail_templates[:granted]
×
137
  end
138

139
  def body_template
1✔
140
    return unless granted_mail_template.key?(:body)
×
141

142
    @body_template ||= granted_mail_template[:body]
×
143
  end
144

145
  def body
1✔
146
    return if body_template.nil?
×
147

148
    @body ||= body_template.result(binding)
×
149
  end
150

151
  def subject_template
1✔
152
    raise("No subject header specified within the e-mail template: #{granted_mail_template}") unless granted_mail_template.key?(:subject)
×
153

154
    @subject_template ||= granted_mail_template[:subject]
×
155
  end
156

157
  def subject
1✔
158
    return if subject_template.nil?
×
159

160
    @subject ||= subject_template.result(binding)
×
161
  end
162

163
  def from
1✔
164
    @from ||= self.class.from
×
165
  end
166

167
  def reply_to
1✔
168
    @reply_to ||= self.class.reply_to
×
169
  end
170

171
  def bcc
1✔
172
    @bcc ||= self.class.bcc
×
173
  end
174

175
  def cc
1✔
176
    @cc ||= self.class.cc
×
177
  end
178

179
  def granted(cc_address = nil)
1✔
180
    if cc_address.present?
×
181
      self.class.validate_email(cc_address)
×
182
      @cc = cc_address
×
183
    end
184

185
    mail(
×
186
      to: to,
187
      from: from,
188
      reply_to: reply_to,
189
      cc: cc,
190
      bcc: bcc,
191
      subject: subject,
192
      body: body
193
    )
194
  end
195
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc