From 9335c51cfcf9f2d57caec4f3b922de6e2ef584d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 4 Apr 2019 18:38:28 +0200 Subject: [PATCH] Include hidden users in stats If users participated and were hidden after participating, we should still count them in the participants stats. In the tests, we set users' `hidden_at` attribute before they vote. Although in real life they would vote first and then they would be hidden, I've written the tests like this for the sake of simplicity. --- app/models/concerns/statisticable.rb | 2 +- spec/models/budget/stats_spec.rb | 6 +++--- spec/models/poll/stats_spec.rb | 9 +++++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/models/concerns/statisticable.rb b/app/models/concerns/statisticable.rb index cb1860c9d..05a8c8bff 100644 --- a/app/models/concerns/statisticable.rb +++ b/app/models/concerns/statisticable.rb @@ -34,7 +34,7 @@ module Statisticable end def participants - User.where(id: participant_ids) + User.unscoped.where(id: participant_ids) end def total_male_participants diff --git a/spec/models/budget/stats_spec.rb b/spec/models/budget/stats_spec.rb index 2a154f448..b67d23ddf 100644 --- a/spec/models/budget/stats_spec.rb +++ b/spec/models/budget/stats_spec.rb @@ -7,10 +7,10 @@ describe Budget::Stats do describe "#participants" do let(:author) { investment.author } - let(:author_and_voter) { create(:user) } + let(:author_and_voter) { create(:user, hidden_at: Time.current) } let(:voter) { create(:user) } let(:voter_and_balloter) { create(:user) } - let(:balloter) { create(:user) } + let(:balloter) { create(:user, hidden_at: Time.current) } let(:poll_balloter) { create(:user, :level_two) } let(:non_participant) { create(:user, :level_two) } @@ -29,7 +29,7 @@ describe Budget::Stats do create(:poll_voter, :from_booth, user: non_participant, budget: create(:budget)) end - it "returns unique participants, including authors" do + it "returns unique participants, including authors and hidden users" do expect(stats.participants).to match_array( [author, author_and_voter, voter, voter_and_balloter, balloter, poll_balloter] ) diff --git a/spec/models/poll/stats_spec.rb b/spec/models/poll/stats_spec.rb index e55d51169..335d72181 100644 --- a/spec/models/poll/stats_spec.rb +++ b/spec/models/poll/stats_spec.rb @@ -4,6 +4,15 @@ describe Poll::Stats do let(:poll) { create(:poll) } let(:stats) { Poll::Stats.new(poll) } + describe "#participants" do + it "includes hidden users" do + create(:poll_voter, poll: poll) + create(:poll_voter, poll: poll, user: create(:user, :level_two, hidden_at: Time.current)) + + expect(stats.participants.count).to eq(2) + end + end + describe "total participants" do before { allow(stats).to receive(:total_web_white).and_return(1) }