From 76c7827cf42e0cf9a387393857314802fe3c983d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 18 Mar 2019 15:38:53 +0100 Subject: [PATCH] Use stats objects instead of hashes It will make it far easier to call other methods on the stats object, and we're already caching the methods. We had to remove the view fragment caching because the stats object isn't as easy to cache. The good thing about it is the view will automatically be updated when we change logic regarding which stats to show, and the methods taking long to execute are cached in the model. --- app/controllers/budgets/stats_controller.rb | 6 +- app/controllers/polls_controller.rb | 2 +- app/models/budget/stats.rb | 102 +++---- app/models/concerns/statisticable.rb | 2 +- app/views/budgets/stats/show.html.erb | 272 +++++++++--------- app/views/polls/stats.html.erb | 34 +-- .../shared/stats/_participation.html.erb | 14 +- spec/models/budget/stats_spec.rb | 60 ++-- spec/models/poll/stats_spec.rb | 42 +-- 9 files changed, 244 insertions(+), 290 deletions(-) diff --git a/app/controllers/budgets/stats_controller.rb b/app/controllers/budgets/stats_controller.rb index 080bf0ef9..d8ac1e267 100644 --- a/app/controllers/budgets/stats_controller.rb +++ b/app/controllers/budgets/stats_controller.rb @@ -6,16 +6,12 @@ module Budgets def show authorize! :read_stats, @budget - @stats = load_stats + @stats = Budget::Stats.new(@budget) @headings = @budget.headings.sort_by_name end private - def load_stats - Budget::Stats.new(@budget).generate - end - def load_budget @budget = Budget.find_by(slug: params[:budget_id]) || Budget.find_by(id: params[:budget_id]) end diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index 790045b0f..60e1e4eff 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -33,7 +33,7 @@ class PollsController < ApplicationController end def stats - @stats = Poll::Stats.new(@poll).generate + @stats = Poll::Stats.new(@poll) end def results diff --git a/app/models/budget/stats.rb b/app/models/budget/stats.rb index e063756c4..a61013fe6 100644 --- a/app/models/budget/stats.rb +++ b/app/models/budget/stats.rb @@ -13,36 +13,59 @@ class Budget::Stats User.where(id: (authors + voters + balloters + poll_ballot_voters).uniq.compact) end + def total_participants + participants.distinct.count + end + + def total_participants_support_phase + voters.uniq.count + end + + def total_participants_vote_phase + (balloters + poll_ballot_voters).uniq.count + end + + def total_budget_investments + budget.investments.count + end + + def total_votes + budget.ballots.pluck(:ballot_lines_count).inject(0) { |sum, x| sum + x } + end + + def total_selected_investments + budget.investments.selected.count + end + + def total_unfeasible_investments + budget.investments.unfeasible.count + end + + def headings + groups = Hash.new(0) + budget.headings.order("id ASC").each do |heading| + groups[heading.id] = Hash.new(0).merge(calculate_heading_totals(heading)) + end + + groups[:total] = Hash.new(0) + groups[:total][:total_investments_count] = groups.collect {|_k, v| v[:total_investments_count]}.sum + groups[:total][:total_participants_support_phase] = groups.collect {|_k, v| v[:total_participants_support_phase]}.sum + groups[:total][:total_participants_vote_phase] = groups.collect {|_k, v| v[:total_participants_vote_phase]}.sum + groups[:total][:total_participants_all_phase] = groups.collect {|_k, v| v[:total_participants_all_phase]}.sum + + budget.headings.each do |heading| + groups[heading.id].merge!(calculate_heading_stats_with_totals(groups[heading.id], groups[:total], heading.population)) + end + + groups[:total][:percentage_participants_support_phase] = groups.collect {|_k, v| v[:percentage_participants_support_phase]}.sum + groups[:total][:percentage_participants_vote_phase] = groups.collect {|_k, v| v[:percentage_participants_vote_phase]}.sum + groups[:total][:percentage_participants_all_phase] = groups.collect {|_k, v| v[:percentage_participants_all_phase]}.sum + + groups + end + private - def total_participants - participants.distinct.count - end - - def total_participants_support_phase - voters.uniq.count - end - - def total_participants_vote_phase - (balloters + poll_ballot_voters).uniq.count - end - - def total_budget_investments - budget.investments.count - end - - def total_votes - budget.ballots.pluck(:ballot_lines_count).inject(0) { |sum, x| sum + x } - end - - def total_selected_investments - budget.investments.selected.count - end - - def total_unfeasible_investments - budget.investments.unfeasible.count - end - def authors budget.investments.pluck(:author_id) end @@ -71,29 +94,6 @@ class Budget::Stats end end - def headings - groups = Hash.new(0) - budget.headings.order("id ASC").each do |heading| - groups[heading.id] = Hash.new(0).merge(calculate_heading_totals(heading)) - end - - groups[:total] = Hash.new(0) - groups[:total][:total_investments_count] = groups.collect {|_k, v| v[:total_investments_count]}.sum - groups[:total][:total_participants_support_phase] = groups.collect {|_k, v| v[:total_participants_support_phase]}.sum - groups[:total][:total_participants_vote_phase] = groups.collect {|_k, v| v[:total_participants_vote_phase]}.sum - groups[:total][:total_participants_all_phase] = groups.collect {|_k, v| v[:total_participants_all_phase]}.sum - - budget.headings.each do |heading| - groups[heading.id].merge!(calculate_heading_stats_with_totals(groups[heading.id], groups[:total], heading.population)) - end - - groups[:total][:percentage_participants_support_phase] = groups.collect {|_k, v| v[:percentage_participants_support_phase]}.sum - groups[:total][:percentage_participants_vote_phase] = groups.collect {|_k, v| v[:percentage_participants_vote_phase]}.sum - groups[:total][:percentage_participants_all_phase] = groups.collect {|_k, v| v[:percentage_participants_all_phase]}.sum - - groups - end - def calculate_heading_totals(heading) { total_investments_count: heading.investments.count, diff --git a/app/models/concerns/statisticable.rb b/app/models/concerns/statisticable.rb index 980bda637..411bec21c 100644 --- a/app/models/concerns/statisticable.rb +++ b/app/models/concerns/statisticable.rb @@ -9,7 +9,7 @@ module Statisticable end def generate - self.class.stats_methods.map { |stat_name| [stat_name, send(stat_name)] }.to_h + self.class.stats_methods.each { |stat_name| send(stat_name) } end def total_male_participants diff --git a/app/views/budgets/stats/show.html.erb b/app/views/budgets/stats/show.html.erb index d4064ab90..7edf44342 100644 --- a/app/views/budgets/stats/show.html.erb +++ b/app/views/budgets/stats/show.html.erb @@ -8,158 +8,156 @@ social_description: @budget.description_finished %> <% end %> -<% cache [@stats] do %> -
-
-
-
- <%= back_link_to budgets_path %> -

- <%= t("stats.title") %>
- <%= @budget.name %> -

-
-
-
- -
+
+
+
-
    -
  • - <%= t("shared.you_are_in") %> - <%= link_to t("budgets.results.link"), budget_results_path(@budget) %> -
  • -
  • - <%= link_to t("stats.budgets.link"), budget_stats_path(@budget), class: "is-active" %> -
  • -
  • - <%= link_to t("budgets.executions.link"), budget_executions_path(@budget) %> -
  • -
+ <%= back_link_to budgets_path %> +

+ <%= t("stats.title") %>
+ <%= @budget.name %> +

+
-
- +
+