From d20e07ee19a9e645c2d303088437f74378568600 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Fri, 31 Dec 2021 22:26:47 +0100 Subject: [PATCH] Remove relationship counters in `users` table --- Rakefile | 2 -- app/models/relationships/follow.rb | 6 ------ app/views/settings/_data.haml | 4 ++-- ...0211231212335_remove_relationship_counters_from_users.rb | 6 ++++++ db/schema.rb | 2 -- lib/exporter.rb | 6 +++--- 6 files changed, 11 insertions(+), 15 deletions(-) create mode 100644 db/migrate/20211231212335_remove_relationship_counters_from_users.rb diff --git a/Rakefile b/Rakefile index 65f51123..665af766 100644 --- a/Rakefile +++ b/Rakefile @@ -69,8 +69,6 @@ namespace :justask do asked = Question.where(user: user).where(author_is_anonymous: false).count commented = Comment.where(user: user).count smiled = Smile.where(user: user).count - user.friend_count = user.friends.count - user.follower_count = user.followers.count user.answered_count = answered user.asked_count = asked user.commented_count = commented diff --git a/app/models/relationships/follow.rb b/app/models/relationships/follow.rb index f8d3c657..11aafc35 100644 --- a/app/models/relationships/follow.rb +++ b/app/models/relationships/follow.rb @@ -1,16 +1,10 @@ class Relationships::Follow < Relationship after_create do Notification.notify target, self - # increment counts - source.increment! :friend_count - target.increment! :follower_count end before_destroy do Notification.denotify target, self - # decrement counts - source.decrement! :friend_count - target.decrement! :follower_count end def notification_type(*_args) diff --git a/app/views/settings/_data.haml b/app/views/settings/_data.haml index 929800ab..0ede0262 100644 --- a/app/views/settings/_data.haml +++ b/app/views/settings/_data.haml @@ -76,10 +76,10 @@ %p.text-muted= current_user.asked_count %p.font-weight-bold.mb-0 Following - %p.text-muted= current_user.friend_count + %p.text-muted= current_user.followings.count %p.font-weight-bold.mb-0 Followers - %p.text-muted= current_user.follower_count + %p.text-muted= current_user.followers.count %p.font-weight-bold.mb-0 Smiles %p.text-muted= current_user.smiled_count + current_user.comment_smiled_count diff --git a/db/migrate/20211231212335_remove_relationship_counters_from_users.rb b/db/migrate/20211231212335_remove_relationship_counters_from_users.rb new file mode 100644 index 00000000..40cc4033 --- /dev/null +++ b/db/migrate/20211231212335_remove_relationship_counters_from_users.rb @@ -0,0 +1,6 @@ +class RemoveRelationshipCountersFromUsers < ActiveRecord::Migration[5.2] + def change + remove_column :users, :follower_count + remove_column :users, :friend_count + end +end diff --git a/db/schema.rb b/db/schema.rb index 3db5ff31..55de0a22 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -275,8 +275,6 @@ ActiveRecord::Schema.define(version: 2022_01_05_171216) do t.datetime "created_at" t.datetime "updated_at" t.string "screen_name" - t.integer "friend_count", default: 0, null: false - t.integer "follower_count", default: 0, null: false t.integer "asked_count", default: 0, null: false t.integer "answered_count", default: 0, null: false t.integer "commented_count", default: 0, null: false diff --git a/lib/exporter.rb b/lib/exporter.rb index 0c087c82..f4566f86 100644 --- a/lib/exporter.rb +++ b/lib/exporter.rb @@ -36,7 +36,7 @@ class Exporter def collect_user_info %i(answered_count asked_count ban_reason banned_until comment_smiled_count commented_count confirmation_sent_at confirmed_at created_at profile_header profile_header_h profile_header_w profile_header_x profile_header_y - profile_picture_w profile_picture_h profile_picture_x profile_picture_y current_sign_in_at current_sign_in_ip follower_count friend_count + profile_picture_w profile_picture_h profile_picture_x profile_picture_y current_sign_in_at current_sign_in_ip id last_sign_in_at last_sign_in_ip locale permanently_banned privacy_allow_anonymous_questions privacy_allow_public_timeline privacy_allow_stranger_answers privacy_show_in_search profile_header_file_name profile_picture_file_name @@ -232,8 +232,8 @@ class Exporter def user_stub(user) uobj = {} - %i(answered_count asked_count comment_smiled_count commented_count created_at follower_count - friend_count id permanently_banned screen_name smiled_count).each do |f| + %i(answered_count asked_count comment_smiled_count commented_count created_at + id permanently_banned screen_name smiled_count).each do |f| uobj[f] = user.send f end