Skip to content

Commit 88afa51

Browse files
davezuckermanDavid Zuckerman
andauthored
Calling deliver_later for forms instead of deliver_now (#16)
added struct file for affiliate borrow request moved struct into model for borrow_request removed borrow_request.rb struct file Changing doemoff_patron_email_form to use struct similar to affiliate_borrower Changing stack pass for to use deliver_later Changed deliver_now to deliver_later for more forms Update request_mailer to work with update to efees which uses deliver_later now removed ActiveJob::Base.queue_adapter = :test from affiliate_borrow_request_form, already a default Co-authored-by: David Zuckerman <dzuckerm@library.berkeley.edu>
1 parent 2e73735 commit 88afa51

12 files changed

Lines changed: 242 additions & 137 deletions

app/mailers/request_mailer.rb

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ class RequestMailer < ApplicationMailer
77
helper :mail
88

99
# Sends the AffiliateBorrowRequestForm
10-
def affiliate_borrow_request_form_email(borrow_request)
11-
@borrow_request = borrow_request
10+
def affiliate_borrow_request_form_email(borrow_request_hash)
11+
@borrow_request = AffiliateBorrowRequestForm::BorrowRequest.new(**borrow_request_hash)
1212

13-
mail(to: borrow_request.department_head_email)
13+
mail(to: @borrow_request.department_head_email)
1414
end
1515

1616
# Send LibstaffEdevicesLoanRequest confirmation email to user
@@ -40,16 +40,21 @@ def doemoff_room_failure_email(empid, displayname, note)
4040
mail(to: admin_to)
4141
end
4242

43-
# Sends doemoff patron email
44-
def doemoff_patron_email(email_form)
45-
@email_form = email_form
46-
mail(to: @email_form.patron_email, bcc: @email_form.recipient_email, reply_to: @email_form.recipient_email,
47-
subject: 'Message from Doe/Moffitt Libraries Staff')
43+
def doemoff_patron_email(payload)
44+
@email_form = DoemoffPatronEmailForm::PatronEmail.new(**payload)
45+
46+
mail(
47+
to: @email_form.patron_email,
48+
bcc: @email_form.recipient_email,
49+
reply_to: @email_form.recipient_email,
50+
subject: 'Message from Doe/Moffitt Libraries Staff'
51+
)
4852
end
4953

5054
# Sends the Security Incident form email
51-
def security_incident_email(security_incident)
52-
@security_incident = security_incident
55+
def security_incident_email(payload)
56+
@security_incident = SecurityIncidentReportForm::SecurityIncident.new(**payload)
57+
5358
if @security_incident.sup_email.nil?
5459
mail(to: security_to, subject: 'Incident Report Form email')
5560
else
@@ -227,7 +232,8 @@ def bibliographic_email(email, attachment_contents, subject, body)
227232
mail(to: email, subject:, body:)
228233
end
229234

230-
def efee_invoice_email(efee)
235+
def efee_invoice_email(alma_id)
236+
efee = EfeesInvoice.new(alma_id)
231237
# type probably isn't needed now that I spun this off to a separate url
232238
params = {
233239
type: 'efee',
@@ -239,7 +245,7 @@ def efee_invoice_email(efee)
239245
@name = efee.name
240246
@link = invoice_link.to_s
241247

242-
mail(to: efee.email)
248+
mail(to: efee.email, subject: 'Library Fees')
243249
end
244250

245251
# Departmental Card Request : alerts to privdesk, Mark Marrow and Supervisor

app/models/affiliate_borrow_request_form.rb

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
# app/models/affiliate_borrow_request_form.rb
12
class AffiliateBorrowRequestForm < Form
2-
attr_accessor(
3-
:department_head_email,
4-
:department_head_name,
5-
:department_name,
6-
:employee_email,
7-
:employee_id,
8-
:employee_name,
9-
:employee_personal_email,
10-
:employee_phone,
11-
:employee_preferred_name,
12-
:employee_address
13-
)
3+
ATTRIBUTES = %i[
4+
department_head_email
5+
department_head_name
6+
department_name
7+
employee_email
8+
employee_id
9+
employee_name
10+
employee_personal_email
11+
employee_phone
12+
employee_preferred_name
13+
employee_address
14+
].freeze
15+
16+
BorrowRequest = Struct.new(*ATTRIBUTES, keyword_init: true)
17+
18+
attr_accessor(*ATTRIBUTES)
1419

1520
validates :department_name,
1621
presence: true
@@ -39,9 +44,14 @@ class AffiliateBorrowRequestForm < Form
3944
validates :employee_address,
4045
presence: true
4146

42-
private
47+
# Return a serializable hash for deliver_later
48+
def to_h
49+
ATTRIBUTES.index_with { |attr| public_send(attr) }
50+
end
51+
52+
def submit!
53+
raise ActiveModel::ValidationError, self unless valid?
4354

44-
def submit
45-
RequestMailer.affiliate_borrow_request_form_email(self).deliver_now
55+
RequestMailer.affiliate_borrow_request_form_email(to_h).deliver_later
4656
end
4757
end
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
class DoemoffPatronEmailForm < Form
2-
attr_accessor(
3-
:patron_email,
4-
:patron_message,
5-
:sender,
6-
:recipient_email
7-
)
2+
ATTRIBUTES = %i[
3+
patron_email
4+
patron_message
5+
sender
6+
recipient_email
7+
].freeze
8+
9+
PatronEmail = Struct.new(*ATTRIBUTES, keyword_init: true)
10+
11+
attr_accessor(*ATTRIBUTES)
812

913
validates :patron_email,
1014
email: true,
1115
presence: true
1216

1317
validates :patron_message, :sender, :recipient_email, presence: true
1418

15-
private
19+
def to_h
20+
ATTRIBUTES.index_with { |attr| public_send(attr) }
21+
end
22+
23+
def submit!
24+
raise ActiveModel::ValidationError, self unless valid?
1625

17-
def submit
18-
RequestMailer.doemoff_patron_email(self).deliver_now
26+
RequestMailer.doemoff_patron_email(to_h).deliver_later
1927
end
2028
end

app/models/efees_invoice.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def encode
4141

4242
def submit!
4343
# Send the email with the link to the user!
44-
RequestMailer.efee_invoice_email(self).deliver_now
44+
RequestMailer.efee_invoice_email(alma_id).deliver_later
4545
end
4646

4747
private

app/models/proxy_borrower_requests.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class ProxyBorrowerRequests < ActiveRecord::Base
2727
validate :date_limit
2828

2929
def submit!
30-
RequestMailer.proxy_borrower_request_email(self).deliver_now
31-
RequestMailer.proxy_borrower_alert_email(self).deliver_now
30+
RequestMailer.proxy_borrower_request_email(self).deliver_later
31+
RequestMailer.proxy_borrower_alert_email(self).deliver_later
3232
end
3333

3434
def full_name

app/models/reference_card_form.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ class ReferenceCardForm < StackRequest
44
MAX_LENGTH = 91
55

66
def submit!
7-
RequestMailer.reference_card_email(self).deliver_now
7+
RequestMailer.reference_card_email(self).deliver_later
88
end
99

1010
def approve!
11-
RequestMailer.reference_card_approved(self).deliver_now
11+
RequestMailer.reference_card_approved(self).deliver_later
1212
end
1313

1414
def deny!
15-
RequestMailer.reference_card_denied(self).deliver_now
15+
RequestMailer.reference_card_denied(self).deliver_later
1616
end
1717

1818
# Add up and return the total number of days this requester
Lines changed: 74 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,71 @@
11
class SecurityIncidentReportForm < Form
2-
attr_accessor(
3-
:incident_location,
4-
:incident_date,
5-
:incident_time,
6-
:reporter_name,
7-
:email,
8-
:unit,
9-
:phone,
10-
:incident_description,
11-
:theft_personal,
12-
:theft_library,
13-
:vandalism,
14-
:assault,
15-
:criminal_other,
16-
:rules_violation,
17-
:irate_abusive,
18-
:physical_altercation,
19-
:disruptive_other,
20-
:power_outage,
21-
:flooding,
22-
:elevator,
23-
:fire,
24-
:facility_other,
25-
:library_employee,
26-
:student_patron,
27-
:campus_employee,
28-
:visitor,
29-
:injury_other,
30-
:injury_description,
31-
:subject_affiliation,
32-
:race,
33-
:sex,
34-
:build,
35-
:height,
36-
:weight,
37-
:hair,
38-
:eyes,
39-
:clothing,
40-
:subject_affiliation_1,
41-
:race_1,
42-
:sex_1,
43-
:build_1,
44-
:height_1,
45-
:weight_1,
46-
:hair_1,
47-
:eyes_1,
48-
:clothing_1,
49-
:subject_affiliation_2,
50-
:race_2,
51-
:sex_2,
52-
:build_2,
53-
:height_2,
54-
:weight_2,
55-
:hair_2,
56-
:eyes_2,
57-
:clothing_2,
58-
:property_description,
59-
:police_report_number,
60-
:officer_name_badge,
61-
:fire_department,
62-
:sup_email,
63-
:police_notified
64-
)
2+
ATTRIBUTES = %i[
3+
incident_location
4+
incident_date
5+
incident_time
6+
reporter_name
7+
email
8+
unit
9+
phone
10+
incident_description
11+
theft_personal
12+
theft_library
13+
vandalism
14+
assault
15+
criminal_other
16+
rules_violation
17+
irate_abusive
18+
physical_altercation
19+
disruptive_other
20+
power_outage
21+
flooding
22+
elevator
23+
fire
24+
facility_other
25+
library_employee
26+
student_patron
27+
campus_employee
28+
visitor
29+
injury_other
30+
injury_description
31+
subject_affiliation
32+
race
33+
sex
34+
build
35+
height
36+
weight
37+
hair
38+
eyes
39+
clothing
40+
subject_affiliation_1
41+
race_1
42+
sex_1
43+
build_1
44+
height_1
45+
weight_1
46+
hair_1
47+
eyes_1
48+
clothing_1
49+
subject_affiliation_2
50+
race_2
51+
sex_2
52+
build_2
53+
height_2
54+
weight_2
55+
hair_2
56+
eyes_2
57+
clothing_2
58+
property_description
59+
police_report_number
60+
officer_name_badge
61+
fire_department
62+
sup_email
63+
police_notified
64+
].freeze
65+
66+
SecurityIncident = Struct.new(*ATTRIBUTES, keyword_init: true)
67+
68+
attr_accessor(*ATTRIBUTES)
6569

6670
validates :email, :sup_email,
6771
email: true,
@@ -70,9 +74,13 @@ class SecurityIncidentReportForm < Form
7074
validates :incident_location, :incident_date, :incident_time, :reporter_name, :unit, :phone,
7175
:incident_description, presence: true
7276

73-
private
77+
def to_h
78+
ATTRIBUTES.index_with { |attr| public_send(attr) }
79+
end
80+
81+
def submit!
82+
raise ActiveModel::ValidationError, self unless valid?
7483

75-
def submit
76-
RequestMailer.security_incident_email(self).deliver_now
84+
RequestMailer.security_incident_email(to_h).deliver_later
7785
end
7886
end

app/models/stack_pass_form.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ class StackPassForm < StackRequest
33
validates :main_stack, acceptance: { message: 'Item must be located in the Main (Gardner) stacks.' }
44

55
def submit!
6-
RequestMailer.stack_pass_email(self).deliver_now
6+
RequestMailer.stack_pass_email(self).deliver_later
77
end
88

99
def approve!
10-
RequestMailer.stack_pass_approved(self).deliver_now
10+
RequestMailer.stack_pass_approved(self).deliver_later
1111
end
1212

1313
def deny!
14-
RequestMailer.stack_pass_denied(self).deliver_now
14+
RequestMailer.stack_pass_denied(self).deliver_later
1515
end
1616

1717
def approval_count

spec/models/affiliate_borrow_request_form_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
require 'rails_helper'
22

33
describe AffiliateBorrowRequestForm do
4+
include ActiveJob::TestHelper
5+
include ActionMailer::TestHelper
6+
47
it 'validates the form' do
58
tests = [
69
{
@@ -90,10 +93,8 @@
9093
employee_address: '123 North St, Berkeley, CA 94707'
9194
)
9295

93-
expect { form.submit! }.to(change { ActionMailer::Base.deliveries.count }.by(1))
94-
last_email = ActionMailer::Base.deliveries.last
95-
expect(last_email.subject).to eq('UC Berkeley Borrowing Card Requested')
96-
expect(last_email.to).to include('jeff@wilco.com')
96+
expect { form.submit! }.to have_enqueued_job(ActionMailer::MailDeliveryJob)
97+
9798
end
9899

99100
describe :model_name do

0 commit comments

Comments
 (0)