From 139973605364113f6c9b2860af73bf3c78fd04be Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Fri, 17 Feb 2023 15:58:28 +0100 Subject: [PATCH 1/3] Cache about page --- app/controllers/about_controller.rb | 32 +++++++++++++++-------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/app/controllers/about_controller.rb b/app/controllers/about_controller.rb index b957e445..259f2f10 100644 --- a/app/controllers/about_controller.rb +++ b/app/controllers/about_controller.rb @@ -4,23 +4,25 @@ class AboutController < ApplicationController def index; end def about - user_count = User - .where.not(confirmed_at: nil) - .where("answered_count > 0") - .count + cache "about_counters", expires_in: 1.hour do + user_count = User + .where.not(confirmed_at: nil) + .where("answered_count > 0") + .count - current_ban_count = UserBan - .current - .joins(:user) - .where.not("users.confirmed_at": nil) - .where("users.answered_count > 0") - .count + current_ban_count = UserBan + .current + .joins(:user) + .where.not("users.confirmed_at": nil) + .where("users.answered_count > 0") + .count - @users = user_count - current_ban_count - @questions = Question.count - @answers = Answer.count - @comments = Comment.count - @smiles = Appendable::Reaction.count + @users = user_count - current_ban_count + @questions = Question.count(:id) + @answers = Answer.count(:id) + @comments = Comment.count(:id) + @smiles = Appendable::Reaction.count + end end def privacy_policy; end From 7635d3a491a9df45b60fb0390c8eedc1b8163e68 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Fri, 17 Feb 2023 15:58:46 +0100 Subject: [PATCH 2/3] Humanise numbers on about page --- app/views/about/about.html.haml | 6 +++--- config/locales/units.en.yml | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 config/locales/units.en.yml diff --git a/app/views/about/about.html.haml b/app/views/about/about.html.haml index 5dd5ba2b..6dcc22a4 100644 --- a/app/views/about/about.html.haml +++ b/app/views/about/about.html.haml @@ -17,13 +17,13 @@ %h2= t(".statistics.header") %p= t(".statistics.body", app_name: APP_CONFIG["site_name"]) .entry - .entry__value= @questions + .entry__value{ title: number_to_human(@questions) }= number_to_human @questions, units: :short, format: "%n%u" %h4.entry__description= Question.model_name.human(count: @questions) .entry - .entry__value= @answers + .entry__value{ title: number_to_human(@answers) }= number_to_human @answers, units: :short, format: "%n%u" %h4.entry__description= Answer.model_name.human(count: @answers) .entry - .entry__value= @users + .entry__value{ title: number_to_human(@users) }= number_to_human @users, units: :short, format: "%n%u" %h4.entry__description= User.model_name.human(count: @users) = render "shared/links" diff --git a/config/locales/units.en.yml b/config/locales/units.en.yml new file mode 100644 index 00000000..2494fcb1 --- /dev/null +++ b/config/locales/units.en.yml @@ -0,0 +1,7 @@ +en: + short: + unit: "" + thousand: "K" + million: "M" + billion: "B" + trillion: "T" From 5a9909adae12853b969ea295ea2635159ec6cd30 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sat, 18 Feb 2023 10:55:35 +0100 Subject: [PATCH 3/3] Move user counts to methods --- app/controllers/about_controller.rb | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/app/controllers/about_controller.rb b/app/controllers/about_controller.rb index 259f2f10..f6d02c9c 100644 --- a/app/controllers/about_controller.rb +++ b/app/controllers/about_controller.rb @@ -5,18 +5,6 @@ class AboutController < ApplicationController def about cache "about_counters", expires_in: 1.hour do - user_count = User - .where.not(confirmed_at: nil) - .where("answered_count > 0") - .count - - current_ban_count = UserBan - .current - .joins(:user) - .where.not("users.confirmed_at": nil) - .where("users.answered_count > 0") - .count - @users = user_count - current_ban_count @questions = Question.count(:id) @answers = Answer.count(:id) @@ -28,4 +16,18 @@ class AboutController < ApplicationController def privacy_policy; end def terms; end + + private + + def user_count = User + .where.not(confirmed_at: nil) + .where("answered_count > 0") + .count + + def current_ban_count = UserBan + .current + .joins(:user) + .where.not("users.confirmed_at": nil) + .where("users.answered_count > 0") + .count end