Skip to content

Commit 2f71f13

Browse files
committed
fix counting newbies on dashboard
1 parent f8862f5 commit 2f71f13

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

app/controllers/activities_controller.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,32 @@ def show
2727
end
2828

2929
def dashboard
30-
@total_results = Result.joins(:activity).where(activity: weekly_activities)
30+
@total_results = Result.where(activity_id: weekly_activities_ids)
3131
@personal_bests_count = @total_results.where(personal_best: true).count
32-
@first_runs_count = @total_results.where(first_run: true).count
32+
@first_runs_count = count_newbies(model: Result, current_ds: @total_results)
3333

34-
@total_volunteers = Volunteer.joins(:activity).where(activity: weekly_activities)
35-
@first_time_volunteers_count = count_first_time_volunteers(@total_volunteers)
34+
@total_volunteers = Volunteer.where(activity_id: weekly_activities_ids)
35+
@first_time_volunteers_count = count_newbies(model: Volunteer, current_ds: @total_volunteers)
3636

3737
@gender_stats = @total_results.left_joins(:athlete).group(:gender).count
3838
end
3939

4040
private
4141

42-
def weekly_activities
43-
@weekly_activities ||=
42+
def weekly_activities_ids
43+
@weekly_activities_ids ||=
4444
begin
4545
today = Date.current
4646
last_saturday = today.saturday? ? today : today.prev_occurring(:saturday)
4747

48-
Activity.published.joins(:event).where(event: @country_events, date: last_saturday..)
48+
Activity.published.joins(:event).where(event: @country_events, date: last_saturday..).select(:id)
4949
end
5050
end
5151

52-
def count_first_time_volunteers(current_volunteers)
53-
Volunteer
54-
.where(athlete_id: current_volunteers.select(:athlete_id))
52+
def count_newbies(model:, current_ds:)
53+
model
54+
.published
55+
.where(athlete_id: current_ds.select(:athlete_id))
5556
.group(:athlete_id)
5657
.having('count(athlete_id) = 1')
5758
.count

spec/requests/events_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
describe 'GET /events/:code_name' do
1616
before do
17-
create(:badge, kind: :participating, info: { type: 'result', threshold: 100 })
18-
create(:badge, kind: :participating, info: { type: 'volunteer', threshold: 100 })
17+
create(:participating_badge, type: 'result')
18+
create(:participating_badge, type: 'volunteer')
1919
end
2020

2121
it 'renders a successful response' do

0 commit comments

Comments
 (0)