Skip to content

Commit 59a96ec

Browse files
committed
Migrate mailers to use parametrized attributes
1 parent 7a902dc commit 59a96ec

File tree

7 files changed

+36
-29
lines changed

7 files changed

+36
-29
lines changed

app/controllers/comments_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def comment_params
8888

8989
def send_mail_to_author(comment, request_for_comment)
9090
if current_user != request_for_comment.user
91-
UserMailer.got_new_comment(comment, request_for_comment, current_user).deliver_later
91+
UserMailer.with(comment:, request_for_comment:, commenting_user: current_user).got_new_comment.deliver_later
9292
end
9393
end
9494

@@ -101,7 +101,7 @@ def send_mail_to_subscribers(comment, request_for_comment)
101101
)
102102
subscriptions.each do |subscription|
103103
if (((subscription.subscription_type == 'author') && (current_user == request_for_comment.user)) || (subscription.subscription_type == 'all')) && !((subscription.user == current_user) || already_sent_mail)
104-
UserMailer.got_new_comment_for_subscription(comment, subscription, current_user).deliver_later
104+
UserMailer.with(comment:, subscription:, from_user: current_user).got_new_comment_for_subscription.deliver_later
105105
already_sent_mail = true
106106
end
107107
end

app/controllers/request_for_comments_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def set_thank_you_note
104104
@request_for_comment.thank_you_note = params[:note]
105105

106106
commenters = @request_for_comment.commenters
107-
commenters.each {|commenter| UserMailer.send_thank_you_note(@request_for_comment, commenter).deliver_later }
107+
commenters.each {|commenter| UserMailer.with(request_for_comment: @request_for_comment, receiver: commenter).send_thank_you_note.deliver_later }
108108

109109
respond_to do |format|
110110
if @request_for_comment.save

app/jobs/detect_exercise_anomalies_job.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def get_contributor_working_times(exercise)
103103

104104
def notify_collection_author(collection, anomalies)
105105
log("Sending E-Mail to author (#{collection.user.displayname} <#{collection.user.email}>)...", 2)
106-
UserMailer.exercise_anomaly_detected(collection, anomalies).deliver_later
106+
UserMailer.with(exercise_collection: collection, anomalies:).exercise_anomaly_detected.deliver_later
107107
end
108108

109109
def notify_contributors(collection, anomalies)
@@ -145,7 +145,7 @@ def notify_contributors(collection, anomalies)
145145
token = AuthenticationToken.generate!(user, last_submission.study_group).shared_secret
146146
feedback_link = Rails.application.routes.url_helpers.url_for(action: :new,
147147
controller: :user_exercise_feedbacks, exercise_id: exercise.id, host:, token:)
148-
UserMailer.exercise_anomaly_needs_feedback(user, exercise, feedback_link).deliver
148+
UserMailer.with(user:, exercise:, link: feedback_link).exercise_anomaly_needs_feedback.deliver_later
149149
end
150150
end
151151
log("Asked #{contributors_to_notify.size} contributors for feedback.", 2)

app/mailers/user_mailer.rb

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,26 @@ def reset_password_email(user)
1818
mail(subject: t('mailers.user_mailer.reset_password.subject'), to: user.email)
1919
end
2020

21-
def got_new_comment(comment, request_for_comment, commenting_user)
21+
def got_new_comment
2222
# TODO: check whether we can take the last known locale of the receiver?
23+
request_for_comment = params.fetch(:request_for_comment)
2324
token = AuthenticationToken.generate!(request_for_comment.user, request_for_comment.submission.study_group)
2425
@receiver_displayname = request_for_comment.user.displayname
25-
@commenting_user_displayname = commenting_user.displayname
26-
@comment_text = ERB::Util.html_escape comment.text
26+
@commenting_user_displayname = params.fetch(:commenting_user).displayname
27+
@comment_text = ERB::Util.html_escape params.fetch(:comment).text
2728
@rfc_link = request_for_comment_url(request_for_comment, token: token.shared_secret)
2829
mail(
2930
subject: t('mailers.user_mailer.got_new_comment.subject',
3031
commenting_user_displayname: @commenting_user_displayname), to: request_for_comment.user.email
3132
)
3233
end
3334

34-
def got_new_comment_for_subscription(comment, subscription, from_user)
35+
def got_new_comment_for_subscription
36+
subscription = params.fetch(:subscription)
3537
token = AuthenticationToken.generate!(subscription.user, subscription.study_group)
3638
@receiver_displayname = subscription.user.displayname
37-
@author_displayname = from_user.displayname
38-
@comment_text = ERB::Util.html_escape comment.text
39+
@author_displayname = params.fetch(:from_user).displayname
40+
@comment_text = ERB::Util.html_escape params.fetch(:comment).text
3941
@rfc_link = request_for_comment_url(subscription.request_for_comment, token: token.shared_secret)
4042
@unsubscribe_link = unsubscribe_subscription_url(subscription)
4143
mail(
@@ -44,22 +46,28 @@ def got_new_comment_for_subscription(comment, subscription, from_user)
4446
)
4547
end
4648

47-
def send_thank_you_note(request_for_comment, receiver)
49+
def send_thank_you_note
50+
request_for_comment = params.fetch(:request_for_comment)
51+
receiver = params.fetch(:receiver)
4852
token = AuthenticationToken.generate!(receiver, request_for_comment.submission.study_group)
53+
4954
@receiver_displayname = receiver.displayname
5055
@author = request_for_comment.user.displayname
5156
@thank_you_note = ERB::Util.html_escape request_for_comment.thank_you_note
5257
@rfc_link = request_for_comment_url(request_for_comment, token: token.shared_secret)
5358
mail(subject: t('mailers.user_mailer.send_thank_you_note.subject', author: @author), to: receiver.email)
5459
end
5560

56-
def exercise_anomaly_detected(exercise_collection, anomalies)
61+
def exercise_anomaly_detected
5762
# First, we try to find the best matching study group for the user being notified.
5863
# The study group is relevant, since it determines the access rights to the exercise within the collection.
5964
# The best matching study group is the one that grants access to the most exercises in the collection.
6065
# This approach might look complicated, but since it is called from a Rake task and no active user session, we need it.
61-
@relevant_exercises = Exercise.where(id: anomalies.keys)
62-
potential_study_groups = exercise_collection.user.study_groups.where(study_group_memberships: {role: StudyGroupMembership.roles[:teacher]})
66+
@collection = params.fetch(:exercise_collection)
67+
@anomalies = params.fetch(:anomalies)
68+
@relevant_exercises = Exercise.where(id: @anomalies.keys)
69+
70+
potential_study_groups = @collection.user.study_groups.where(study_group_memberships: {role: StudyGroupMembership.roles[:teacher]})
6371
potential_study_groups_with_expected_access = potential_study_groups.to_h do |study_group|
6472
exercises_granting_access = @relevant_exercises.count do |exercise|
6573
author_study_groups = exercise.author.study_groups.where(study_group_memberships: {role: StudyGroupMembership.roles[:teacher]})
@@ -70,18 +78,17 @@ def exercise_anomaly_detected(exercise_collection, anomalies)
7078
best_matching_study_group = potential_study_groups_with_expected_access.max_by {|_study_group, exercises_granting_access| exercises_granting_access }.first
7179

7280
# Second, all relevant values are passed to the view
73-
@user = exercise_collection.user
81+
@user = @collection.user
7482
@receiver_displayname = @user.displayname
7583
@token = AuthenticationToken.generate!(@user, best_matching_study_group).shared_secret
76-
@collection = exercise_collection
77-
@anomalies = anomalies
78-
mail(subject: t('mailers.user_mailer.exercise_anomaly_detected.subject'), to: exercise_collection.user.email)
84+
mail(subject: t('mailers.user_mailer.exercise_anomaly_detected.subject'), to: @user.email)
7985
end
8086

81-
def exercise_anomaly_needs_feedback(user, exercise, link)
87+
def exercise_anomaly_needs_feedback
88+
user = params.fetch(:user)
8289
@receiver_displayname = user.displayname
83-
@exercise_title = exercise.title
84-
@link = link
90+
@exercise_title = params.fetch(:exercise).title
91+
@link = params.fetch(:link)
8592
mail(subject: t('mailers.user_mailer.exercise_anomaly_needs_feedback.subject'), to: user.email)
8693
end
8794
end

spec/mailers/previews/user_mailer_preview.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ class UserMailerPreview < ActionMailer::Preview
44
def exercise_anomaly_detected
55
collection = ExerciseCollection.new(name: 'Hello World', user: FactoryBot.build(:admin))
66
anomalies = {49 => 879.325828, 51 => 924.870057, 31 => 1031.21233, 69 => 2159.182116}
7-
UserMailer.exercise_anomaly_detected(collection, anomalies)
7+
UserMailer.with(exercise_collection: collection, anomalies:).exercise_anomaly_detected.deliver_later
88
end
99
end

spec/mailers/user_mailer_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
let(:token) { AuthenticationToken.find_by(user:) }
6767
let(:request_for_comment) { create(:rfc_with_comment, user:) }
6868
let(:commenting_user) { InternalUser.create(attributes_for(:teacher)) }
69-
let(:mail) { described_class.got_new_comment(request_for_comment.comments.first, request_for_comment, commenting_user).deliver_now }
69+
let(:mail) { described_class.with(comment: request_for_comment.comments.first, request_for_comment:, commenting_user:).got_new_comment.deliver_now }
7070

7171
it 'sets the correct sender' do
7272
expect(mail.from).to include('[email protected]')
@@ -120,7 +120,7 @@
120120
let(:request_for_comment) { create(:rfc_with_comment, user:) }
121121
let(:subscription) { Subscription.create(request_for_comment:, user:, study_group_id: user.current_study_group_id) }
122122
let(:from_user) { InternalUser.create(attributes_for(:teacher)) }
123-
let(:mail) { described_class.got_new_comment_for_subscription(request_for_comment.comments.first, subscription, from_user).deliver_now }
123+
let(:mail) { described_class.with(comment: request_for_comment.comments.first, subscription:, from_user:).got_new_comment_for_subscription.deliver_now }
124124

125125
it 'sets the correct sender' do
126126
expect(mail.from).to include('[email protected]')
@@ -173,7 +173,7 @@
173173
let(:receiver) { create(:teacher) }
174174
let(:token) { AuthenticationToken.find_by(user: receiver) }
175175
let(:request_for_comment) { create(:rfc_with_comment, user:) }
176-
let(:mail) { described_class.send_thank_you_note(request_for_comment, receiver).deliver_now }
176+
let(:mail) { described_class.with(request_for_comment:, receiver:).send_thank_you_note.deliver_now }
177177

178178
it 'sets the correct sender' do
179179
expect(mail.from).to include('[email protected]')

spec/system/authentication_system_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
let(:study_group) { request_for_comment.submission.study_group }
8484
let(:request_for_comment) { create(:rfc_with_comment, user:) }
8585
let(:commenting_user) { InternalUser.create(attributes_for(:teacher)) }
86-
let(:mail) { UserMailer.got_new_comment(request_for_comment.comments.first, request_for_comment, commenting_user) }
86+
let(:mail) { UserMailer.with(comment: request_for_comment.comments.first, request_for_comment:, commenting_user:).got_new_comment }
8787
let(:rfc_link) { request_for_comment_url(request_for_comment, token: token.shared_secret) }
8888

8989
before { allow(AuthenticationToken).to receive(:generate!).with(user, study_group).and_return(token).once }
@@ -143,12 +143,12 @@
143143
let(:request_for_comment) { create(:rfc_with_comment, user:) }
144144
let(:study_group) { request_for_comment.submission.study_group }
145145
let(:commenting_user) { InternalUser.create(attributes_for(:teacher)) }
146-
let(:mail) { UserMailer.got_new_comment(request_for_comment.comments.first, request_for_comment, commenting_user) }
146+
let(:mail) { UserMailer.with(comment: request_for_comment.comments.first, request_for_comment:, commenting_user:).got_new_comment }
147147
let(:rfc_link) { request_for_comment_url(request_for_comment, token: token.shared_secret) }
148148

149149
it 'still invalidates the token on login' do
150150
token = create(:authentication_token, user:, study_group:)
151-
mail = UserMailer.got_new_comment(request_for_comment.comments.first, request_for_comment, commenting_user)
151+
mail = UserMailer.with(comment: request_for_comment.comments.first, request_for_comment:, commenting_user:).got_new_comment
152152
mail.deliver_now
153153
visit(request_for_comment_url(request_for_comment, token: token.shared_secret))
154154
expect(token.reload.expire_at).to be_within(10.seconds).of(Time.zone.now)

0 commit comments

Comments
 (0)