Skip to content

Commit

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

if current_user.batch_id.nil? && form_params[:city_id] # rubocop:disable Style/IfUnlessModifier
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

Expand Down
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class User < ApplicationRecord
validates :aoc_id, uniqueness: { allow_nil: true }
validates :username, presence: true

validate :city_cant_change
validate :not_referring_self
validate :referrer_exists

Expand Down Expand Up @@ -98,6 +99,10 @@ def referrer_code

private

def city_cant_change
errors.add(:city, "can't change") if batch_changed? && batch_id_was.present?
end

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

0 comments on commit 3290ed7

Please sign in to comment.