diff --git a/app/assets/stylesheets/stats.scss b/app/assets/stylesheets/stats.scss index 2992e5d05..543c01433 100644 --- a/app/assets/stylesheets/stats.scss +++ b/app/assets/stylesheets/stats.scss @@ -81,4 +81,66 @@ border-bottom-left-radius: rem-calc(20); border-top-left-radius: rem-calc(20); } + + .gender-and-channel { + thead, + tbody { + border: 0; + } + + thead, + tr, + th { + background-color: transparent; + } + + td, + th { + text-align: center; + text-transform: uppercase; + + &::before { + display: inline-block; + transform: scale(0.6); + vertical-align: middle; + } + } + + th { + font-weight: bold; + font-size: rem-calc(21); + + &.gender { + opacity: 0; + } + + &.web::before { + content: image-url('stats_web.png'); + } + + &.booth::before { + content: image-url('stats_booth.png'); + } + + &.mail::before { + content: image-url('stats_mail.png'); + } + } + + td { + &.gender { + font-weight: bold; + font-size: rem-calc(21); + text-align: left; + } + + &.male::before { + content: image-url('stats_man.png'); + } + + &.female::before { + content: image-url('stats_woman.png'); + } + } + } } diff --git a/app/models/concerns/statisticable.rb b/app/models/concerns/statisticable.rb index 2d588a59a..a47826aa3 100644 --- a/app/models/concerns/statisticable.rb +++ b/app/models/concerns/statisticable.rb @@ -13,11 +13,11 @@ module Statisticable end def total_male_participants - participants.where(gender: "male").count + participants.male.count end def total_female_participants - participants.where(gender: "female").count + participants.female.count end def total_unknown_gender_or_age diff --git a/app/models/poll/stats.rb b/app/models/poll/stats.rb index 191139d5a..dda2f74c1 100644 --- a/app/models/poll/stats.rb +++ b/app/models/poll/stats.rb @@ -14,7 +14,11 @@ class Poll::Stats total_participants_mail_percentage valid_percentage_web valid_percentage_booth valid_percentage_mail total_valid_percentage white_percentage_web white_percentage_booth white_percentage_mail total_white_percentage - null_percentage_web null_percentage_booth null_percentage_mail total_null_percentage] + null_percentage_web null_percentage_booth null_percentage_mail total_null_percentage + total_male_web total_male_booth total_male_mail + total_female_web total_female_booth total_female_mail + male_web_percentage male_booth_percentage male_mail_percentage + female_web_percentage female_booth_percentage female_mail_percentage] end def total_participants @@ -31,6 +35,23 @@ class Poll::Stats define_method :"total_participants_#{channel}_percentage" do calculate_percentage(send(:"total_participants_#{channel}"), total_participants) end + + define_method :"#{channel}_participants" do + User.where(id: voters.where(origin: channel).pluck(:user_id)) + end + + %i[male female].each do |gender| + define_method :"total_#{gender}_#{channel}" do + send(:"#{channel}_participants").public_send(gender).count + end + + define_method :"#{gender}_#{channel}_percentage" do + calculate_percentage( + send(:"total_#{gender}_#{channel}"), + send(:"total_#{gender}_participants") + ) + end + end end def total_web_valid diff --git a/app/models/user.rb b/app/models/user.rb index 35bff958a..948cc4082 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -55,6 +55,8 @@ class User < ApplicationRecord scope :moderators, -> { joins(:moderator) } scope :organizations, -> { joins(:organization) } scope :officials, -> { where("official_level > 0") } + scope :male, -> { where(gender: "male") } + scope :female, -> { where(gender: "female") } scope :newsletter, -> { where(newsletter: true) } scope :for_render, -> { includes(:organization) } scope :by_document, ->(document_type, document_number) do diff --git a/app/views/polls/stats.html.erb b/app/views/polls/stats.html.erb index 216a1a2e8..7fdbed498 100644 --- a/app/views/polls/stats.html.erb +++ b/app/views/polls/stats.html.erb @@ -14,6 +14,9 @@
| + <% Poll::Stats::CHANNELS.each do |channel| %> + | <%= t("stats.polls.#{channel}") %> | + <% end %> +
|---|---|
| <%= t("stats.#{gender}") %> | + <% Poll::Stats::CHANNELS.each do |channel| %> ++ <%= "#{@stats[:"total_#{gender}_#{channel}"]} + (#{number_to_stats_percentage(@stats[:"#{gender}_#{channel}_percentage"])})" %> + | + <% end %> +