Skip to content
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

Merged
merged 22 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ec82105
feat(users): validate that referrer exists
wJoenn Nov 12, 2023
3290ed7
feat(users): validate city can't change
wJoenn Nov 12, 2023
5cf2a0a
feat(users): validate batch can't change
wJoenn Nov 12, 2023
3233fff
Merge branch 'main' into feat/users/validation-flashes
wJoenn Nov 18, 2023
b296a59
fix(users): rspec
wJoenn Nov 18, 2023
3eeec6b
fix(users): revert refactos
wJoenn Nov 18, 2023
d24e518
fix(users) disable Metrics/ClassLength for user model
wJoenn Nov 18, 2023
ff2ea49
fix(users): rename cant to cannot
wJoenn Nov 18, 2023
73770ee
fix(users): move referrer param inside params
wJoenn Nov 18, 2023
4663981
feat(users): add explanation for params[:batch_id] = nil if form_para…
wJoenn Nov 18, 2023
541c570
refactor(users): validations params
wJoenn Nov 18, 2023
9f6d5dd
Spacing
pil0u Nov 18, 2023
03e1596
Handle referral_code in a proper update_referrer method (we keep refe…
pil0u Nov 19, 2023
daf8cda
Add back the batch validation
pil0u Nov 19, 2023
c9154b6
Rename update to assign referrer
pil0u Nov 19, 2023
8bba6d3
Wording
pil0u Nov 19, 2023
2d8ec04
Rename, again
pil0u Nov 19, 2023
2cc40d4
Trigger batch/city validations on update
pil0u Nov 19, 2023
6f7ad13
Rollback to the previous behaviour: referrer cannot be nil after it w…
pil0u Nov 19, 2023
ba79750
Test pass, but behaviour is not as expected
pil0u Nov 19, 2023
a065fb6
fix(users): add #to_i to referrer_id to trigger change even if user h…
wJoenn Nov 19, 2023
1f97ead
Explicitly use -1 if referrer is not found
pil0u Nov 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def edit

def update
referral_code = params.dig(:user, :referrer_code)
current_user.batch_id = -1 if params.dig(:user, :batch_number) # Change with impossible value to trigger validation

if current_user.update_referrer(referral_code) && current_user.update(user_params)
unlock_achievements
Expand Down
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class User < ApplicationRecord
validates :username, presence: true
validates :private_leaderboard, presence: true

validate :batch_cannot_be_changed, if: :batch_id_changed?
validate :city_cannot_be_changed_if_present, if: :city_id_changed?
validate :referrer_cannot_be_self

Expand Down Expand Up @@ -134,6 +135,10 @@ def update_referrer(referral_code)

private

def batch_cannot_be_changed
errors.add(:batch, "can't be changed")
end
pil0u marked this conversation as resolved.
Show resolved Hide resolved

def city_cannot_be_changed_if_present
errors.add(:city, "can't be changed") if city_id_was.present?
end
Expand Down