From c5f09b05630e10c47a5c8c3b9caf7c34d8c9429d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie?= Date: Thu, 16 Nov 2023 05:20:13 +0100 Subject: [PATCH 1/9] Plug Batch info on Kitt callback and move back City on User to simplify validation logic. --- app/components/user_form_component.html.erb | 4 +- app/controllers/users_controller.rb | 11 ++---- app/models/batch.rb | 2 +- app/models/user.rb | 37 ++++++++++++++++--- .../20231116030303_add_back_city_on_users.rb | 8 ++++ ...foreign_key_for_users_city_relationship.rb | 5 +++ db/schema.rb | 5 ++- spec/factories/users.rb | 1 + 8 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 db/migrate/20231116030303_add_back_city_on_users.rb create mode 100644 db/migrate/20231116030521_add_foreign_key_for_users_city_relationship.rb diff --git a/app/components/user_form_component.html.erb b/app/components/user_form_component.html.erb index 6d400cc2..70782a5a 100644 --- a/app/components/user_form_component.html.erb +++ b/app/components/user_form_component.html.erb @@ -11,12 +11,12 @@
<%= f.label :batch_number, "Batch #" %> - <%= f.number_field :batch_number, value: @user.batch&.number, min: 1, class: "input", disabled: true %> + <%= f.number_field :batch_number, value: @user.batch&.number, min: 1, class: "input", disabled: @user.batch_id? %>
<%= f.label :city_name, "Campus" %> - <%= f.collection_select(:city_id, City.order(:name), :id, :name, { prompt: "", selected: @user.batch&.city_id }, class: "input", disabled: @user.batch&.city_id?) %> + <%= f.collection_select(:city_id, City.order(:vanity_name), :id, :vanity_name, { prompt: "", selected: @user.city_id }, class: "input", disabled: @user.city_id?) %>
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index f30c9aaf..50c058e0 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 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 diff --git a/app/models/batch.rb b/app/models/batch.rb index ed507cf9..9bb1e12d 100644 --- a/app/models/batch.rb +++ b/app/models/batch.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 7092b0f4..664ac567 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 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.sort_by { |batch| batch.camp.starts_at }.first + + 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 + + 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 + def assign_private_leaderboard return if private_leaderboard.present? diff --git a/db/migrate/20231116030303_add_back_city_on_users.rb b/db/migrate/20231116030303_add_back_city_on_users.rb new file mode 100644 index 00000000..1ee23558 --- /dev/null +++ b/db/migrate/20231116030303_add_back_city_on_users.rb @@ -0,0 +1,8 @@ +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 diff --git a/db/migrate/20231116030521_add_foreign_key_for_users_city_relationship.rb b/db/migrate/20231116030521_add_foreign_key_for_users_city_relationship.rb new file mode 100644 index 00000000..a6a5e228 --- /dev/null +++ b/db/migrate/20231116030521_add_foreign_key_for_users_city_relationship.rb @@ -0,0 +1,5 @@ +class AddForeignKeyForUsersCityRelationship < ActiveRecord::Migration[7.1] + def change + validate_foreign_key :users, :cities + end +end diff --git a/db/schema.rb b/db/schema.rb index 2916816b..503dda1b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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_01_144409) do +ActiveRecord::Schema[7.1].define(version: 2023_11_16_030521) do # These are extensions that must be enabled in order to support this database enable_extension "citext" enable_extension "pgcrypto" @@ -374,6 +374,7 @@ t.boolean "accepted_coc", default: false, null: false t.integer "aoc_id" t.bigint "batch_id" + t.bigint "city_id" t.datetime "created_at", null: false t.boolean "entered_hardcore", default: false, null: false t.string "github_username" @@ -389,6 +390,7 @@ t.string "username" 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 ["referrer_id"], name: "index_users_on_referrer_id" end @@ -406,5 +408,6 @@ add_foreign_key "squad_points", "squads" add_foreign_key "squad_scores", "squads" add_foreign_key "users", "batches" + add_foreign_key "users", "cities" add_foreign_key "users", "users", column: "referrer_id" end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 880624ac..75d4a98d 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -5,5 +5,6 @@ username { "pil0u" } synced { true } uid { id } + city_id { batch&.city_id } end end From 4850c9aed6a165d60440b6c012e40f5ff5ae02fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie?= Date: Thu, 16 Nov 2023 05:22:10 +0100 Subject: [PATCH 2/9] Rubocop --- app/models/user.rb | 4 ++-- db/migrate/20231116030303_add_back_city_on_users.rb | 2 ++ ...31116030521_add_foreign_key_for_users_city_relationship.rb | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 664ac567..9137b882 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -40,14 +40,14 @@ class User < ApplicationRecord def self.from_kitt(auth) batches = auth.info&.schoolings - oldest_batch = batches.sort_by { |batch| batch.camp.starts_at }.first + 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.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) + b.city = City.find_or_initialize_by(name: city&.slug, vanity_name: city&.name) end u.city = u.batch.city diff --git a/db/migrate/20231116030303_add_back_city_on_users.rb b/db/migrate/20231116030303_add_back_city_on_users.rb index 1ee23558..797e5111 100644 --- a/db/migrate/20231116030303_add_back_city_on_users.rb +++ b/db/migrate/20231116030303_add_back_city_on_users.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AddBackCityOnUsers < ActiveRecord::Migration[7.1] disable_ddl_transaction! diff --git a/db/migrate/20231116030521_add_foreign_key_for_users_city_relationship.rb b/db/migrate/20231116030521_add_foreign_key_for_users_city_relationship.rb index a6a5e228..000c2289 100644 --- a/db/migrate/20231116030521_add_foreign_key_for_users_city_relationship.rb +++ b/db/migrate/20231116030521_add_foreign_key_for_users_city_relationship.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AddForeignKeyForUsersCityRelationship < ActiveRecord::Migration[7.1] def change validate_foreign_key :users, :cities From 52b3889c1182c5fd1d531d14878c57b41980bc8b Mon Sep 17 00:00:00 2001 From: Louis Ramos Date: Thu, 16 Nov 2023 18:40:14 +0100 Subject: [PATCH 3/9] fix(users): revert some changes --- app/components/user_form_component.html.erb | 2 +- app/controllers/users_controller.rb | 3 +-- app/models/batch.rb | 2 +- app/models/user.rb | 19 ++----------------- 4 files changed, 5 insertions(+), 21 deletions(-) diff --git a/app/components/user_form_component.html.erb b/app/components/user_form_component.html.erb index 18036cdc..cd1d9d1d 100644 --- a/app/components/user_form_component.html.erb +++ b/app/components/user_form_component.html.erb @@ -11,7 +11,7 @@
<%= f.label :batch_number, "Batch #" %> - <%= f.number_field :batch_number, value: @user.batch&.number, min: 1, class: "input", disabled: @user.batch_id? %> + <%= f.number_field :batch_number, value: @user.batch&.number, min: 1, class: "input", disabled: true %>
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 1b24f91d..d0097a09 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -99,7 +99,6 @@ def updated_params aoc_id: form_params[:aoc_id], entered_hardcore: form_params[:entered_hardcore], username: form_params[:username], - 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 @@ -110,6 +109,6 @@ def unlock_achievements end def form_params - params.require(:user).permit(:accepted_coc, :aoc_id, :entered_hardcore, :username, :batch_number, :city_id, :referrer_code) + params.require(:user).permit(:accepted_coc, :aoc_id, :entered_hardcore, :username, :city_id, :referrer_code) end end diff --git a/app/models/batch.rb b/app/models/batch.rb index 9bb1e12d..ed507cf9 100644 --- a/app/models/batch.rb +++ b/app/models/batch.rb @@ -2,7 +2,7 @@ class Batch < ApplicationRecord has_many :users, dependent: :nullify - belongs_to :city, optional: true + belongs_to :city validates :number, numericality: { in: 1...(2**31), message: "should be between 1 and 2^31" }, allow_nil: true end diff --git a/app/models/user.rb b/app/models/user.rb index 50bc78a6..3017dffd 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,7 +9,6 @@ class User < ApplicationRecord belongs_to :batch, optional: true belongs_to :city, optional: true - has_one :batch_city, through: :batch, source: :city belongs_to :squad, optional: true, touch: true belongs_to :referrer, class_name: "User", optional: true @@ -29,8 +28,6 @@ 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) } @@ -46,9 +43,9 @@ def self.from_kitt(auth) 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) do |b| + u.batch = Batch.find_or_initialize_by(number: oldest_batch&.camp&.slug.to_i) do |b| city = oldest_batch&.city - b.city = City.find_or_initialize_by(name: city&.slug, vanity_name: city&.name) + b.city = City.find_or_initialize_by(name: city&.slug) end u.city = u.batch.city @@ -123,18 +120,6 @@ 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 - def assign_private_leaderboard return if private_leaderboard.present? From 2b552471a5eea00eb44f669d7aad5bb80670feda Mon Sep 17 00:00:00 2001 From: Louis Ramos Date: Thu, 16 Nov 2023 19:27:58 +0100 Subject: [PATCH 4/9] fix(users): undo everything from #289 --- app/domains/scores/city_points.rb | 2 +- ...tt_scraper_job.rb => _kitt_scraper_job.rb} | 0 app/models/batch.rb | 1 - app/models/city.rb | 3 +- app/models/user.rb | 11 ++---- .../scores/user_scores_presenter.rb | 2 +- ...74333_remove_city_and_size_from_batches.rb | 8 ++++ db/schema.rb | 6 +-- db/seeds.rb | 39 ++++++++++++++----- spec/domains/ranks/city_scores_spec.rb | 14 +++---- spec/domains/scores/city_points_spec.rb | 8 ++-- spec/domains/scores/city_scores_spec.rb | 4 +- .../scores/city_scores_presenter_spec.rb | 9 ++--- .../scores/user_scores_presenter_spec.rb | 23 ++++++++--- 14 files changed, 76 insertions(+), 54 deletions(-) rename app/jobs/{kitt_scraper_job.rb => _kitt_scraper_job.rb} (100%) create mode 100644 db/migrate/20231116174333_remove_city_and_size_from_batches.rb diff --git a/app/domains/scores/city_points.rb b/app/domains/scores/city_points.rb index 5c37a34e..f85abfcb 100644 --- a/app/domains/scores/city_points.rb +++ b/app/domains/scores/city_points.rb @@ -19,7 +19,7 @@ def compute points = Scores::SoloPoints.get # index for o(1) fetch - city_for_user = User.joins(:batch).where(id: points.pluck(:user_id)).pluck(:id, "batches.city_id").to_h + city_for_user = User.where(id: points.pluck(:user_id)).pluck(:id, :city_id).to_h city_users = points.group_by { |user| city_for_user[user[:user_id]] } city_users.without(nil).flat_map do |city_id, user_points| diff --git a/app/jobs/kitt_scraper_job.rb b/app/jobs/_kitt_scraper_job.rb similarity index 100% rename from app/jobs/kitt_scraper_job.rb rename to app/jobs/_kitt_scraper_job.rb diff --git a/app/models/batch.rb b/app/models/batch.rb index ed507cf9..7b9fdbba 100644 --- a/app/models/batch.rb +++ b/app/models/batch.rb @@ -2,7 +2,6 @@ class Batch < ApplicationRecord has_many :users, dependent: :nullify - belongs_to :city validates :number, numericality: { in: 1...(2**31), message: "should be between 1 and 2^31" }, allow_nil: true end diff --git a/app/models/city.rb b/app/models/city.rb index c658c585..292c711a 100644 --- a/app/models/city.rb +++ b/app/models/city.rb @@ -4,8 +4,7 @@ class City < ApplicationRecord has_many :city_points, class_name: "Cache::CityPoint", dependent: :delete_all has_many :city_scores, class_name: "Cache::CityScore", dependent: :delete_all - has_many :batches, dependent: :nullify - has_many :users, through: :batches + has_many :users, dependent: :nullify has_many :completions, through: :users before_create :set_default_vanity_name diff --git a/app/models/user.rb b/app/models/user.rb index 3017dffd..37caeb06 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,7 +8,7 @@ class User < ApplicationRecord CONTRIBUTORS = { pilou: "6788", aquaj: "449", louis: "19049", aurelie: "9168" }.freeze belongs_to :batch, optional: true - belongs_to :city, optional: true + belongs_to :city, optional: true, touch: true belongs_to :squad, optional: true, touch: true belongs_to :referrer, class_name: "User", optional: true @@ -42,13 +42,8 @@ def self.from_kitt(auth) 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) do |b| - city = oldest_batch&.city - b.city = City.find_or_initialize_by(name: city&.slug) - end - - u.city = u.batch.city + 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&.slug) end user.github_username = auth.info.github_nickname diff --git a/app/presenters/scores/user_scores_presenter.rb b/app/presenters/scores/user_scores_presenter.rb index e3d9d04a..78bc09cb 100644 --- a/app/presenters/scores/user_scores_presenter.rb +++ b/app/presenters/scores/user_scores_presenter.rb @@ -12,7 +12,7 @@ def initialize(scores) def get @scores ||= User # rubocop:disable Naming/MemoizedInstanceVariableName .where(id: scores_per_user.keys) - .includes(:squad, :completions, batch: :city) + .includes(:squad, :completions, :city) .map { |user| { **identity_of(user), **stats_of(user) } } .sort_by { |user| user[:order] || Float::INFINITY } end diff --git a/db/migrate/20231116174333_remove_city_and_size_from_batches.rb b/db/migrate/20231116174333_remove_city_and_size_from_batches.rb new file mode 100644 index 00000000..7de8f6fc --- /dev/null +++ b/db/migrate/20231116174333_remove_city_and_size_from_batches.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class RemoveCityAndSizeFromBatches < ActiveRecord::Migration[7.1] + def change + safety_assured { remove_column :batches, :city_id, :bigint } + safety_assured { remove_column :batches, :size, :int } + end +end diff --git a/db/schema.rb b/db/schema.rb index c3c5b473..99b436f4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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_16_030521) do +ActiveRecord::Schema[7.1].define(version: 2023_11_16_174333) do # These are extensions that must be enabled in order to support this database enable_extension "citext" enable_extension "pgcrypto" @@ -28,12 +28,9 @@ end create_table "batches", force: :cascade do |t| - t.bigint "city_id" t.datetime "created_at", null: false t.integer "number" - t.integer "size" t.datetime "updated_at", null: false - t.index ["city_id"], name: "index_batches_on_city_id" end create_table "blazer_audits", force: :cascade do |t| @@ -398,7 +395,6 @@ end add_foreign_key "achievements", "users" - add_foreign_key "batches", "cities" add_foreign_key "city_points", "cities" add_foreign_key "city_scores", "cities" add_foreign_key "completions", "users" diff --git a/db/seeds.rb b/db/seeds.rb index 5d2fed1c..76d35271 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -16,7 +16,20 @@ Batch.destroy_all end -KittScraperJob.perform_now(scrape_kitt: false) +def fetch(url) + uri = URI(url) + https = Net::HTTP.new(uri.hostname, uri.port) + https.use_ssl = true + + request = Net::HTTP::Get.new(uri) + request["Cookie"] = "_kitt2017_=#{ENV.fetch('KITT_SESSION_COOKIE')}" + + https.request(request) +end + +data = JSON.parse(fetch("https://kitt.lewagon.com/api/v1/users").read_body) +cities = data["all_places"] +cities.each { |city| City.find_or_create_by(name: city["label"], size: city["count"]) } Rails.logger.info "✔ Cities initialized" @@ -26,49 +39,57 @@ User.create!([ { username: "test_1", - batch: Batch.find_by(number: 343), + batch: Batch.find_or_create_by(number: 343), + city: City.find_by(name: "Paris"), aoc_id: 151_323, uid: 1 }, { username: "test_2", - batch: Batch.find_by(number: 454), + batch: Batch.find_or_create_by(number: 454), + city: City.find_by(name: "Paris"), aoc_id: 1_095_582, uid: 2 }, { username: "test_3", - batch: Batch.find_by(number: 123), + batch: Batch.find_or_create_by(number: 123), + city: City.find_by(name: "Bordeaux"), aoc_id: 1_266_664, uid: 3 }, { username: "test_4", - batch: Batch.find_by(number: 123), + batch: Batch.find_or_create_by(number: 123), + city: City.find_by(name: "London"), aoc_id: 1_237_086, uid: 4 }, { username: "test_5", - batch: Batch.find_by(number: 123), + batch: Batch.find_or_create_by(number: 123), + city: City.find_by(name: "London"), aoc_id: 1_258_899, uid: 5 }, { username: "test_6", - batch: Batch.find_by(number: 454), + batch: Batch.find_or_create_by(number: 454), + city: City.find_by(name: "Paris"), aoc_id: 1_259_034, uid: 6 }, { username: "test_7", - batch: Batch.find_by(number: 454), + batch: Batch.find_or_create_by(number: 454), + city: City.find_by(name: "Brussels"), aoc_id: 1_259_062, uid: 7 }, { username: "test_8", - batch: Batch.find_by(number: 343), + batch: Batch.find_or_create_by(number: 343), + city: City.find_by(name: "Paris"), aoc_id: 1_259_379, uid: 8 } diff --git a/spec/domains/ranks/city_scores_spec.rb b/spec/domains/ranks/city_scores_spec.rb index e07b54d7..cd41bdfb 100644 --- a/spec/domains/ranks/city_scores_spec.rb +++ b/spec/domains/ranks/city_scores_spec.rb @@ -4,9 +4,7 @@ RSpec.describe Ranks::CityScores do let(:bordeaux) { create :city, name: "Bordeaux", size: 60 } - let(:bordeaux_batch) { create(:batch, number: 1, city: bordeaux) } let(:brussels) { create :city, name: "Brussels", size: 10 } - let(:brussels_batch) { create(:batch, number: 2, city: brussels) } let(:input) do [ @@ -34,10 +32,10 @@ let!(:completions) do [ - *create_list(:user, 11, batch: bordeaux_batch).map do |u| + *create_list(:user, 11, city: bordeaux).map do |u| create(:completion, user: u, day: 1, challenge: 1, completion_unix_time: Aoc.begin_time + 23.hours) end, - *create_list(:user, 10, batch: brussels_batch).map do |u| + *create_list(:user, 10, city: brussels).map do |u| create(:completion, user: u, day: 1, challenge: 1, completion_unix_time: Aoc.begin_time + 23.hours) end ] @@ -60,17 +58,17 @@ let!(:completions) do [ - *create_list(:user, 12, batch: bordeaux_batch).map do |u| + *create_list(:user, 12, city: bordeaux).map do |u| create(:completion, user: u, day: 1, challenge: 1, completion_unix_time: Aoc.begin_time + 23.hours) end, - *create_list(:user, 10, batch: brussels_batch).map do |u| + *create_list(:user, 10, city: brussels).map do |u| create(:completion, user: u, day: 1, challenge: 1, completion_unix_time: Aoc.begin_time + 23.hours) end, - *create_list(:user, 11, batch: bordeaux_batch).map do |u| + *create_list(:user, 11, city: bordeaux).map do |u| create(:completion, user: u, day: 1, challenge: 2, completion_unix_time: Aoc.begin_time + 40.hours) end, - *create_list(:user, 9, batch: brussels_batch).map do |u| + *create_list(:user, 9, city: brussels).map do |u| create(:completion, user: u, day: 1, challenge: 2, completion_unix_time: Aoc.begin_time + 40.hours) end ] diff --git a/spec/domains/scores/city_points_spec.rb b/spec/domains/scores/city_points_spec.rb index 35a4b6e1..a056cfe0 100644 --- a/spec/domains/scores/city_points_spec.rb +++ b/spec/domains/scores/city_points_spec.rb @@ -5,8 +5,7 @@ RSpec.describe Scores::CityPoints do let!(:state) { create :state, completions_fetched: solo_points.count, fetch_api_end: Time.zone.now } let(:brussels) { create(:city, name: "Brussels", size: 400) } - let(:brussels_batch) { create(:batch, number: 1, city: brussels) } - let(:brussels_users) { create_list(:user, 18, batch: brussels_batch) } + let(:brussels_users) { create_list(:user, 18, city: brussels) } let(:brussels_points) do [ { day: 1, challenge: 1, score: 50, user_id: brussels_users[0].id }, @@ -19,8 +18,7 @@ end let(:bordeaux) { create(:city, name: "Bordeaux", size: 400) } - let(:bordeaux_batch) { create(:batch, number: 1, city: bordeaux) } - let(:bordeaux_users) { create_list(:user, 18, batch: bordeaux_batch) } + let(:bordeaux_users) { create_list(:user, 18, city: bordeaux) } let(:bordeaux_points) do [ { day: 1, challenge: 1, score: 46, user_id: bordeaux_users[0].id } @@ -164,7 +162,7 @@ end context "when the users makeup of the city has changed in the meantime" do - let(:new_bordeaux_users) { create_list :user, 2, batch: bordeaux_batch } + let(:new_bordeaux_users) { create_list :user, 2, city: bordeaux } before do travel 10.seconds # Specs go too fast, updated_at stays the same otherwise diff --git a/spec/domains/scores/city_scores_spec.rb b/spec/domains/scores/city_scores_spec.rb index c557af13..93f9eadd 100644 --- a/spec/domains/scores/city_scores_spec.rb +++ b/spec/domains/scores/city_scores_spec.rb @@ -6,9 +6,7 @@ let(:state) { create(:state, completions_fetched: 6, fetch_api_end: Time.zone.now) } let(:bordeaux) { create :city, name: "Bordeaux", size: 10 } - let(:bordeaux_batch) { create(:batch, number: 1, city: bordeaux) } let(:brussels) { create :city, name: "Brussels", size: 10 } - let(:brussels_batch) { create(:batch, number: 2, city: brussels) } let(:city_points) do [ { day: 1, challenge: 1, city_id: brussels.id, contributor_count: 2, total_score: 96, score: 8.00 }, @@ -98,7 +96,7 @@ before do travel 10.seconds # Specs go too fast, updated_at stays the same otherwise create(:state, fetch_api_begin: Time.zone.now, completions_fetched: 1) - create_list :user, 2, batch: bordeaux_batch # creating after travel + create_list :user, 2, city: bordeaux # creating after travel allow(Scores::CityPoints).to receive(:get).and_return(new_city_points) end diff --git a/spec/presenters/scores/city_scores_presenter_spec.rb b/spec/presenters/scores/city_scores_presenter_spec.rb index b6b2b0eb..93fbd4cb 100644 --- a/spec/presenters/scores/city_scores_presenter_spec.rb +++ b/spec/presenters/scores/city_scores_presenter_spec.rb @@ -4,15 +4,12 @@ RSpec.describe Scores::CityScoresPresenter do let!(:city_1) { create :city, id: 1, name: "Bordeaux", size: 200 } - let!(:batch_1) { create(:batch, number: 1, city: city_1) } let!(:city_2) { create :city, id: 2, name: "Rio de Janeiro", size: 100 } - let!(:batch_2) { create(:batch, number: 2, city: city_2) } let!(:city_3) { create :city, id: 3, name: "Paris", size: 500 } - let!(:batch_3) { create(:batch, number: 3, city: city_3) } - let!(:user_1) { create :user, id: 1, batch: batch_1 } - let!(:user_2) { create :user, id: 2, batch: batch_2 } - let!(:user_3) { create :user, id: 3, batch: batch_1 } + let!(:user_1) { create :user, id: 1, city: city_1 } + let!(:user_2) { create :user, id: 2, city: city_2 } + let!(:user_3) { create :user, id: 3, city: city_1 } let(:input) do [ diff --git a/spec/presenters/scores/user_scores_presenter_spec.rb b/spec/presenters/scores/user_scores_presenter_spec.rb index 84a3445e..04001f82 100644 --- a/spec/presenters/scores/user_scores_presenter_spec.rb +++ b/spec/presenters/scores/user_scores_presenter_spec.rb @@ -7,9 +7,9 @@ let!(:bordeaux) { create :city, name: "Bordeaux" } let!(:squad_1) { create :squad, name: "The Killers" } let!(:squad_2) { create :squad, name: "Grouplove" } - let!(:batch_1) { create :batch, number: 1, city: paris } - let!(:batch_2) { create :batch, number: 2, city: bordeaux } - let!(:batch_3) { create :batch, number: 3, city: bordeaux } + let!(:batch_1) { create :batch, number: 1 } + let!(:batch_2) { create :batch, number: 2 } + let!(:batch_3) { create :batch, number: 3 } let!(:user_1) do create :user, @@ -17,6 +17,7 @@ username: "Saunier", squad: squad_1, batch: batch_1, + city: paris, entered_hardcore: false end let!(:user_2) do @@ -25,6 +26,7 @@ username: "pil0u", squad: squad_2, batch: batch_2, + city: bordeaux, entered_hardcore: true end @@ -150,10 +152,21 @@ end end + context "when user has no city" do + before do + user_1.update!(city: nil) + end + + it "includes no info about it in the output" do + expect(described_class.new(input).get).to include( + hash_including(uid: 1, city_vanity_name: nil) + ) + end + end + context "when user has no batch" do before do - user_1.batch_id = nil - user_1.save(validate: false) + user_1.update!(batch: nil) end it "includes no info about it in the output" do From f7205a481b6930f6ec95001bdb43e6b249d32121 Mon Sep 17 00:00:00 2001 From: Louis Ramos Date: Thu, 16 Nov 2023 19:44:02 +0100 Subject: [PATCH 5/9] fix(users): use kitt city name instead of slug --- app/models/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 37caeb06..6087e8d3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -43,7 +43,7 @@ def self.from_kitt(auth) 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&.slug) + u.city = City.find_or_initialize_by(name: oldest_batch&.city&.name) end user.github_username = auth.info.github_nickname From 837b0bc765677f8bd568db22736f287b95283c80 Mon Sep 17 00:00:00 2001 From: Louis Ramos Date: Thu, 16 Nov 2023 23:14:59 +0100 Subject: [PATCH 6/9] fix(users) last comments --- app/components/user_form_component.html.erb | 2 +- app/jobs/_kitt_scraper_job.rb | 80 ------------------ app/models/city.rb | 2 +- db/seeds.rb | 92 ++++++++++++++++++--- spec/factories/users.rb | 1 - 5 files changed, 81 insertions(+), 96 deletions(-) delete mode 100644 app/jobs/_kitt_scraper_job.rb diff --git a/app/components/user_form_component.html.erb b/app/components/user_form_component.html.erb index cd1d9d1d..37825800 100644 --- a/app/components/user_form_component.html.erb +++ b/app/components/user_form_component.html.erb @@ -15,7 +15,7 @@
- <%= f.label :city_name, "Campus" %> + <%= 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?) %>
diff --git a/app/jobs/_kitt_scraper_job.rb b/app/jobs/_kitt_scraper_job.rb deleted file mode 100644 index 202929e7..00000000 --- a/app/jobs/_kitt_scraper_job.rb +++ /dev/null @@ -1,80 +0,0 @@ -# frozen_string_literal: true - -require "csv" -require "net/http" - -class KittScraperJob < ApplicationJob - CSV_PATH = "db/static/kitt_alumni.csv" - - def perform(scrape_kitt: true, upsert_data: true) - perform_scrape_kitt if scrape_kitt - perform_upsert_data if upsert_data - end - - private - - def append_new_users_to_csv - CSV.open(CSV_PATH, "a") do |csv| - @new_users.each { |user| csv << user } - end - end - - def fetch(url) - uri = URI(url) - https = Net::HTTP.new(uri.hostname, uri.port) - https.use_ssl = true - - request = Net::HTTP::Get.new(uri) - request["Cookie"] = "_kitt2017_=#{ENV.fetch('KITT_SESSION_COOKIE')}" - - https.request(request) - end - - def perform_scrape_kitt - @known_uids = CSV.read(CSV_PATH, headers: true).pluck("uid").to_set(&:to_i) - @new_users = [] - page = 1 - - loop do - data = JSON.parse(fetch("https://kitt.lewagon.com/api/v1/users?page=#{page}").read_body) - Rails.logger.info("Scraping page #{page}/#{data['page_count']}...") - - push_new_users(data["users"]) - - page += 1 - break unless data["has_more"] - end - - append_new_users_to_csv - end - - def perform_upsert_data - cities = Hash.new { |city_hash, city| city_hash[city] = Hash.new(0) } - CSV.foreach(CSV_PATH, headers: true) { |row| cities[row["city_name"]][row["batch_number"]] += 1 } - - cities.each do |city_name, batches| - city = City.find_or_initialize_by(name: city_name) - city.update!(size: batches.values.sum) - - batches.each do |batch_number, size| - batch = Batch.find_or_initialize_by(number: batch_number) - batch.update!(size:, city:) - end - end - end - - def push_new_users(users) - users.each do |user| - alumnus = user["alumnus"] - uid = alumnus["id"] - next if @known_uids.include?(uid) - - batch_number = alumnus["camp_slug"].to_i - next if batch_number == 0 - - city_name = alumnus["city"] - batch_year = Date.parse(alumnus["camp_date"]).year - @new_users << [uid, batch_number, batch_year, city_name] - end - end -end diff --git a/app/models/city.rb b/app/models/city.rb index 292c711a..52a84333 100644 --- a/app/models/city.rb +++ b/app/models/city.rb @@ -9,7 +9,7 @@ class City < ApplicationRecord before_create :set_default_vanity_name - validates :name, uniqueness: { case_sensitive: false } + validates :name, presence: true, uniqueness: { case_sensitive: false } def self.find_by_slug(slug) find_by!("REPLACE(LOWER(name), ' ', '-') = ?", slug) diff --git a/db/seeds.rb b/db/seeds.rb index 76d35271..a294201a 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -16,21 +16,87 @@ Batch.destroy_all end -def fetch(url) - uri = URI(url) - https = Net::HTTP.new(uri.hostname, uri.port) - https.use_ssl = true - - request = Net::HTTP::Get.new(uri) - request["Cookie"] = "_kitt2017_=#{ENV.fetch('KITT_SESSION_COOKIE')}" - - https.request(request) +cities = [ + { name: "Amsterdam", size: 666 }, + { name: "Angers", size: 0 }, + { name: "B2G Latam", size: 105 }, + { name: "Bali", size: 421 }, + { name: "Barcelona", size: 610 }, + { name: "Beirut", size: 14 }, + { name: "Belo Horizonte", size: 22 }, + { name: "Berlin", size: 2563 }, + { name: "Bordeaux", size: 712 }, + { name: "Brasilia", size: 134 }, + { name: "Brussels", size: 837 }, + { name: "Budapest", size: 0 }, + { name: "Buenos Aires", size: 397 }, + { name: "Cape Town", size: 92 }, + { name: "Casablanca", size: 143 }, + { name: "Chengdu", size: 64 }, + { name: "Cologne", size: 97 }, + { name: "Copenhagen", size: 117 }, + { name: "Dubai", size: 35 }, + { name: "Emil", size: 172 }, + { name: "Essonne", size: 16 }, + { name: "Florianópolis", size: 0 }, + { name: "For Business", size: 217 }, + { name: "For School", size: 0 }, + { name: "Istanbul", size: 30 }, + { name: "Kyoto", size: 18 }, + { name: "Lausanne", size: 191 }, + { name: "Lille", size: 484 }, + { name: "Lima", size: 53 }, + { name: "Lisbon", size: 1037 }, + { name: "London", size: 2366 }, + { name: "Lyon", size: 575 }, + { name: "Madrid", size: 209 }, + { name: "Malmö", size: 21 }, + { name: "Marseille", size: 687 }, + { name: "Martinique", size: 42 }, + { name: "Mauritius", size: 184 }, + { name: "Medellín", size: 71 }, + { name: "Melbourne", size: 468 }, + { name: "Mexico", size: 301 }, + { name: "Milan", size: 137 }, + { name: "Montréal", size: 645 }, + { name: "Munich", size: 293 }, + { name: "Nantes", size: 375 }, + { name: "Nice", size: 200 }, + { name: "Online", size: 1696 }, + { name: "Online APAC", size: 0 }, + { name: "Oslo", size: 55 }, + { name: "Paris", size: 4189 }, + { name: "Playa del Carmen", size: 0 }, + { name: "Porto", size: 53 }, + { name: "Recife", size: 0 }, + { name: "Rennes", size: 77 }, + { name: "Renova", size: 0 }, + { name: "Rio de Janeiro", size: 488 }, + { name: "Riyadh", size: 74 }, + { name: "Rome", size: 0 }, + { name: "Santiago", size: 98 }, + { name: "Seine et Marne", size: 25 }, + { name: "Seoul", size: 16 }, + { name: "Shanghai", size: 435 }, + { name: "Shenzhen", size: 24 }, + { name: "Singapore", size: 402 }, + { name: "Stockholm", size: 51 }, + { name: "Sydney", size: 2 }, + { name: "São Paulo", size: 1043 }, + { name: "Tel Aviv", size: 158 }, + { name: "Testville", size: 7 }, + { name: "Tokyo", size: 816 }, + { name: "Toronto", size: 0 }, + { name: "Toulouse", size: 20 }, + { name: "Tunis", size: 0 }, + { name: "Zurich", size: 64 }, + { name: "remote", size: 0 } +] +cities.each do |city| + c = City.find_or_create_by(name: city[:name]) + c.update(size: city[:size]) end -data = JSON.parse(fetch("https://kitt.lewagon.com/api/v1/users").read_body) -cities = data["all_places"] -cities.each { |city| City.find_or_create_by(name: city["label"], size: city["count"]) } - Rails.logger.info "✔ Cities initialized" # Initialize some users in development diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 75d4a98d..880624ac 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -5,6 +5,5 @@ username { "pil0u" } synced { true } uid { id } - city_id { batch&.city_id } end end From 07e6384ac762aa01cb9708681245ea5e014d9593 Mon Sep 17 00:00:00 2001 From: Louis Ramos Date: Thu, 16 Nov 2023 23:27:49 +0100 Subject: [PATCH 7/9] fix(users): remove empty city froms seed --- db/seeds.rb | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index a294201a..63ccdf63 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -18,7 +18,6 @@ cities = [ { name: "Amsterdam", size: 666 }, - { name: "Angers", size: 0 }, { name: "B2G Latam", size: 105 }, { name: "Bali", size: 421 }, { name: "Barcelona", size: 610 }, @@ -28,7 +27,6 @@ { name: "Bordeaux", size: 712 }, { name: "Brasilia", size: 134 }, { name: "Brussels", size: 837 }, - { name: "Budapest", size: 0 }, { name: "Buenos Aires", size: 397 }, { name: "Cape Town", size: 92 }, { name: "Casablanca", size: 143 }, @@ -38,9 +36,7 @@ { name: "Dubai", size: 35 }, { name: "Emil", size: 172 }, { name: "Essonne", size: 16 }, - { name: "Florianópolis", size: 0 }, { name: "For Business", size: 217 }, - { name: "For School", size: 0 }, { name: "Istanbul", size: 30 }, { name: "Kyoto", size: 18 }, { name: "Lausanne", size: 191 }, @@ -63,17 +59,12 @@ { name: "Nantes", size: 375 }, { name: "Nice", size: 200 }, { name: "Online", size: 1696 }, - { name: "Online APAC", size: 0 }, { name: "Oslo", size: 55 }, { name: "Paris", size: 4189 }, - { name: "Playa del Carmen", size: 0 }, { name: "Porto", size: 53 }, - { name: "Recife", size: 0 }, { name: "Rennes", size: 77 }, - { name: "Renova", size: 0 }, { name: "Rio de Janeiro", size: 488 }, { name: "Riyadh", size: 74 }, - { name: "Rome", size: 0 }, { name: "Santiago", size: 98 }, { name: "Seine et Marne", size: 25 }, { name: "Seoul", size: 16 }, @@ -86,11 +77,8 @@ { name: "Tel Aviv", size: 158 }, { name: "Testville", size: 7 }, { name: "Tokyo", size: 816 }, - { name: "Toronto", size: 0 }, { name: "Toulouse", size: 20 }, - { name: "Tunis", size: 0 }, - { name: "Zurich", size: 64 }, - { name: "remote", size: 0 } + { name: "Zurich", size: 64 } ] cities.each do |city| c = City.find_or_create_by(name: city[:name]) From 828396b38cfa2abe20b935791420570d06137e95 Mon Sep 17 00:00:00 2001 From: pil0u <8350914+pil0u@users.noreply.github.com> Date: Fri, 17 Nov 2023 01:36:02 +0100 Subject: [PATCH 8/9] Wording + spacing --- db/seeds.rb | 139 ++++++++++++++++++++++++++-------------------------- 1 file changed, 69 insertions(+), 70 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index 63ccdf63..57c26547 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -9,83 +9,82 @@ ) Rails.logger.info "✔ States initialized" -# Initialize cities & batch - +# Initialize campuses if Rails.env.development? City.destroy_all Batch.destroy_all end -cities = [ - { name: "Amsterdam", size: 666 }, - { name: "B2G Latam", size: 105 }, - { name: "Bali", size: 421 }, - { name: "Barcelona", size: 610 }, - { name: "Beirut", size: 14 }, - { name: "Belo Horizonte", size: 22 }, - { name: "Berlin", size: 2563 }, - { name: "Bordeaux", size: 712 }, - { name: "Brasilia", size: 134 }, - { name: "Brussels", size: 837 }, - { name: "Buenos Aires", size: 397 }, - { name: "Cape Town", size: 92 }, - { name: "Casablanca", size: 143 }, - { name: "Chengdu", size: 64 }, - { name: "Cologne", size: 97 }, - { name: "Copenhagen", size: 117 }, - { name: "Dubai", size: 35 }, - { name: "Emil", size: 172 }, - { name: "Essonne", size: 16 }, - { name: "For Business", size: 217 }, - { name: "Istanbul", size: 30 }, - { name: "Kyoto", size: 18 }, - { name: "Lausanne", size: 191 }, - { name: "Lille", size: 484 }, - { name: "Lima", size: 53 }, - { name: "Lisbon", size: 1037 }, - { name: "London", size: 2366 }, - { name: "Lyon", size: 575 }, - { name: "Madrid", size: 209 }, - { name: "Malmö", size: 21 }, - { name: "Marseille", size: 687 }, - { name: "Martinique", size: 42 }, - { name: "Mauritius", size: 184 }, - { name: "Medellín", size: 71 }, - { name: "Melbourne", size: 468 }, - { name: "Mexico", size: 301 }, - { name: "Milan", size: 137 }, - { name: "Montréal", size: 645 }, - { name: "Munich", size: 293 }, - { name: "Nantes", size: 375 }, - { name: "Nice", size: 200 }, - { name: "Online", size: 1696 }, - { name: "Oslo", size: 55 }, - { name: "Paris", size: 4189 }, - { name: "Porto", size: 53 }, - { name: "Rennes", size: 77 }, - { name: "Rio de Janeiro", size: 488 }, - { name: "Riyadh", size: 74 }, - { name: "Santiago", size: 98 }, - { name: "Seine et Marne", size: 25 }, - { name: "Seoul", size: 16 }, - { name: "Shanghai", size: 435 }, - { name: "Shenzhen", size: 24 }, - { name: "Singapore", size: 402 }, - { name: "Stockholm", size: 51 }, - { name: "Sydney", size: 2 }, - { name: "São Paulo", size: 1043 }, - { name: "Tel Aviv", size: 158 }, - { name: "Testville", size: 7 }, - { name: "Tokyo", size: 816 }, - { name: "Toulouse", size: 20 }, - { name: "Zurich", size: 64 } +campuses = [ + { name: "Amsterdam", size: 666 }, + { name: "B2G Latam", size: 105 }, + { name: "Bali", size: 421 }, + { name: "Barcelona", size: 610 }, + { name: "Beirut", size: 14 }, + { name: "Belo Horizonte", size: 22 }, + { name: "Berlin", size: 2563 }, + { name: "Bordeaux", size: 712 }, + { name: "Brasilia", size: 134 }, + { name: "Brussels", size: 837 }, + { name: "Buenos Aires", size: 397 }, + { name: "Cape Town", size: 92 }, + { name: "Casablanca", size: 143 }, + { name: "Chengdu", size: 64 }, + { name: "Cologne", size: 97 }, + { name: "Copenhagen", size: 117 }, + { name: "Dubai", size: 35 }, + { name: "Emil", size: 172 }, + { name: "Essonne", size: 16 }, + { name: "For Business", size: 217 }, + { name: "Istanbul", size: 30 }, + { name: "Kyoto", size: 18 }, + { name: "Lausanne", size: 191 }, + { name: "Lille", size: 484 }, + { name: "Lima", size: 53 }, + { name: "Lisbon", size: 1037 }, + { name: "London", size: 2366 }, + { name: "Lyon", size: 575 }, + { name: "Madrid", size: 209 }, + { name: "Malmö", size: 21 }, + { name: "Marseille", size: 687 }, + { name: "Martinique", size: 42 }, + { name: "Mauritius", size: 184 }, + { name: "Medellín", size: 71 }, + { name: "Melbourne", size: 468 }, + { name: "Mexico", size: 301 }, + { name: "Milan", size: 137 }, + { name: "Montréal", size: 645 }, + { name: "Munich", size: 293 }, + { name: "Nantes", size: 375 }, + { name: "Nice", size: 200 }, + { name: "Online", size: 1696 }, + { name: "Oslo", size: 55 }, + { name: "Paris", size: 4189 }, + { name: "Porto", size: 53 }, + { name: "Rennes", size: 77 }, + { name: "Rio de Janeiro", size: 488 }, + { name: "Riyadh", size: 74 }, + { name: "Santiago", size: 98 }, + { name: "Seine et Marne", size: 25 }, + { name: "Seoul", size: 16 }, + { name: "Shanghai", size: 435 }, + { name: "Shenzhen", size: 24 }, + { name: "Singapore", size: 402 }, + { name: "Stockholm", size: 51 }, + { name: "Sydney", size: 2 }, + { name: "São Paulo", size: 1043 }, + { name: "Tel Aviv", size: 158 }, + { name: "Testville", size: 7 }, + { name: "Tokyo", size: 816 }, + { name: "Toulouse", size: 20 }, + { name: "Zurich", size: 64 } ] -cities.each do |city| - c = City.find_or_create_by(name: city[:name]) - c.update(size: city[:size]) -end -Rails.logger.info "✔ Cities initialized" +campuses.each do |campus| + c = City.find_or_create_by(name: campus[:name]) + c.update(size: campus[:size]) +end +Rails.logger.info "✔ Campuses initialized" # Initialize some users in development if Rails.env.development? From 194c053134585add5eb28280019f8e776932a033 Mon Sep 17 00:00:00 2001 From: pil0u <8350914+pil0u@users.noreply.github.com> Date: Fri, 17 Nov 2023 01:44:51 +0100 Subject: [PATCH 9/9] Remove Testville --- db/seeds.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/db/seeds.rb b/db/seeds.rb index 57c26547..ce3f31b6 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -74,7 +74,6 @@ { name: "Sydney", size: 2 }, { name: "São Paulo", size: 1043 }, { name: "Tel Aviv", size: 158 }, - { name: "Testville", size: 7 }, { name: "Tokyo", size: 816 }, { name: "Toulouse", size: 20 }, { name: "Zurich", size: 64 }