Calculate age stats based on the participation date
We were calculating the age stats based on the age of the users who participated... at the moment where we were calculating the stats. That means that, if 20 years ago, 1000 people who were 16 years old participated, they would be shown as having 36 years in the stats. Instead, we want to show the stats at the time when the process took place, so we're implementing a `participation_date` method. Note that, for polls, we could actually use the `age` column in the `poll_voters` table. However, doing so would be harder, would only work for polls but not for budgets, and it wouldn't be statistically very relevant, since the stats are shown by age groups, and only a small percentage of people would change their age group (and only to the nearest one) between the time they participate and the time the process ends. We might use the `poll_voters` table in the future, though, since we have a similar issue with geozones and genders, and using the information in `poll_voters` would solve it as well (only for polls, though). Also note that we're using the `ends_at` dates because some people but be too young to vote when a process starts but old enough to vote when the process ends. Finally, note that we might need to change the way we calculate the participation date for a budget, since some budgets might not enabled every phase. Not sure how stats work in that scenario (even before these changes).
This commit is contained in:
@@ -437,6 +437,26 @@ describe User do
|
||||
expect(User.between_ages(41, 45).count).to eq 3
|
||||
expect(User.between_ages(51, 100).count).to eq 1
|
||||
end
|
||||
|
||||
it "returns users between certain ages on a reference date" do
|
||||
reference_date = 20.years.ago
|
||||
|
||||
travel_to(reference_date) do
|
||||
[21, 22, 23, 23, 34, 42, 43, 44, 50, 51].each do |age|
|
||||
create(:user, date_of_birth: age.years.ago)
|
||||
end
|
||||
end
|
||||
|
||||
expect(User.between_ages(0, 20, at_time: reference_date).count).to eq 0
|
||||
expect(User.between_ages(21, 25, at_time: reference_date).count).to eq 4
|
||||
expect(User.between_ages(25, 30, at_time: reference_date).count).to eq 0
|
||||
expect(User.between_ages(30, 34, at_time: reference_date).count).to eq 1
|
||||
expect(User.between_ages(35, 39, at_time: reference_date).count).to eq 0
|
||||
expect(User.between_ages(40, 44, at_time: reference_date).count).to eq 3
|
||||
expect(User.between_ages(45, 49, at_time: reference_date).count).to eq 0
|
||||
expect(User.between_ages(50, 54, at_time: reference_date).count).to eq 2
|
||||
expect(User.between_ages(55, 100, at_time: reference_date).count).to eq 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user