From 6a3b6f1f3229e841c9c42a17305bbf1c1f4175ce Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Mon, 15 Apr 2024 22:48:37 -0700 Subject: [PATCH] Revert "Clean up after `UserStats`: Remove `User.bonuses`" --- app/models/user.rb | 12 ++++++++++++ app/models/user_stats.rb | 6 ++++-- .../20240416050340_users_remove_bonuses_column.rb | 5 ----- db/schema.rb | 3 ++- 4 files changed, 18 insertions(+), 8 deletions(-) delete mode 100644 db/migrate/20240416050340_users_remove_bonuses_column.rb diff --git a/app/models/user.rb b/app/models/user.rb index c627f081f2..bfc9b7a18d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -88,6 +88,7 @@ # email:: Email address. # admin:: Allowed to enter admin mode? # alert:: Alert message we need to display for User. (serialized) +# bonuses:: List of zero or more contribution bonuses. (serialized) # contribution:: Contribution score (integer). # # ==== Profile @@ -154,6 +155,7 @@ # # ==== Profile # percent_complete:: How much of profile has User finished? +# sum_bonuses:: Add up all the bonuses User has earned. # # ==== Object ownership # comments:: Comment's they've posted. @@ -724,6 +726,16 @@ def percent_complete result * 100 / max end + # Sum up all the bonuses the User has earned. + # + # contribution += user.sum_bonuses + # + def sum_bonuses + return nil unless bonuses + + bonuses.inject(0) { |acc, elem| acc + elem[0] } + end + def successful_contributor? observations.any? end diff --git a/app/models/user_stats.rb b/app/models/user_stats.rb index bfcfe310bb..64bc469de1 100644 --- a/app/models/user_stats.rb +++ b/app/models/user_stats.rb @@ -233,10 +233,12 @@ def refresh_all_user_stats(dry_run: false) # This runs after the migration, to copy columns from users to user_stats # It's a batch insert, so it's fast. + # TODO: After the initial population, drop the column `bonuses` from User, + # and remove references to bonuses in `pluck` and the hash here. def create_user_stats_for_all_users_without records = User.where.missing(:user_stats). - pluck(:id).map do |id| - { user_id: id } + pluck(:id, :bonuses).map do |id, bonuses| + { user_id: id, bonuses: bonuses } end UserStats.insert_all(records) diff --git a/db/migrate/20240416050340_users_remove_bonuses_column.rb b/db/migrate/20240416050340_users_remove_bonuses_column.rb deleted file mode 100644 index 60a1d54feb..0000000000 --- a/db/migrate/20240416050340_users_remove_bonuses_column.rb +++ /dev/null @@ -1,5 +0,0 @@ -class UsersRemoveBonusesColumn < ActiveRecord::Migration[7.1] - def change - remove_column :users, :bonuses - end -end diff --git a/db/schema.rb b/db/schema.rb index 9c83647b36..61f71f614b 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: 2024_04_16_050340) do +ActiveRecord::Schema[7.1].define(version: 2024_04_15_115711) do create_table "api_keys", id: :integer, charset: "utf8mb3", force: :cascade do |t| t.datetime "created_at", precision: nil t.datetime "last_used", precision: nil @@ -708,6 +708,7 @@ t.integer "location_id" t.integer "image_id" t.string "locale", limit: 5 + t.text "bonuses" t.boolean "email_comments_owner", default: true, null: false t.boolean "email_comments_response", default: true, null: false t.boolean "email_comments_all", default: false, null: false