Skip to content

Commit

Permalink
Merge pull request #315 from minnestar/mdecuir/fix-presentations-cont…
Browse files Browse the repository at this point in the history
…roller-tests

Update presentations controller logic and spec
  • Loading branch information
pcantrell authored Jun 9, 2024
2 parents b081997 + f92eab3 commit a590e3f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 27 deletions.
48 changes: 31 additions & 17 deletions src/app/controllers/presentations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,41 @@ def index
end

def create
participant = Participant.find(params[:id])
begin
participant = if participant_params[:id].present?
Participant.find(participant_params[:id])
elsif participant_params[:name].present?
participant = Participant.where(name: participant_params[:name])&.first
end

if participant.nil?
flash[:error] = "Sorry, no presenter named #{params[:name]} was found. Please try again."
redirect_to session_presentations_path(@session)
return
elsif participant.signed_code_of_conduct_for_current_event? == false
flash[:error] = "Sorry, #{params[:name]} hasn't signed the current Code of Conduct."
redirect_to session_presentations_path(@session)
return
end
if participant.nil?
flash[:error] = "Sorry, no presenter #{participant_params[:name] ? "matching '#{participant_params[:name]}' " : "" }was found. Please try again."
redirect_to session_presentations_path(@session)
return
elsif participant.signed_code_of_conduct_for_current_event? == false
flash[:error] = "Sorry, #{participant.name} hasn't signed the current Code of Conduct."
redirect_to session_presentations_path(@session)
return
end

presentation = @session.presentations.new
presentation.participant = participant
presentation = @session.presentations.new
presentation.participant = participant

if presentation.save
flash[:notice] = "Presenter added."
redirect_to session_presentations_path(@session)
else
flash[:error] = "There was an error adding the presenter. Please try again. If this keeps happening, please contact [email protected]."
if presentation.save
flash[:notice] = "Presenter added."
redirect_to session_presentations_path(@session)
else
raise StandardError
end
rescue => e
flash[:error] = "There was an error adding the presenter. Please try again. If this keeps happening, please contact [email protected]."
redirect_to session_presentations_path(@session)
end
end

private

def participant_params
params.permit(:session_id, :id, :name)
end
end
51 changes: 41 additions & 10 deletions src/spec/controllers/presentations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

describe PresentationsController do
let(:session) { create(:session) }
let(:participant) { create(:joe) }

describe "#index" do
it "should be successful" do
Expand All @@ -13,38 +14,68 @@
end

describe "#create" do
context "when the user exists" do
before do
create(:event)
create(:joe)
before do
create(:event)
end

context "when the user is found by id" do
it "should be successful when the user has signed the code of conduct" do
CodeOfConductAgreement.create!({
participant_id: participant.id,
event_id: Event.current_event.id,
})

expect {
post :create, params: { session_id: session, id: participant.id }
}.to change { session.presentations.count }.by(1)
expect(response).to redirect_to session_presentations_path(session)
expect(flash[:notice]).to eq "Presenter added."
end

it 'is unsuccessful when the user hasnt signed the code of conduct' do
post :create, params: { session_id: session, id: participant.id }
expect(response).to redirect_to session_presentations_path(session)
expect(flash[:error]).to match(/hasn\'t signed the current Code of Conduct/)
end
end

context "when the user is found by name" do
it "should be successful when the user has signed the code of conduct" do
CodeOfConductAgreement.create!({
participant_id: Participant.where(name: 'Joe Schmoe').first.id,
participant_id: participant.id,
event_id: Event.current_event.id,
})

expect {
post :create, params: { session_id: session, name: 'Joe Schmoe' }
post :create, params: { session_id: session, name: participant.name }
}.to change { session.presentations.count }.by(1)
expect(response).to redirect_to session_presentations_path(session)
expect(flash[:notice]).to eq "Presenter added."
end

it 'is unsuccessful when the user hasnt signed the code of conduct' do
post :create, params: { session_id: session, name: 'Joe Schmoe' }
post :create, params: { session_id: session, name: participant.name }
expect(response).to redirect_to session_presentations_path(session)
expect(flash[:error]).to match(/hasn\'t signed the Code of Conduct for this event/)
expect(flash[:error]).to match(/hasn\'t signed the current Code of Conduct/)
end
end

context "when the user is not found" do
context "when the user name is not found" do
it "should set a flash message" do
expect {
post :create, params: { session_id: session, name: 'Grace Hopper' }
}.not_to change { session.presentations.count }
expect(flash[:error]).to eq "Sorry, no presenter named 'Grace Hopper' was found. Please try again."
expect(flash[:error]).to eq "Sorry, no presenter matching 'Grace Hopper' was found. Please try again."
expect(response).to redirect_to session_presentations_path(session)
end
end

context "when the user id is not found" do
it "should set a flash message" do
expect {
post :create, params: { session_id: session, id: 0 }
}.not_to change { session.presentations.count }
expect(flash[:error]).to eq "There was an error adding the presenter. Please try again. If this keeps happening, please contact [email protected]."
expect(response).to redirect_to session_presentations_path(session)
end
end
Expand Down

0 comments on commit a590e3f

Please sign in to comment.