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 5 commits
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
2 changes: 1 addition & 1 deletion Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
web: bin/rails server -p 3000 -b 0.0.0.0
web: bin/rails server -p 3000
pil0u marked this conversation as resolved.
Show resolved Hide resolved
css: bin/rails tailwindcss:watch
worker: bundle exec good_job start
12 changes: 8 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,25 @@ def restrict_after_lock
end

def updated_params
{
params = {
accepted_coc: form_params[:accepted_coc],
aoc_id: form_params[:aoc_id],
entered_hardcore: form_params[:entered_hardcore],
username: form_params[:username],
city_id: form_params[:city_id],
referrer: User.find_by_referral_code(form_params[:referrer_code])
city_id: form_params[:city_id]
}.compact

params[:batch] = nil if form_params[:batch_number]
wJoenn marked this conversation as resolved.
Show resolved Hide resolved
params[:referrer_id] = User.find_by_referral_code(form_params[:referrer_code])&.id.to_i if form_params[:referrer_code]
pil0u marked this conversation as resolved.
Show resolved Hide resolved

params
end

def unlock_achievements
Achievements::UnlockJob.perform_later(:city_join, current_user.id)
end

def form_params
params.require(:user).permit(:accepted_coc, :aoc_id, :entered_hardcore, :username, :city_id, :referrer_code)
params.require(:user).permit(:accepted_coc, :aoc_id, :entered_hardcore, :username, :batch_number, :city_id, :referrer_code)
pil0u marked this conversation as resolved.
Show resolved Hide resolved
end
end
34 changes: 20 additions & 14 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class User < ApplicationRecord
has_many :achievements, dependent: :destroy
has_many :referees, class_name: "User", inverse_of: :referrer, dependent: :nullify

validates :aoc_id, numericality: { in: 1...(2**31), message: "should be between 1 and 2^31" }, allow_nil: true
pil0u marked this conversation as resolved.
Show resolved Hide resolved
validates :aoc_id, uniqueness: { allow_nil: true }
validates :username, presence: true

validates :private_leaderboard, presence: true
validates :username, :private_leaderboard, presence: true
pil0u marked this conversation as resolved.
Show resolved Hide resolved

validate :city_cant_change
validate :batch_cant_change
validate :not_referring_self
validate :referrer_exists

scope :admins, -> { where(uid: ADMINS.values) }
scope :confirmed, -> { where(accepted_coc: true, synced: true).where.not(aoc_id: nil) }
Expand All @@ -39,27 +39,21 @@ class User < ApplicationRecord
before_validation :assign_private_leaderboard, on: :create

def self.from_kitt(auth)
batches = auth.info&.schoolings
oldest_batch = batches.min_by { |batch| batch.camp.starts_at }
oldest_batch = auth.info&.schoolings&.min_by { |batch| batch.camp.starts_at }

user = find_or_initialize_by(provider: auth.provider, uid: auth.uid) do |u|
u.username = auth.info.github_nickname
u.batch = Batch.find_or_initialize_by(number: oldest_batch&.camp&.slug.to_i)
u.city = City.find_or_initialize_by(name: oldest_batch&.city&.name)
end

user.github_username = auth.info.github_nickname

user.save
user.update(github_username: auth.info.github_nickname)
pil0u marked this conversation as resolved.
Show resolved Hide resolved

user
end

def self.find_by_referral_code(code)
uid = code&.gsub(/R0*/, "")&.to_i
return if uid.nil?

User.find_by(uid:)
User.find_by(uid: code.gsub(/R0*/, "").to_i)
pil0u marked this conversation as resolved.
Show resolved Hide resolved
end

def admin?
Expand Down Expand Up @@ -113,8 +107,20 @@ def referrer_code

private

def batch_cant_change
pil0u marked this conversation as resolved.
Show resolved Hide resolved
errors.add(:batch, "can't be changed") if batch_changed? && batch_id_was.present?
end

def city_cant_change
pil0u marked this conversation as resolved.
Show resolved Hide resolved
errors.add(:city, "can't be changed") if city_changed? && city_id_was.present?
end

def not_referring_self
errors.add(:referrer, "must not be you") if referrer == self
errors.add(:referrer, "can't be you") if referrer == self
end

def referrer_exists
errors.add(:referrer, "must exist") if referrer_id == 0
end

def assign_private_leaderboard
Expand Down
6 changes: 4 additions & 2 deletions spec/presenters/scores/user_scores_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@

context "when user has no city" do
before do
user_1.update!(city: nil)
user_1.city = nil
user_1.save(validate: false)
end

it "includes no info about it in the output" do
Expand All @@ -166,7 +167,8 @@

context "when user has no batch" do
before do
user_1.update!(batch: nil)
user_1.batch = nil
user_1.save(validate: false)
end

it "includes no info about it in the output" do
Expand Down