-
Notifications
You must be signed in to change notification settings - Fork 260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent upvoting or downvoting multiple times on the same restroom #520
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we get rid of the |
||
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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, this would become a lot easier to read and would probably fix the code climate error. |
||
end | ||
end | ||
|
||
def permitted_params | ||
params.require(:restroom).permit( | ||
:name, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might make sense to break this into a service and reduce the complexity of the controller/tests. There's an example here: https://github.com/RefugeRestrooms/refugerestrooms/tree/develop/app/services