From 6639f6646a01220623f3fd3f0b15b646fb66ef65 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 3 Oct 2021 20:40:25 +0200 Subject: [PATCH 1/4] only count active users on about page --- app/controllers/static_controller.rb | 11 +++++++++++ app/views/static/about.haml | 20 ++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb index 8854f0e4..fe888207 100644 --- a/app/controllers/static_controller.rb +++ b/app/controllers/static_controller.rb @@ -17,7 +17,18 @@ class StaticController < ApplicationController end def about + @users = User + .where('confirmed_at IS NOT NULL') + .where(permanently_banned: false) + .where(banned_until: nil) + .where('answered_count > 0') + .where('asked_count > 0') + .count + @questions = Question.count + @answers = Answer.count + @comments = Comment.count + @smiles = Smile.count + CommentSmile.count end def faq diff --git a/app/views/static/about.haml b/app/views/static/about.haml index f6ad2c21..34ba6762 100644 --- a/app/views/static/about.haml +++ b/app/views/static/about.haml @@ -48,21 +48,21 @@ %p= t('views.about.statistics.desc', app_title: APP_CONFIG['site_name']) .col-md-3.col-sm-6.col-xs-6 .entry.entry--statistics - %h2.entry__value#asked-count= Question.count - %h4.entry__description= t('views.general.question').pluralize(Question.count) + %h2.entry__value#asked-count= @questions + %h4.entry__description= t('views.general.question').pluralize(@questions) .entry.entry--statistics - %h2.entry__value#answered-count= Answer.count - %h4.entry__description= t('views.general.answer').pluralize(Answer.count) + %h2.entry__value#answered-count= @answers + %h4.entry__description= t('views.general.answer').pluralize(@answers) .col-md-3.col-sm-6.col-xs-6 .entry.entry--statistics - %h2.entry__value#comment-count= Comment.count - %h4.entry__description= t('views.general.comment').pluralize(Comment.count) + %h2.entry__value#comment-count= @comments + %h4.entry__description= t('views.general.comment').pluralize(@comments) .entry.entry--statistics - %h2.entry__value#smile-count= Smile.count + CommentSmile.count - %h4.entry__description= t('views.general.smile').pluralize(Smile.count) + %h2.entry__value#smile-count= @smiles + %h4.entry__description= t('views.general.smile').pluralize(@smiles) .col-md-3.col-sm-12.col-xs-12 .entry.entry--users - .entry__value#follower-count= User.count - %h4.entry__description= t('views.general.user').pluralize(User.count) + .entry__value#follower-count= @users + %h4.entry__description= t('views.general.user').pluralize(@users) = render 'shared/links' From 58588d22b15313fa34fcc3daa175c56e90dffd39 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 3 Oct 2021 20:49:33 +0200 Subject: [PATCH 2/4] use .where.not instead of in-query condition for user confirm date --- app/controllers/static_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb index fe888207..c98501a2 100644 --- a/app/controllers/static_controller.rb +++ b/app/controllers/static_controller.rb @@ -18,7 +18,7 @@ class StaticController < ApplicationController def about @users = User - .where('confirmed_at IS NOT NULL') + .where.not(confirmed_at: nil) .where(permanently_banned: false) .where(banned_until: nil) .where('answered_count > 0') From 85210589cbb85e62f8cf3551110895c8eb13e164 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 3 Oct 2021 21:26:46 +0200 Subject: [PATCH 3/4] add test case for user count on about page --- spec/controllers/static_controller_spec.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 spec/controllers/static_controller_spec.rb diff --git a/spec/controllers/static_controller_spec.rb b/spec/controllers/static_controller_spec.rb new file mode 100644 index 00000000..02b08787 --- /dev/null +++ b/spec/controllers/static_controller_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe StaticController, type: :controller do + describe "#about" do + subject { get :about } + + before(:each) { + User.create(screen_name: 'very_valid_user', confirmed_at: Time.now, answered_count: 1, asked_count: 1) + User.create(screen_name: 'big_ben', permanently_banned: true) + User.create(screen_name: 'youre_mom', banned_until: Time.now + 10.days) + User.create(screen_name: 'silence_fox', confirmed_at: Time.now) + } + + it "shows the correct user count" do + subject + expect(assigns(:users)).to eq(1) + end + end +end \ No newline at end of file From f8af42b16fbe0d436c8180d1e17a35c392b38138 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 3 Oct 2021 21:52:07 +0200 Subject: [PATCH 4/4] fix user creation in test case --- spec/controllers/static_controller_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/controllers/static_controller_spec.rb b/spec/controllers/static_controller_spec.rb index 02b08787..0a79ec39 100644 --- a/spec/controllers/static_controller_spec.rb +++ b/spec/controllers/static_controller_spec.rb @@ -7,10 +7,10 @@ describe StaticController, type: :controller do subject { get :about } before(:each) { - User.create(screen_name: 'very_valid_user', confirmed_at: Time.now, answered_count: 1, asked_count: 1) - User.create(screen_name: 'big_ben', permanently_banned: true) - User.create(screen_name: 'youre_mom', banned_until: Time.now + 10.days) - User.create(screen_name: 'silence_fox', confirmed_at: Time.now) + FactoryBot.create(:user, { confirmed_at: Time.now, answered_count: 1, asked_count: 1 }) + FactoryBot.create(:user, { permanently_banned: true }) + FactoryBot.create(:user, { banned_until: Time.now + 10.days }) + FactoryBot.create(:user, { confirmed_at: Time.now }) } it "shows the correct user count" do