diff --git a/app/controllers/polls/stats_controller.rb b/app/controllers/polls/stats_controller.rb deleted file mode 100644 index 4d6a40ff3..000000000 --- a/app/controllers/polls/stats_controller.rb +++ /dev/null @@ -1,20 +0,0 @@ -class Polls::StatsController < ApplicationController - - before_action :load_poll - load_and_authorize_resource :poll - - def show - authorize! :read_stats, @poll - @stats = load_stats - end - - private - - def load_stats - Poll::Stats.new(@poll).generate - end - - def load_poll - @poll = Poll.find_by(id: params[:id]) - end -end \ No newline at end of file diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index 338f72cc4..a13bfd33d 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -25,6 +25,8 @@ class PollsController < ApplicationController @commentable = @poll @comment_tree = CommentTree.new(@commentable, params[:page], @current_order) + + @stats = Poll::Stats.new(@poll).generate end end diff --git a/app/models/poll/stats.rb b/app/models/poll/stats.rb index 823724ade..1eef537a0 100644 --- a/app/models/poll/stats.rb +++ b/app/models/poll/stats.rb @@ -6,26 +6,115 @@ class Poll end def generate - stats = %w[total_participants total_participants_web total_participants_booth] + stats = %w[total_participants total_participants_web total_web_valid total_web_white total_web_null + total_participants_booth total_booth_valid total_booth_white total_booth_null + total_valid_votes total_white_votes total_null_votes valid_percentage_web valid_percentage_booth + total_valid_percentage white_percentage_web white_percentage_booth total_white_percentage + null_percentage_web null_percentage_booth total_null_percentage total_participants_web_percentage + total_participants_booth_percentage] stats.map { |stat_name| [stat_name.to_sym, send(stat_name)] }.to_h end private - + def total_participants - stats_cache('total_participants') { voters.uniq.count } + total_participants_web + total_participants_booth end def total_participants_web - stats_cache('total_participants_web') { voters.where(origin: 'web').count } + total_web_valid + total_web_white + total_web_null + end + + def total_participants_web_percentage + total_participants_web * 100 / total_participants end def total_participants_booth - stats_cache('total_participants_booth') { voters.where(origin: 'booth').count } + voters.where(origin: 'booth').count + end + + def total_participants_booth_percentage + (total_participants) == 0 ? 0 : total_participants_booth * 100 / total_participants.to_f + end + + def total_web_valid + voters.where(origin: 'web').count + end + + def valid_percentage_web + (total_valid_votes) == 0 ? 0 : total_web_valid * 100 / total_valid_votes.to_f + end + + def total_web_white + 0 + end + + def white_percentage_web + 0 + end + + def total_web_null + 0 + end + + def null_percentage_web + 0 + end + + def total_booth_valid + recounts.sum(:total_amount) + end + + def valid_percentage_booth + (total_valid_votes) == 0 ? 0 : total_booth_valid * 100 / total_valid_votes.to_f + end + + def total_booth_white + recounts.sum(:white_amount) + end + + def white_percentage_booth + (total_white_votes) == 0 ? 0 : total_booth_white * 100 / total_white_votes.to_f + end + + def total_booth_null + recounts.sum(:null_amount) + end + + def null_percentage_booth + (total_null_votes == 0) ? 0 : total_booth_null * 100 / total_null_votes.to_f + end + + def total_valid_votes + total_web_valid + total_booth_valid + end + + def total_valid_percentage + (total_participants) == 0 ? 0 : total_valid_votes * 100 / total_participants.to_f + end + + def total_white_votes + total_web_white + total_booth_white + end + + def total_white_percentage + (total_participants) == 0 ? 0 : total_white_votes * 100 / total_participants.to_f + end + + def total_null_votes + total_web_null + total_booth_null + end + + def total_null_percentage + (total_participants) == 0 ? 0 : total_null_votes * 100 / total_participants.to_f end def voters - stats_cache('voters') { @poll.voters } + @poll.voters + end + + def recounts + @poll.recounts end def stats_cache(key, &block) diff --git a/app/views/polls/_results_subnavigation.html.erb b/app/views/polls/_results_subnavigation.html.erb new file mode 100644 index 000000000..afff4de03 --- /dev/null +++ b/app/views/polls/_results_subnavigation.html.erb @@ -0,0 +1,20 @@ +
+ <%= t("polls.show.stats.total_votes") %>
+ <%= @stats[:total_participants] %>
+
| <%= t("polls.show.stats.votes") %> | +<%= t("polls.show.stats.web") %> | +<%= t("polls.show.stats.booth") %> | +<%= t("polls.show.stats.total") %> | +
|---|---|---|---|
| <%= t("polls.show.stats.valid") %> | +<%= @stats[:total_web_valid] %> + (<%= number_to_percentage(@stats[:valid_percentage_web], + strip_insignificant_zeros: true, + precision: 2) %>) | +<%= @stats[:total_booth_valid] %> + (<%= number_to_percentage(@stats[:valid_percentage_booth], + strip_insignificant_zeros: true, + precision: 2) %>) | +<%= @stats[:total_valid_votes] %> + (<%= number_to_percentage(@stats[:total_valid_percentage], + strip_insignificant_zeros: true, + precision: 2) %>) | +
| <%= t("polls.show.stats.white") %> | +<%= @stats[:total_web_white] %> + (<%= number_to_percentage(@stats[:white_percentage_web], + strip_insignificant_zeros: true, + precision: 2) %>) | +<%= @stats[:total_booth_white] %> + (<%= number_to_percentage(@stats[:white_percentage_booth], + strip_insignificant_zeros: true, + precision: 2) %>) | +<%= @stats[:total_white_votes] %> + (<%= number_to_percentage(@stats[:total_white_percentage], + strip_insignificant_zeros: true, + precision: 2) %>) | +
| <%= t("polls.show.stats.null_votes") %> | +<%= @stats[:total_web_null] %> + (<%= number_to_percentage(@stats[:null_percentage_web], + strip_insignificant_zeros: true, + precision: 2) %>) | +<%= @stats[:total_booth_null] %> + (<%= number_to_percentage(@stats[:null_percentage_booth], + strip_insignificant_zeros: true, + precision: 2) %>) | +<%= @stats[:total_null_votes] %> + (<%= number_to_percentage(@stats[:total_null_percentage], + strip_insignificant_zeros: true, + precision: 2) %>) | +
| <%= t("polls.show.stats.total") %> | +<%= @stats[:total_participants_web] %> + (<%= number_to_percentage(@stats[:total_participants_web_percentage], + strip_insignificant_zeros: true, + precision: 2) %>) | +<%= @stats[:total_participants_booth] %> + (<%= number_to_percentage(@stats[:total_participants_booth_percentage], + strip_insignificant_zeros: true, + precision: 2) %>) | +<%= @stats[:total_participants_web] + @stats[:total_participants_booth] %> | +
- <%= @stats[:total_participants] %> -
-| Web | -Booth | -
| <%= @stats[:total_participants_web] %> | -<%= @stats[:total_participants_booth] %> | -
- <%= @stats[:total_votes] %> -
-