Skip to content

Commit 8305a7c

Browse files
authored
Merge pull request #2795 from codebar/fix/event-invitation-nil-token
Fix events coach/student 500 when member already has an event invitation
2 parents f150cb8 + 46c06e6 commit 8305a7c

2 files changed

Lines changed: 75 additions & 2 deletions

File tree

app/controllers/events_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ def latest_model_updated
6363

6464
def find_invitation_and_redirect_to_event(role)
6565
set_event
66-
@invitation = Invitation.create_or_find_by(event: @event, member: current_user, role: role)
67-
redirect_to event_invitation_path(@event, @invitation)
66+
invitation = Invitation.create_or_find_by(event: @event, member: current_user, role: role)
67+
invitation = Invitation.find_by(event: @event, member: current_user, role: role) unless invitation.persisted?
68+
redirect_to event_invitation_path(@event, invitation)
6869
end
6970

7071
def set_event

spec/controllers/events_controller_spec.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,78 @@
1515
end
1616
end
1717

18+
describe 'GET #student' do
19+
let(:member) { Fabricate(:member) }
20+
let(:event) { Fabricate(:event) }
21+
22+
before { login(member) }
23+
24+
context 'when the member already has an invitation for the event and role with attending nil' do
25+
let!(:invitation) do
26+
Fabricate(:invitation, event: event, member: member, role: 'Student', attending: nil)
27+
end
28+
29+
it 'redirects to the existing invitation page' do
30+
get :student, params: { event_id: event.slug }
31+
32+
expect(response).to redirect_to(event_invitation_path(event, invitation))
33+
end
34+
35+
it 'does not create a new invitation' do
36+
expect do
37+
get :student, params: { event_id: event.slug }
38+
end.not_to change(Invitation, :count)
39+
end
40+
end
41+
42+
context 'when the member does not have an invitation for the event and role' do
43+
it 'creates a new invitation and redirects' do
44+
expect do
45+
get :student, params: { event_id: event.slug }
46+
end.to change(Invitation, :count).by(1)
47+
48+
invitation = Invitation.last
49+
expect(response).to redirect_to(event_invitation_path(event, invitation))
50+
end
51+
end
52+
end
53+
54+
describe 'GET #coach' do
55+
let(:member) { Fabricate(:member) }
56+
let(:event) { Fabricate(:event) }
57+
58+
before { login(member) }
59+
60+
context 'when the member already has a coach invitation for the event with attending nil' do
61+
let!(:invitation) do
62+
Fabricate(:coach_invitation, event: event, member: member, attending: nil)
63+
end
64+
65+
it 'redirects to the existing invitation page' do
66+
get :coach, params: { event_id: event.slug }
67+
68+
expect(response).to redirect_to(event_invitation_path(event, invitation))
69+
end
70+
71+
it 'does not create a new invitation' do
72+
expect do
73+
get :coach, params: { event_id: event.slug }
74+
end.not_to change(Invitation, :count)
75+
end
76+
end
77+
78+
context 'when the member does not have a coach invitation for the event' do
79+
it 'creates a new coach invitation and redirects' do
80+
expect do
81+
get :coach, params: { event_id: event.slug }
82+
end.to change(Invitation, :count).by(1)
83+
84+
invitation = Invitation.last
85+
expect(response).to redirect_to(event_invitation_path(event, invitation))
86+
end
87+
end
88+
end
89+
1890
describe '#past' do
1991
before { Fabricate(:event, date_and_time: 2.weeks.ago) }
2092

0 commit comments

Comments
 (0)