Skip to content

Commit

Permalink
Revert "Clean up after UserStats: Remove User.bonuses"
Browse files Browse the repository at this point in the history
  • Loading branch information
nimmolo committed Apr 16, 2024
1 parent 3c8a36a commit 6a3b6f1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
12 changes: 12 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions app/models/user_stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions db/migrate/20240416050340_users_remove_bonuses_column.rb

This file was deleted.

3 changes: 2 additions & 1 deletion 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: 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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6a3b6f1

Please sign in to comment.