Skip to content

Commit d2f50d9

Browse files
committed
Create Service object for Invitations management
This is an ugly first pass - I've just moved all the code out of the controller and into a new Ruby object. The tests still pass, which is a good start. The next step will be to look at whether there's more refactoring that can be done - for example, does the workshop need to be decorated on l.6? It enables email sending, but that might be a separate concern Signed-off-by: jonathan.kerr <3410350+jonodrew@users.noreply.github.com>
1 parent 6fee571 commit d2f50d9

File tree

2 files changed

+70
-62
lines changed

2 files changed

+70
-62
lines changed

app/controllers/admin/invitations_controller.rb

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ class Admin::InvitationsController < Admin::ApplicationController
22
include Admin::WorkshopConcerns
33

44
def update
5-
set_and_decorate_workshop
5+
@invitation_manager = Managers::InvitationManager.new(params[:workshop_id], invitation_id, current_user)
6+
@workshop = WorkshopPresenter.decorate(@invitation_manager.workshop)
67
authorize @workshop
7-
set_invitation
8-
9-
message = update_attendance(attending: params[:attending], attended: params[:attended])
8+
@invitation = @invitation_manager.invitation
9+
message = @invitation_manager.update_attendance(attending: params[:attending], attended: params[:attended])
1010

1111
if request.xhr?
1212
set_admin_workshop_data
@@ -18,64 +18,6 @@ def update
1818

1919
private
2020

21-
def update_attendance(attending:, attended:)
22-
return update_to_attended if attended
23-
24-
result = attending.eql?('true') ? update_to_attending : update_to_not_attending
25-
message, error = result.values_at(:message, :error)
26-
27-
unless error
28-
waiting_listed = WaitingList.find_by(invitation: @invitation)
29-
waiting_listed&.destroy
30-
end
31-
32-
message
33-
end
34-
35-
def update_to_attended
36-
@invitation.update(attended: true)
37-
38-
"You have verified #{@invitation.member.full_name}’s attendace."
39-
end
40-
41-
def update_to_attending
42-
update_successful = @invitation.update(
43-
attending: true,
44-
rsvp_time: Time.zone.now,
45-
automated_rsvp: true,
46-
last_overridden_by_id: current_user.id
47-
)
48-
49-
{
50-
message: update_successful ? attending_successful : attending_failed,
51-
error: !update_successful
52-
}
53-
end
54-
55-
def attending_successful
56-
@workshop.send_attending_email(@invitation) if @workshop.future?
57-
58-
"You have added #{@invitation.member.full_name} to the workshop as a #{@invitation.role}."
59-
end
60-
61-
def attending_failed
62-
"Error adding #{@invitation.member.full_name} as a #{@invitation.role}. "\
63-
"#{@invitation.errors.full_messages.to_sentence}."
64-
end
65-
66-
def update_to_not_attending
67-
@invitation.update!(attending: false, last_overridden_by_id: current_user.id)
68-
69-
{
70-
message: "You have removed #{@invitation.member.full_name} from the workshop.",
71-
error: false
72-
}
73-
end
74-
75-
def set_invitation
76-
@invitation = @workshop.invitations.find_by(token: invitation_id)
77-
end
78-
7921
def invitation_id
8022
params.key?(:workshop) ? params[:workshop][:invitations] : params[:id]
8123
end
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# frozen_string_literal: true
2+
module Managers
3+
class InvitationManager
4+
attr_reader :workshop, :invitation
5+
def initialize(workshop_id, invitation_id, current_user)
6+
@workshop = WorkshopPresenter.decorate(Workshop.find(workshop_id))
7+
@invitation = @workshop.invitations.find_by(token: invitation_id)
8+
@current_user = current_user
9+
end
10+
11+
def update_attendance(attending:, attended:)
12+
return update_to_attended if attended
13+
14+
result = attending.eql?('true') ? update_to_attending : update_to_not_attending
15+
message, error = result.values_at(:message, :error)
16+
17+
unless error
18+
waiting_listed = WaitingList.find_by(invitation: @invitation)
19+
waiting_listed&.destroy
20+
end
21+
22+
message
23+
end
24+
25+
private
26+
def update_to_attended
27+
@invitation.update(attended: true)
28+
29+
"You have verified #{@invitation.member.full_name}’s attendance."
30+
end
31+
32+
def update_to_attending
33+
update_successful = @invitation.update(
34+
attending: true,
35+
rsvp_time: Time.zone.now,
36+
automated_rsvp: true,
37+
last_overridden_by_id: @current_user.id
38+
)
39+
40+
{
41+
message: update_successful ? attending_successful : attending_failed,
42+
error: !update_successful
43+
}
44+
end
45+
46+
def update_to_not_attending
47+
@invitation.update!(attending: false, last_overridden_by_id: @current_user.id)
48+
49+
{
50+
message: "You have removed #{@invitation.member.full_name} from the workshop.",
51+
error: false
52+
}
53+
end
54+
55+
def attending_successful
56+
@workshop.send_attending_email(@invitation) if @workshop.future?
57+
58+
"You have added #{@invitation.member.full_name} to the workshop as a #{@invitation.role}."
59+
end
60+
61+
def attending_failed
62+
"Error adding #{@invitation.member.full_name} as a #{@invitation.role}. "\
63+
"#{@invitation.errors.full_messages.to_sentence}."
64+
end
65+
end
66+
end

0 commit comments

Comments
 (0)