From: Nenad Vujicic Date: Mon, 3 Jun 2024 12:58:23 +0000 (+0200) Subject: Add note and diary comments counts to the user profile X-Git-Tag: live~466^2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/a4d3d324ecc2e4535dd1e8fa62bac2bdd2951d5b Add note and diary comments counts to the user profile Fixes #1643 --- diff --git a/app/models/diary_comment.rb b/app/models/diary_comment.rb index 55d2c3c84..a88c69f12 100644 --- a/app/models/diary_comment.rb +++ b/app/models/diary_comment.rb @@ -23,7 +23,7 @@ # class DiaryComment < ApplicationRecord - belongs_to :user + belongs_to :user, :counter_cache => true belongs_to :diary_entry scope :visible, -> { where(:visible => true) } diff --git a/app/models/note_comment.rb b/app/models/note_comment.rb index b870e5558..d318c5e95 100644 --- a/app/models/note_comment.rb +++ b/app/models/note_comment.rb @@ -26,7 +26,7 @@ class NoteComment < ApplicationRecord belongs_to :note, :touch => true - belongs_to :author, :class_name => "User", :optional => true + belongs_to :author, :class_name => "User", :optional => true, :counter_cache => true validates :id, :uniqueness => true, :presence => { :on => :update }, :numericality => { :on => :update, :only_integer => true } diff --git a/app/models/user.rb b/app/models/user.rb index e4067a350..4241a9e56 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,35 +2,37 @@ # # Table name: users # -# email :string not null -# id :bigint(8) not null, primary key -# pass_crypt :string not null -# creation_time :datetime not null -# display_name :string default(""), not null -# data_public :boolean default(FALSE), not null -# description :text default(""), not null -# home_lat :float -# home_lon :float -# home_zoom :integer default(3) -# pass_salt :string -# email_valid :boolean default(FALSE), not null -# new_email :string -# creation_ip :string -# languages :string -# status :enum default("pending"), not null -# terms_agreed :datetime -# consider_pd :boolean default(FALSE), not null -# auth_uid :string -# preferred_editor :string -# terms_seen :boolean default(FALSE), not null -# description_format :enum default("markdown"), not null -# changesets_count :integer default(0), not null -# traces_count :integer default(0), not null -# diary_entries_count :integer default(0), not null -# image_use_gravatar :boolean default(FALSE), not null -# auth_provider :string -# home_tile :bigint(8) -# tou_agreed :datetime +# email :string not null +# id :bigint(8) not null, primary key +# pass_crypt :string not null +# creation_time :datetime not null +# display_name :string default(""), not null +# data_public :boolean default(FALSE), not null +# description :text default(""), not null +# home_lat :float +# home_lon :float +# home_zoom :integer default(3) +# pass_salt :string +# email_valid :boolean default(FALSE), not null +# new_email :string +# creation_ip :string +# languages :string +# status :enum default("pending"), not null +# terms_agreed :datetime +# consider_pd :boolean default(FALSE), not null +# auth_uid :string +# preferred_editor :string +# terms_seen :boolean default(FALSE), not null +# description_format :enum default("markdown"), not null +# changesets_count :integer default(0), not null +# traces_count :integer default(0), not null +# diary_entries_count :integer default(0), not null +# image_use_gravatar :boolean default(FALSE), not null +# auth_provider :string +# home_tile :bigint(8) +# tou_agreed :datetime +# diary_comments_count :integer default(0) +# note_comments_count :integer default(0) # # Indexes # diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 2c40cfebe..c73581b25 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -14,7 +14,8 @@ <%= number_with_delimiter(current_user.changesets.size) %>
  • - <%= link_to t(".my notes"), user_notes_path(@user) %> + <%= link_to t(".my notes"), user_notes_path(current_user) %> + <%= number_with_delimiter(current_user.note_comments.size) %>
  • <%= link_to t(".my traces"), :controller => "traces", :action => "mine" %> @@ -26,6 +27,7 @@
  • <%= link_to t(".my comments"), diary_comments_path(current_user) %> + <%= number_with_delimiter(current_user.diary_comments.size) %>
  • <%= link_to t(".my settings"), edit_account_path %> @@ -59,6 +61,7 @@
  • <%= link_to t(".notes"), user_notes_path(@user) %> + <%= number_with_delimiter(@user.note_comments.size) %>
  • <%= link_to t(".traces"), :controller => "traces", :action => "index", :display_name => @user.display_name %> @@ -76,6 +79,7 @@
  • <%= link_to t(".comments"), diary_comments_path(@user) %> + <%= number_with_delimiter(@user.diary_comments.size) %>
  • <% if current_user and current_user.friends_with?(@user) %> diff --git a/db/migrate/20240605134916_add_notes_and_diary_comments_counter_caches.rb b/db/migrate/20240605134916_add_notes_and_diary_comments_counter_caches.rb new file mode 100644 index 000000000..6b30071bd --- /dev/null +++ b/db/migrate/20240605134916_add_notes_and_diary_comments_counter_caches.rb @@ -0,0 +1,30 @@ +class AddNotesAndDiaryCommentsCounterCaches < ActiveRecord::Migration[7.1] + class DiaryComment < ApplicationRecord + end + + class NoteComment < ApplicationRecord + end + + class User < ApplicationRecord + end + + def self.up + add_column :users, :diary_comments_count, :integer, :default => 0 + add_column :users, :note_comments_count, :integer, :default => 0 + + users_with_diary_comments = DiaryComment.distinct.pluck(:user_id) + users_with_diary_comments.each do |user_id| + User.reset_counters(user_id, :diary_comments) + end + + users_with_note_comments = NoteComment.where.not(:author_id => nil).distinct.pluck(:author_id) + users_with_note_comments.each do |author_id| + User.reset_counters(author_id, :note_comments) + end + end + + def self.down + remove_column :users, :diary_comments_count + remove_column :users, :note_comments_count + end +end diff --git a/db/structure.sql b/db/structure.sql index 829595e7c..2e8bdbb61 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1565,7 +1565,9 @@ CREATE TABLE public.users ( image_use_gravatar boolean DEFAULT false NOT NULL, auth_provider character varying, home_tile bigint, - tou_agreed timestamp without time zone + tou_agreed timestamp without time zone, + diary_comments_count integer DEFAULT 0, + note_comments_count integer DEFAULT 0 ); @@ -3519,6 +3521,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('23'), ('22'), ('21'), +('20240605134916'), ('20240405083825'), ('20240307181018'), ('20240307180830'),