Skip to content

Commit

Permalink
feat(users): validate batch can't change
Browse files Browse the repository at this point in the history
  • Loading branch information
wJoenn committed Nov 12, 2023
1 parent 3290ed7 commit 5cf2a0a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
12 changes: 4 additions & 8 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,9 @@ def updated_params
username: form_params[:username]
}.compact

if form_params[:city_id] # rubocop:disable Style/IfUnlessModifier
params[:batch] = Batch.find_or_create_by(number: nil, city_id: form_params[:city_id])
end

if form_params[:referrer_code] # rubocop:disable Style/IfUnlessModifier
params[:referrer_id] = User.find_by_referral_code(form_params[:referrer_code])&.id.to_i
end
params[:batch] = nil if form_params[:batch_number]
params[:batch] = Batch.find_or_create_by(number: nil, city_id: form_params[:city_id]) if form_params[:city_id]
params[:referrer_id] = User.find_by_referral_code(form_params[:referrer_code])&.id.to_i if form_params[:referrer_code]

params
end
Expand All @@ -88,6 +84,6 @@ def unlock_achievements
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)
end
end
6 changes: 3 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class User < ApplicationRecord
validates :aoc_id, uniqueness: { allow_nil: true }
validates :username, presence: true

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

Expand Down Expand Up @@ -99,8 +99,8 @@ def referrer_code

private

def city_cant_change
errors.add(:city, "can't change") if batch_changed? && batch_id_was.present?
def batch_cant_change
errors.add(:batch, " or City can't be changed") if batch_changed? && batch_id_was.present?
end

def not_referring_self
Expand Down

0 comments on commit 5cf2a0a

Please sign in to comment.