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 %> -