-
Notifications
You must be signed in to change notification settings - Fork 5
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
feat(users): validation flashes for batch city and referrer #318
Conversation
3b8dc54
to
5cf2a0a
Compare
@wJoenn Un petit rebase de |
@pil0u Le model de User commence faire plus de 100 lignes ce qui trigger Rubocop. |
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.
Si Rubocop est relou avec la longueur de fichier, soit on ajuste Rubocop pour qu'il nous laisse tranquille, soit on bouge des bouts de code ailleurs, mais partir sur des one-liners n'est pas la bonne approche. On ne rend pas le code moins lisible (voire changer la logique) pour plaire au linter 😅
@wJoenn Pour RuboCop, tu peux ajouter une exception pour cette règle, pour ce fichier là 👍 |
C'est un peu confu les validations donc je vais essayer de reprendre ca point par point. Batch
def updated_params
params = {
batch: Batch.find_by(number: form_params[:batch_number])
}.compact
params
end
def form_params
params.require(:user).permit(:batch_number)
end
def updated_params
params = {
batch_id: Batch.find_by(number: form_params[:batch_number])&.id.to_i
}.compact
params
end
def updated_params
params = {
batch_id: (Batch.find_by(number: form_params[:batch_number])&.id.to_i if form_params[:batch_number].present?)
}.compact
params
end
def batch_cannot_change
errors.add(:batch, "can't be changed") if batch_changed?
end
def batch_cannot_change
errors.add(:batch, "can't be changed") if batch_changed? && batch_id_was.present?
end
def updated_params
params = {
}.compact
params[:batch] = nil if form_params[:batch_number].present?
params
end Referrer
def updated_params
params = {
referrer: User.find_by_referral_code(form_params[:referrer_code])
}.compact
params
end
def form_params
params.require(:user).permit(:referrer_code)
end
```ruby
def updated_params
params = {
referrer_id: User.find_by_referral_code(form_params[:referrer_code])&.id.to_i
}.compact
params
end
def referrer_exists
errors.add(:referrer, "must exist") if referrer_id == 0
end
def updated_params
params = {
referrer_id: (User.find_by_referral_code(form_params[:referrer_code])&.id.to_i if form_params[:referrer_code].present?)
}.compact
params
end |
86e286b
to
541c570
Compare
…rrer_code unpermitted) and drop the flash message for the batch, it's not worth the effort (after a looooong thought and tests). Forbidding City update is important (and easy) because scoring depends on it. Displaying a proper error message when a user tries a wrong referral code is worth it.
@wJoenn J'ai testé le dernier code que tu as push, malheureusement il ne fonctionnait pas tout à fait - j'obtenais une erreur sur le batch quand j'appuyais sur "SAVE", le referral code s'enlevait pas si on laissait vide après avoir entré un code valide... Beaucoup de discussions et d'aller-retour pour un truc si micro 🥵 Le bilan :
|
app/models/user.rb
Outdated
@@ -110,7 +110,7 @@ def referrer_code | |||
referrer&.referral_code | |||
end | |||
|
|||
def assign_referrer(referral_code) | |||
def referrer_valid?(referral_code) |
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.
referrer_valid?
ne sous entend pas que le referrer sera update.
Pour moi le naming est pas correct ici
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.
Suggestions ?
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.
Je suggère qu'on ne permette pas aux users de reset leur referrer a nil si ils en ont deja un de valide et qu'on remette cette validation comme elle etait avant ton commit 😅
@pil0u by doing this you end up with a id of current_user.referrer_id = User.find_by_referral_code(referrer_code)&.id if referrer_code If a User has no referrer yet it means its By using this instead current_user.referrer_id = User.find_by_referral_code(referrer_code)&.id.to_i if referrer_code We update from |
@wJoenn La conversion en Une solution alternative que je venais de terminer, c'est ça : # User model
def assign_and_validate_referrer_with(referral_code)
# Do nothing and return true if no referral code is provided
return true if referral_code.nil?
if referral_code == ""
errors.add(:referrer, "can't be blank")
return false
end
referrer = User.find_by_referral_code(referral_code)
if referrer.nil? # not found
errors.add(:referrer, "must exist")
return false
end
if referrer == self
errors.add(:referrer, "can't be you (nice try!)")
return false
end
self.referrer_id = referrer.id
true
end
# user controller
def update
current_user.batch_id = -1 if params.dig(:user, :batch_number) # Set impossible value to trigger validation
is_referrer_valid = current_user.assign_and_validate_referrer_with(params.dig(:user, :referrer_code))
if is_referrer_valid && current_user.update(user_params)
unlock_achievements
redirect_back fallback_location: "/", notice: "Your user information was updated"
else
redirect_back fallback_location: "/", alert: current_user.errors.full_messages[0].to_s
end
end En gros je créais une méthode d'instance qui gère l'assignation et la validation du referrer. J'en avais profité pour y intégrer toutes les validations. Je mets ça pour la postérité, si @Aquaj passe par là il aura sûrement un œil d'expert que je n'ai pas sur ce sujet |
Je trouve ca bizarre de faire une méthode qui s'occupe de faire une assignation, une validation et une confirmation en meme temps perso. |
current_user.batch_id = -1 if params.dig(:user, :batch_number) # Set impossible value to trigger validation | ||
|
||
referrer_code = params.dig(:user, :referrer_code) | ||
current_user.referrer_id = User.find_by_referral_code(referrer_code)&.id || -1 if referrer_code | ||
|
||
if current_user.update(user_params) |
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.
current_user.batch_id = -1 if params.dig(:user, :batch_number) # Set impossible value to trigger validation | |
referrer_code = params.dig(:user, :referrer_code) | |
current_user.referrer_id = User.find_by_referral_code(referrer_code)&.id || -1 if referrer_code | |
if current_user.update(user_params) | |
referrer_code = params.dig(:user, :referrer_code) | |
if referrer_code | |
current_user.referrer_id = User.find_by_referral_code(referrer_code)&.id | |
end | |
if current_user.update(user_params) |
On a pas besoin de ces histoires de -1
avec les validations dans User
(faudrait juste rajouter referral_code
) il me semble. Quelque chose m'échappe ?
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.
Si un user ouvre le DevTool et enleve manuellement le disable du bouton pour le batch, rentre la valeur qu'il veut et submit, on veut quand meme pouvoir lui mettre un message d'erreur.
D'ou
current_user.batch_id = -1 if params.dig(:user, :batch_number)
Et si un user veut changer son referrer pendant le setup il a besoin de pouvoir rentré un referral_code, d'ou
referrer_code = params.dig(:user, :referrer_code)
current_user.referrer_id = User.find_by_referral_code(referrer_code)&.id if referrer_code
Sauf que si il rentre un code erroné on veut quand meme pouvoir validé, donc on ajoute || -1
si le code est erroné
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.
Si un user ouvre le DevTool et enleve manuellement le disable du bouton pour le batch, rentre la valeur qu'il veut et submit, on veut quand meme pouvoir lui mettre un message d'erreur.
Il se ferait tej par cette validation normalement.
Sauf que si il rentre un code erroné on veut quand meme pouvoir validé, donc on ajoute || -1 si le code est erroné
Ça en effet, mais on pourrait mieux le gérer via un attribute :referrer_code
qui n'existe pas en DB mais sert à ce genre de validations.
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.
Il se ferait tej par cette validation normalement.
Non parce que le param est pas permitted.
On trouvais pas ca logiquer de permit un param qu'on va jamais autoriser alors plutot que de l'ajouter dans le require.permit
on s'est dit que ca faisait plus de sens de le mettre dans l'update .
Et du coup quitte a l'assigner a qqch Pilou preferait mettre -1
pour que ca soit bien clair que c'etait impossible
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.
on pourrait mieux le gérer via un attribute :referrer_code
pas la ref
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.
C'est pas beau, j'ai pas trouvé mieux dans le temps imparti 👀
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.
Pas de soucis. Je verrai bien plus tard si on peut clean ça.
Summary of changes and context
closes #307
"Batch or City can't be changed" if user tries to edit their
batch_number
"Batch or City can't be changed" if user has a batch and tries to edit their
city_id
"Referrer must exist" if user inputs a incorrect
referral_code
Sanity checks