From 793bfed37298fdb96048efa53e7c9396c511fb67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 18 Mar 2019 18:07:09 +0100 Subject: [PATCH] Display only existing stats So if we don't have information regarding gender, age or geozone, stats regarding those topics will not be shown. Note we're using `spec/models/statisticable_spec.rb` because having the same file in `spec/models/concerns` caused the tests to be executed twice. Also note the implementation behind the `gender?`, `age?` and `geozone?` methods is a bit primitive. We might need to make it more robust in the future. --- app/models/concerns/statisticable.rb | 57 ++++++- app/views/budgets/stats/show.html.erb | 2 +- app/views/polls/stats.html.erb | 2 +- app/views/shared/stats/_age.html.erb | 28 ++++ app/views/shared/stats/_gender.html.erb | 15 ++ app/views/shared/stats/_geozone.html.erb | 23 +++ app/views/shared/stats/_links.html.erb | 15 +- .../shared/stats/_participation.html.erb | 72 +-------- spec/models/statisticable_spec.rb | 148 ++++++++++++++++++ 9 files changed, 277 insertions(+), 85 deletions(-) create mode 100644 app/views/shared/stats/_age.html.erb create mode 100644 app/views/shared/stats/_gender.html.erb create mode 100644 app/views/shared/stats/_geozone.html.erb create mode 100644 spec/models/statisticable_spec.rb diff --git a/app/models/concerns/statisticable.rb b/app/models/concerns/statisticable.rb index 411bec21c..5c6289ed1 100644 --- a/app/models/concerns/statisticable.rb +++ b/app/models/concerns/statisticable.rb @@ -1,5 +1,6 @@ module Statisticable extend ActiveSupport::Concern + PARTICIPATIONS = %w[gender age geozone] included do attr_reader :resource @@ -9,7 +10,27 @@ module Statisticable end def generate - self.class.stats_methods.each { |stat_name| send(stat_name) } + stats_methods.each { |stat_name| send(stat_name) } + end + + def stats_methods + base_stats_methods + participation_methods + end + + def participations + PARTICIPATIONS.select { |participation| send("#{participation}?") } + end + + def gender? + participants.male.any? || participants.female.any? + end + + def age? + participants.between_ages(age_groups.flatten.min, age_groups.flatten.max).any? + end + + def geozone? + participants.where(geozone: geozones).any? end def total_male_participants @@ -66,6 +87,14 @@ module Statisticable private + def base_stats_methods + self.class.base_stats_methods + end + + def participation_methods + participations.map { |participation| self.class.send("#{participation}_methods") }.flatten + end + def total_participants_with_gender participants.where.not(gender: nil).distinct.count end @@ -115,10 +144,28 @@ module Statisticable class_methods do def stats_methods - %i[total_participants - total_male_participants total_female_participants total_unknown_gender_or_age - male_percentage female_percentage - participants_by_age participants_by_geozone] + base_stats_methods + gender_methods + age_methods + geozone_methods + end + + def base_stats_methods + %i[total_participants participations] + participation_check_methods + end + + def participation_check_methods + PARTICIPATIONS.map { |participation| :"#{participation}?" } + end + + def gender_methods + %i[total_male_participants total_female_participants total_unknown_gender_or_age + male_percentage female_percentage] + end + + def age_methods + [:participants_by_age] + end + + def geozone_methods + [:participants_by_geozone] end def stats_cache(*method_names) diff --git a/app/views/budgets/stats/show.html.erb b/app/views/budgets/stats/show.html.erb index 7edf44342..5ce48bd88 100644 --- a/app/views/budgets/stats/show.html.erb +++ b/app/views/budgets/stats/show.html.erb @@ -40,7 +40,7 @@