Skip to content

Commit

Permalink
Feat/users/add original city reference (#365)
Browse files Browse the repository at this point in the history
* feat(users): add original_city reference to users

* feat(users) add original_city to user on creation

* feat(users): remove disable from city select input

* feat(users): remove user validation city_cannot_be_changed_if_present

* fix(users): move existing user original_city assignation from migration to self.from_kitt
  • Loading branch information
wJoenn authored Nov 27, 2023
1 parent e82475e commit 478affd
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/components/user_form_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<div class="flex flex-col gap-y-1 sm:flex-row sm:items-center sm:gap-x-3">
<%= f.label :campus_name, "Campus" %>
<%= f.collection_select(:city_id, City.order(:vanity_name), :id, :vanity_name, { prompt: "", selected: @user.city_id }, class: "input", disabled: @user.city_id?) %>
<%= f.collection_select(:city_id, City.order(:vanity_name), :id, :vanity_name, { prompt: "", selected: @user.city_id }, class: "input") %>
</div>

<div class="flex flex-col gap-y-1 sm:flex-row sm:items-center sm:gap-x-3">
Expand Down
1 change: 1 addition & 0 deletions app/models/city.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class City < ApplicationRecord
has_many :city_scores, class_name: "Cache::CityScore", dependent: :delete_all

has_many :users, dependent: :nullify
has_many :original_users, class_name: "User", dependent: :nullify
has_many :completions, through: :users

before_create :set_default_vanity_name
Expand Down
9 changes: 3 additions & 6 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class User < ApplicationRecord

belongs_to :batch, optional: true
belongs_to :city, optional: true, touch: true
belongs_to :original_city, class_name: "City", inverse_of: :original_users, optional: true
belongs_to :squad, optional: true, touch: true
belongs_to :referrer, class_name: "User", optional: true

Expand All @@ -30,7 +31,6 @@ class User < ApplicationRecord
validates :favourite_language, inclusion: { in: Snippet::LANGUAGES.keys.map(&:to_s) }, allow_nil: true

validate :batch_cannot_be_changed, on: :update, if: :batch_id_changed?
validate :city_cannot_be_changed_if_present, on: :update, if: :city_id_changed?
validate :referrer_must_exist, on: :update, if: :referrer_id_changed?
validate :referrer_cannot_be_self, on: :update

Expand All @@ -55,7 +55,7 @@ class User < ApplicationRecord
before_validation :assign_private_leaderboard, on: :create

def self.from_kitt(auth)
oldest_batch = auth.info&.schoolings&.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
Expand All @@ -65,6 +65,7 @@ def self.from_kitt(auth)
end

user.github_username = auth.info.github_nickname
user.original_city = City.find_or_initialize_by(name: oldest_batch&.city&.name)

user.save
user
Expand Down Expand Up @@ -135,10 +136,6 @@ def batch_cannot_be_changed
errors.add(:batch, "can't be changed")
end

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

def referrer_must_exist
errors.add(:referrer, "must exist") unless User.exists?(referrer_id)
end
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20231127171224_add_original_city_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class AddOriginalCityToUsers < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def change
add_reference :users, :original_city, index: { algorithm: :concurrently }
add_foreign_key :users, :cities, column: :original_city_id, validate: false
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class ValidateForeignKeyForUsersOriginalCityRelationship < ActiveRecord::Migration[7.1]
def change
validate_foreign_key :users, :cities
end
end
7 changes: 5 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2023_11_19_105530) do
ActiveRecord::Schema[7.1].define(version: 2023_11_27_171245) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
enable_extension "pgcrypto"
Expand Down Expand Up @@ -374,9 +374,10 @@
t.bigint "city_id"
t.datetime "created_at", null: false
t.boolean "entered_hardcore", default: false, null: false
t.string "favourite_language"
t.integer "event_awareness"
t.string "favourite_language"
t.string "github_username"
t.bigint "original_city_id"
t.string "private_leaderboard"
t.string "provider"
t.bigint "referrer_id"
Expand All @@ -393,6 +394,7 @@
t.index ["aoc_id"], name: "index_users_on_aoc_id", unique: true
t.index ["batch_id"], name: "index_users_on_batch_id"
t.index ["city_id"], name: "index_users_on_city_id"
t.index ["original_city_id"], name: "index_users_on_original_city_id"
t.index ["referrer_id"], name: "index_users_on_referrer_id"
end

Expand All @@ -410,5 +412,6 @@
add_foreign_key "squad_scores", "squads"
add_foreign_key "users", "batches"
add_foreign_key "users", "cities"
add_foreign_key "users", "cities", column: "original_city_id"
add_foreign_key "users", "users", column: "referrer_id"
end

0 comments on commit 478affd

Please sign in to comment.