-
Notifications
You must be signed in to change notification settings - Fork 5
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
Plug Batch info on Kitt callback and move back City on User to simplify validation logic. #325
Changes from 2 commits
c5f09b0
4850c9a
0a6f168
7e56926
52b3889
2b55247
f7205a4
837b0bc
07e6384
828396b
194c053
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,18 +86,13 @@ def restrict_after_lock | |
end | ||
|
||
def updated_params | ||
batch = nil | ||
|
||
if current_user.batch_id.nil? && form_params[:city_id] # rubocop:disable Style/IfUnlessModifier | ||
batch = Batch.find_or_create_by(number: nil, city_id: form_params[:city_id]) | ||
end | ||
|
||
{ | ||
accepted_coc: form_params[:accepted_coc], | ||
aoc_id: form_params[:aoc_id], | ||
entered_hardcore: form_params[:entered_hardcore], | ||
username: form_params[:username], | ||
batch_id: batch&.id, | ||
batch_id: Batch.find_by(number: form_params[:batch_number])&.id, | ||
city_id: form_params[:city_id], | ||
referrer: User.find_by_referral_code(form_params[:referrer_code]) | ||
}.compact | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as previous comment, the batch should never be updated by the user event if he has none. |
||
end | ||
|
@@ -107,6 +102,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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
class Batch < ApplicationRecord | ||
has_many :users, dependent: :nullify | ||
belongs_to :city | ||
belongs_to :city, optional: true | ||
|
||
validates :number, numericality: { in: 1...(2**31), message: "should be between 1 and 2^31" }, allow_nil: true | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Si on garde une reference a city sur le batch alors je comprend pas comment un batch pourrait se retrouver sans city There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C'était surtout pour le système de choix de batch en cas de batch manquant sur Kitt. (Pcq du coup l'user choisirait un batch au choix, potentiellement un qu'on connaît pas et donc qui a pas de ville) Mais si c'est pas nécessaire, je vais faire sauter ça. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,11 +7,11 @@ class User < ApplicationRecord | |
CONTRIBUTORS = { pilou: "6788", aquaj: "449", louis: "19049", aurelie: "9168" }.freeze | ||
|
||
belongs_to :batch, optional: true | ||
belongs_to :city, optional: true | ||
has_one :batch_city, through: :batch, source: :city | ||
pil0u marked this conversation as resolved.
Show resolved
Hide resolved
|
||
belongs_to :squad, optional: true, touch: true | ||
belongs_to :referrer, class_name: "User", optional: true | ||
|
||
delegate :city, :city_id, to: :batch, allow_nil: true | ||
|
||
has_many :completions, dependent: :destroy | ||
has_many :user_day_scores, class_name: "Cache::UserDayScore", dependent: :delete_all | ||
has_many :solo_points, class_name: "Cache::SoloPoint", dependent: :delete_all | ||
|
@@ -28,6 +28,8 @@ class User < ApplicationRecord | |
validates :username, presence: true | ||
|
||
validate :not_referring_self | ||
validate :not_changing_cities | ||
validate :city_must_match_batch | ||
|
||
scope :admins, -> { where(uid: ADMINS.values) } | ||
scope :confirmed, -> { where(accepted_coc: true, synced: true).where.not(aoc_id: nil) } | ||
|
@@ -37,13 +39,24 @@ class User < ApplicationRecord | |
after_create :assign_private_leaderboard | ||
|
||
def self.from_kitt(auth) | ||
user = where(provider: auth.provider, uid: auth.uid).first_or_create do |u| | ||
batches = auth.info&.schoolings | ||
oldest_batch = batches.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.github_username = auth.info.github_nickname | ||
u.batch_id = Batch.find_or_create_by(number: auth.info.last_batch_slug.to_i).id | ||
|
||
u.batch = Batch.find_or_initialize_by(number: oldest_batch&.camp&.slug) do |b| | ||
city = oldest_batch&.city | ||
b.city = City.find_or_initialize_by(name: city&.slug, vanity_name: city&.name) | ||
end | ||
Aquaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
u.city = u.batch.city | ||
end | ||
|
||
user.update(github_username: auth.info.github_nickname) | ||
user.github_username = auth.info.github_nickname | ||
|
||
user.save | ||
|
||
user | ||
end | ||
|
||
|
@@ -101,6 +114,18 @@ def not_referring_self | |
errors.add(:referrer, "must not be you") if referrer == self | ||
end | ||
|
||
def not_changing_cities | ||
return if city_id_was.blank? | ||
|
||
errors.add(:city_id, "can't be modified") if city_id_changed? | ||
end | ||
|
||
def city_must_match_batch | ||
return unless batch&.city_id? | ||
|
||
errors.add(:city_id, "must match batch city") if city_id != batch.city_id | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Qu'ets ce qui se passe si tu as une city mais pas de batch ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Le There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sauf que tu as changé le model du Batch en disant que la city est optional. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pour ça que le return checke aussi la présence de la city sur le Batch. Si le batch n'a pas de C'est obsolète par contre si on permet pas de choisir son batch. L'optional sur There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah oui j'avais zappé le |
||
|
||
def assign_private_leaderboard | ||
return if private_leaderboard.present? | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddBackCityOnUsers < ActiveRecord::Migration[7.1] | ||
disable_ddl_transaction! | ||
|
||
def change | ||
add_reference :users, :city, index: { algorithm: :concurrently } | ||
add_foreign_key :users, :cities, validate: false | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddForeignKeyForUsersCityRelationship < ActiveRecord::Migration[7.1] | ||
def change | ||
validate_foreign_key :users, :cities | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Le batch devrait toujours etre disabled.
Soit les gens en ont un et ils peuvent pas changer
Soit ils en ont pas et ils peuvent pas s'en rajouter un.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
J'avais pas cette info 😅 Je savais pas qu'on pouvait être "sans-batch", @pil0u on gère bien leurs points à eux ? Je me souviens plus de l'an dernier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Aquaj Oui, on peut récupérer un batch
0
ounil
sur l'OAuth, des profs extérieurs par exemple, j'évalue à ~1000 Alumnis concernés quand j'ai lancé le scraper, donc pas négligeable.Mais oui, on n'a plus de scoring par batch, uniquement par squad et campus. Et pour le campus, comme ils choisissent eux-mêmes parmi une liste existante, ils sont pris en compte 👍