-
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?
Prevent upvoting or downvoting multiple times on the same restroom #520
Conversation
Thank you for this! Hopefully @mi-wood or @tkwidmer can take a look at this, since they have dealt with this part of the code more than me. But on first look, this appears pretty good! I'm not familiar with the Rails session concept, so I'll perhaps take a look into that before merging. In any case... Happy Hacktoberfest! 🎃 💻 ✨ 🍂 |
Here are some resources about the Rails session feature... Tutorials, explainers and how-to's: Official docs: |
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.
Thanks for the pull request! I've left a few comments about reducing the complexity of the _vote_for_restroom
method. Other than that, this look good!
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Can we get rid of the if session[:voted_for]
and just automatically create that at the beginning? It would be premature, but also reduce the complexity of these conditionals.
@@ -76,6 +76,15 @@ def find_restroom | |||
@restroom = Restroom.find(params[:id]) | |||
end | |||
|
|||
def _vote_for_restroom(up_or_downvote) |
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
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 comment
The 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.
Context
Summary of Changes
session
and prevent tallying further votes for that restroomConcerns
Checklist
Screenshots
After