From 7d6c00ff6135af0eed459bbb640210ac074317f2 Mon Sep 17 00:00:00 2001 From: translatorzepp <3465940+translatorzepp@users.noreply.github.com> Date: Thu, 18 Oct 2018 23:51:37 -0500 Subject: [PATCH] Prevent upvoting or downvoting multiple times on the same restroom --- app/controllers/restrooms_controller.rb | 13 +++++- config/locales/en/restroom.en.yml | 1 + config/locales/es/restroom.es.yml | 1 + spec/controllers/restrooms_controller_spec.rb | 45 ++++++++++++++++--- 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/app/controllers/restrooms_controller.rb b/app/controllers/restrooms_controller.rb index 0e440333..b0ed3498 100644 --- a/app/controllers/restrooms_controller.rb +++ b/app/controllers/restrooms_controller.rb @@ -39,9 +39,9 @@ def create def update if params[:restroom][:downvote] - Restroom.increment_counter(:downvote, @restroom.id) + _vote_for_restroom(:downvote) elsif params[:restroom][:upvote] - Restroom.increment_counter(:upvote, @restroom.id) + _vote_for_restroom(:upvote) elsif @restroom.update(permitted_params) flash[:notice] = I18n.t('restroom.flash.updated') else @@ -76,6 +76,15 @@ def find_restroom @restroom = Restroom.find(params[:id]) end + def _vote_for_restroom(up_or_downvote) + if session[:voted_for] and session[:voted_for].include? @restroom.id + flash[:notice] = I18n.t('restroom.flash.alreadyvoted') + else + Restroom.increment_counter(up_or_downvote, @restroom.id) + session[:voted_for] = session[:voted_for] ? session[:voted_for].push(@restroom.id) : [@restroom.id] + end + end + def permitted_params params.require(:restroom).permit( :name, diff --git a/config/locales/en/restroom.en.yml b/config/locales/en/restroom.en.yml index 8978c2be..416c06fe 100644 --- a/config/locales/en/restroom.en.yml +++ b/config/locales/en/restroom.en.yml @@ -45,6 +45,7 @@ en: upvotesuccess: 'This restroom has been upvoted! Thank you for contributing to our community.' downvoteerror: 'There was an unexpected problem downvoting this post.' downvotesuccess: 'This restroom has been downvoted! Thank you for contributing to our community.' + alreadyvoted: 'You have already voted for this restroom.' new: 'A new restroom entry has been created for %{name}.' updated: 'This restroom entry has been updated' deleted: 'This restroom entry has been deleted' diff --git a/config/locales/es/restroom.es.yml b/config/locales/es/restroom.es.yml index 85137216..6494b03a 100644 --- a/config/locales/es/restroom.es.yml +++ b/config/locales/es/restroom.es.yml @@ -45,6 +45,7 @@ es: upvotesuccess: '¡Este baño se ha calificado en positivo! Gracias por contribuir a nuestra comunidad.' downvoteerror: 'Se generó un problema inesperado con su calificación negativa.' downvotesuccess: '¡Este baño se ha calificado en negativo! Gracias por contribuir a nuestra comunidad.' + alreadyvoted: 'Ya calificó a este baño.' new: 'Una nueva entrada de baño se ha creado para %{name}.' updated: 'Esta entrada de baño ha sido actualizada' deleted: 'Esta entrada de baño ha sido borrada' diff --git a/spec/controllers/restrooms_controller_spec.rb b/spec/controllers/restrooms_controller_spec.rb index 686a5b98..082d7605 100644 --- a/spec/controllers/restrooms_controller_spec.rb +++ b/spec/controllers/restrooms_controller_spec.rb @@ -8,31 +8,62 @@ context "voting" do let(:restroom) { FactoryBot.create(:restroom) } - - it "should downvote" do - post_params = { + let(:post_params_downvote) { + { id: restroom.id, restroom: { downvote: true } } + } + let(:post_params_upvote) { + { + id: restroom.id, + restroom: { + upvote: true + } + } + } + it "should downvote" do expect { - post :update, params: post_params + post :update, params: post_params_downvote }.to change { restroom.reload.downvote }.by 1 end it "should upvote" do + expect { + post :update, params: post_params_upvote + }.to change { restroom.reload.upvote }.by 1 + end + + it "shouldn't upvote or downvote twice" do + post :update, params: post_params_upvote + + expect { + post :update, params: post_params_upvote + }.not_to change { restroom.reload.upvote } + + expect { + post :update, params: post_params_downvote + }.not_to change { restroom.reload.downvote } + end + + it "should allow you to vote for multiple restrooms" do + session[:voted_for] = [restroom.id] + restroom_two = FactoryBot.create(:restroom) + post_params = { - id: restroom.id, + id: restroom_two.id, restroom: { - upvote: true + downvote: true } } expect { post :update, params: post_params - }.to change { restroom.reload.upvote }.by 1 + }.to change { restroom_two.reload.downvote }.by 1 + expect(session[:voted_for]).to match_array([restroom.id, restroom_two.id]) end end end