Skip to content

Commit 12932a1

Browse files
committed
Clean up participant delete validationss
1 parent 6ff30af commit 12932a1

File tree

6 files changed

+69
-20
lines changed

6 files changed

+69
-20
lines changed

app/admin/participants.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55

66
permit_params :name, :email, :bio, :email_confirmed_at
77

8+
controller do
9+
def destroy
10+
resource = Participant.find(params[:id])
11+
if resource.destroy
12+
flash[:notice] = "Participant was successfully deleted."
13+
redirect_to admin_participants_path
14+
else
15+
flash[:alert] = resource.errors.full_messages.to_sentence
16+
redirect_to admin_participant_path(resource)
17+
end
18+
end
19+
end
20+
821
includes :attendances, { presentations: { session: :event } }
922

1023
filter :name

app/controllers/sessions_controller.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ def index
5656
end
5757

5858
def create
59+
unless current_participant.email_confirmed?
60+
return render status: 403, plain: "Email confirmation required"
61+
end
62+
5963
unless Settings.allow_new_sessions?
60-
return render status: 403, plain: 'Session submission is closed'
64+
return render status: 403, plain: "Session submission is closed"
6165
end
6266

6367
@session.attributes = session_params.except(:code_of_conduct_agreement)

app/models/participant.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Participant < ActiveRecord::Base
2-
has_many :sessions
2+
has_many :sessions, -> { unscope(where: :canceled_at) }, dependent: :restrict_with_error
33
has_many :attendances, dependent: :destroy
44
has_many :sessions_attending, :through => :attendances, :source => :session
55
has_many :presentations

app/views/admin/legacy/presenters/_presenter.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
<%= presenter.email %>
77
</td>
88
<td>
9-
<%= presenter.sessions.for_current_event.map { |session| link_to(session.title, session) }.join(", ").html_safe %>
9+
<%= presenter.sessions.active.for_current_event.map { |session| link_to(session.title, session) }.join(", ").html_safe %>
1010
</td>
1111
</tr>

spec/controllers/sessions_controller_spec.rb

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,30 +123,40 @@
123123
let!(:event) { create(:event) }
124124
let(:category) { Category.last }
125125

126-
context "with valid values" do
127-
it "creates a new session " do
126+
context "when participant email is confirmed" do
127+
let(:user) { create(:participant, email_confirmed_at: Time.current) }
128+
129+
context "with valid values" do
130+
it "creates a new session" do
131+
expect {
132+
post :create, params: { session: { title: "new title", description: "new description", category_ids: [category.id], level_id: "2" } }
133+
}.to change { Session.count }.by(1)
134+
expect(response).to redirect_to assigns[:session]
135+
expect(assigns[:session].title).to eq "new title"
136+
expect(assigns[:session].participant).to eq user
137+
expect(assigns[:session].event).to eq event
138+
expect(assigns[:session].category_ids).to include category.id
139+
expect(flash[:notice]).to eq "Thanks for adding your session."
140+
end
141+
end
128142

129-
expect {
130-
post :create, params: { session: { title: 'new title', description: 'new description', category_ids: [category.id], level_id: '2' } }
131-
}.to change { Session.count }.by(1)
132-
expect(response).to redirect_to assigns[:session]
133-
expect(assigns[:session].title).to eq 'new title'
134-
expect(assigns[:session].participant).to eq user
135-
expect(assigns[:session].event).to eq event
136-
expect(assigns[:session].category_ids).to include category.id
137-
expect(flash[:notice]).to eq "Thanks for adding your session."
143+
context "with invalid values" do
144+
it "shows the errors" do
145+
expect {
146+
post :create, params: { session: { title: "" } }
147+
}.not_to change { Session.count }
148+
expect(response).to render_template("new")
149+
end
138150
end
139151
end
140152

141-
context "with invalid values" do
142-
it "shows the errors" do
143-
153+
context "when participant email is not confirmed" do
154+
it "returns 403 and does not create a session" do
144155
expect {
145-
post :create, params: { session: { title: ''} }
156+
post :create, params: { session: { title: "new title", description: "new description", category_ids: [category.id], level_id: "2" } }
146157
}.not_to change { Session.count }
147-
expect(response).to render_template('new')
158+
expect(response).to have_http_status(403)
148159
end
149160
end
150-
151161
end
152162
end

spec/models/participant_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,28 @@
5252
end
5353
end
5454

55+
describe "destroying" do
56+
let!(:participant) { create(:participant) }
57+
let!(:event) { create(:event) }
58+
59+
it "is prevented when the participant has sessions" do
60+
create(:session, participant: participant, event: event)
61+
expect { participant.destroy }.not_to change { Participant.count }
62+
expect(participant.errors[:base]).to include(/Cannot delete record because dependent sessions exist/)
63+
end
64+
65+
it "is prevented when the participant only has canceled sessions" do
66+
session = create(:session, participant: participant, event: event)
67+
session.update!(canceled_at: Time.current)
68+
expect { participant.destroy }.not_to change { Participant.count }
69+
expect(participant.errors[:base]).to include(/Cannot delete record because dependent sessions exist/)
70+
end
71+
72+
it "is allowed when the participant has no sessions" do
73+
expect { participant.destroy }.to change { Participant.count }.by(-1)
74+
end
75+
end
76+
5577
describe '#attending session' do
5678
let!(:session1) { create(:session) }
5779
let!(:joe) { create(:joe) }

0 commit comments

Comments
 (0)