diff --git a/app/controllers/budgets/results_controller.rb b/app/controllers/budgets/results_controller.rb index 363931d24d1..d37ecc1d58e 100644 --- a/app/controllers/budgets/results_controller.rb +++ b/app/controllers/budgets/results_controller.rb @@ -8,6 +8,7 @@ class ResultsController < ApplicationController def show authorize! :read_results, @budget @investments = Budget::Result.new(@budget, @heading).investments + @headings = @budget.headings.sort_by_name end private @@ -18,10 +19,13 @@ def load_budget def load_heading if @budget.present? - headings = @budget.headings - @heading = headings.find_by_slug_or_id(params[:heading_id]) || headings.first + @heading = @budget.headings.find_by_slug_or_id(params[:heading_id]) || default_heading end end + def default_heading + @budget.city_heading || @budget.headings.first + end + end end diff --git a/app/controllers/budgets/stats_controller.rb b/app/controllers/budgets/stats_controller.rb index 4087303596e..1810fcd2cf9 100644 --- a/app/controllers/budgets/stats_controller.rb +++ b/app/controllers/budgets/stats_controller.rb @@ -7,6 +7,7 @@ class StatsController < ApplicationController def show authorize! :read_stats, @budget @stats = load_stats + @headings = @budget.headings.sort_by_name end private diff --git a/app/controllers/spending_proposals_controller.rb b/app/controllers/spending_proposals_controller.rb index 1b6ea1a8d3d..b01f13bdcc2 100644 --- a/app/controllers/spending_proposals_controller.rb +++ b/app/controllers/spending_proposals_controller.rb @@ -94,11 +94,9 @@ def stats def results @geozone = daily_cache("geozone_geozone_#{params[:geozone_id]}") { params[:geozone_id].blank? || params[:geozone_id] == 'all' ? nil : Geozone.find(params[:geozone_id]) } @delegated_ballots = daily_cache("delegated_geozone_#{params[:geozone_id]}") { Forum.delegated_ballots } - @spending_proposals = daily_cache("sps_geozone_#{params[:geozone_id]}") { SpendingProposal.feasible.compatible.valuation_finished.by_geozone(params[:geozone_id]) } - @spending_proposals = daily_cache("sorted_sps_geozone_#{params[:geozone_id]}") { SpendingProposal.sort_by_delegated_ballots_and_price(@spending_proposals, @delegated_ballots) } - - @initial_budget = daily_cache("initial_budget_geozone_#{params[:geozone_id]}") { Ballot.initial_budget(@geozone) } - @incompatibles = daily_cache("incompatibles_geozone_#{params[:geozone_id]}") { SpendingProposal.incompatible.by_geozone(params[:geozone_id]) } + @spending_proposals = daily_cache("sorted_sps_geozone_#{params[:geozone_id]}") { Budget::Result.new(budget_2016, heading_for_geozone(params[:geozone_id])).investments } + @initial_budget = daily_cache("initial_budget_geozone_#{params[:geozone_id]}") { heading_for_geozone(params[:geozone_id]).price } + @incompatibles = daily_cache("incompatibles_geozone_#{params[:geozone_id]}") { budget_2016.investments.incompatible.by_heading(heading_for_geozone(params[:geozone_id])) } end private @@ -178,7 +176,7 @@ def total_participants_with_gender def participants stats_cache('participants') do - users = (authors + voters + balloters + delegators).uniq + users = (authors + voters + balloters).uniq User.where(id: users) end end @@ -197,48 +195,47 @@ def total_participants_vote_phase def total_supports stats_cache('total_supports') do - ActsAsVotable::Vote.where(votable_type: 'SpendingProposal').count + budget_investment_supports.count end end def total_votes stats_cache('total_votes') do - BallotLine.count + budget_2016.lines.count end end def authors - stats_cache('authors') { SpendingProposal.pluck(:author_id) } + stats_cache('authors') { budget_2016.investments.pluck(:author_id) } end def voters - stats_cache('voters') { ActsAsVotable::Vote.where(votable_type: 'SpendingProposal').pluck(:voter_id) } + stats_cache("voters") { budget_investment_supports.pluck(:voter_id) } end def voters_by_geozone(geozone_id) stats_cache("voters_geozone_#{geozone_id}") do - ActsAsVotable::Vote.where(votable_type: 'SpendingProposal', votable_id: SpendingProposal.by_geozone(geozone_id)).pluck(:voter_id) + heading = heading_for_geozone(geozone_id) + ActsAsVotable::Vote.where(votable_type: "Budget::Investment", + votable_id: budget_2016.investments.by_heading(heading.id)) + .pluck(:voter_id) end end def balloters stats_cache('balloters') do - Ballot.where('ballot_lines_count > ?', 0).pluck(:user_id) + budget_2016.ballots.where("ballot_lines_count > ?", 0).pluck(:user_id) end end def balloters_by_geozone(geozone_id) stats_cache("balloters_geozone_#{geozone_id}") do - Ballot.where('ballot_lines_count > ? AND geozone_id = ?', 0, geozone_id).pluck(:user_id) + budget_2016.lines.where(heading_id: heading_for_geozone(geozone_id).id).pluck(:user_id).uniq end end - def delegators - stats_cache('delegators') { User.where.not(representative_id: nil).pluck(:id) } - end - def total_spending_proposals - stats_cache('total_spending_proposals') { SpendingProposal.count } + stats_cache('total_spending_proposals') { budget_2016.investments.count } end def paper_spending_proposals @@ -246,11 +243,11 @@ def paper_spending_proposals end def total_feasible_spending_proposals - stats_cache('total_feasible_spending_proposals') { SpendingProposal.feasible.count } + stats_cache('total_feasible_spending_proposals') { budget_2016.investments.feasible.count } end def total_unfeasible_spending_proposals - stats_cache('total_unfeasible_spending_proposals') { SpendingProposal.unfeasible.count } + stats_cache('total_unfeasible_spending_proposals') { budget_2016.investments.unfeasible.count } end def total_male_participants @@ -356,8 +353,25 @@ def total_unknown_gender_or_age end end + def budget_2016 + Budget.where(slug: "2016").first + end + + def budget_investment_supports + ActsAsVotable::Vote.where(votable: budget_2016.investments) + end + + def heading_for_geozone(geozone_id) + if geozone_id == nil + budget_2016.headings.where(name: "Toda la ciudad").first + else + geozone = Geozone.find(geozone_id) + budget_2016.headings.where(name: geozone.name).first + end + end + def stats_cache(key, &block) - Rails.cache.fetch("spending_proposals_stats/201607131316/#{key}", &block) + Rails.cache.fetch("spending_proposals_stats/20190206172211/#{key}", &block) end end diff --git a/app/models/budget/ballot/line.rb b/app/models/budget/ballot/line.rb index f7c6618272f..52b931df68b 100644 --- a/app/models/budget/ballot/line.rb +++ b/app/models/budget/ballot/line.rb @@ -16,7 +16,7 @@ class Line < ActiveRecord::Base scope :by_investment, ->(investment_id) { where(investment_id: investment_id) } before_validation :set_denormalized_ids - after_save :store_user_heading + #after_save :store_user_heading def check_sufficient_funds errors.add(:money, "insufficient funds") if ballot.amount_available(investment.heading) < investment.price.to_i @@ -31,13 +31,13 @@ def check_selected errors.add(:investment, "unselected investment") unless investment.selected? end - private + def set_denormalized_ids + self.heading_id ||= investment.try(:heading_id) + self.group_id ||= investment.try(:group_id) + self.budget_id ||= investment.try(:budget_id) + end - def set_denormalized_ids - self.heading_id ||= investment.try(:heading_id) - self.group_id ||= investment.try(:group_id) - self.budget_id ||= investment.try(:budget_id) - end + private def store_user_heading return if heading.city_heading? diff --git a/app/models/budget/stats.rb b/app/models/budget/stats.rb index d2a71f8f51d..78399914553 100644 --- a/app/models/budget/stats.rb +++ b/app/models/budget/stats.rb @@ -149,6 +149,6 @@ def supports(supportable) stats_cache :voters, :participants, :authors, :balloters, :poll_ballot_voters def stats_cache(key, &block) - Rails.cache.fetch("budgets_stats/#{budget.id}/#{key}/v10", &block) + Rails.cache.fetch("budgets_stats/#{budget.id}/#{key}/v13", &block) end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 2ce8934218f..d36b7c0c78f 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -20,7 +20,7 @@ class Comment < ActiveRecord::Base validates :commentable_type, inclusion: { in: COMMENTABLE_TYPES } - validate :validate_body_length + validate :validate_body_length, unless: -> { valuation } validate :comment_valuation, if: -> { valuation } belongs_to :commentable, -> { with_hidden }, polymorphic: true, counter_cache: true diff --git a/app/models/spending_proposal.rb b/app/models/spending_proposal.rb index 651e406b7a8..e4a5990b3da 100644 --- a/app/models/spending_proposal.rb +++ b/app/models/spending_proposal.rb @@ -151,23 +151,7 @@ def feasible_explanation_required? end def total_votes - cached_votes_up + physical_votes + delegated_votes - forum_votes - end - - def delegated_votes - count = 0 - representative_voters.each do |voter| - count += voter.forum.represented_users.select { |u| !u.voted_for?(self) }.count - end - return count - end - - def representative_voters - Vote.representative_votes.for_spending_proposals(self).collect(&:voter) - end - - def forum_votes - Vote.representative_votes.for_spending_proposals(self).count + cached_votes_up + physical_votes end def code diff --git a/app/views/budgets/results/show.html.erb b/app/views/budgets/results/show.html.erb index 821a6405810..7cd1dc252bd 100644 --- a/app/views/budgets/results/show.html.erb +++ b/app/views/budgets/results/show.html.erb @@ -49,8 +49,8 @@

<%= t("budgets.results.heading_selection_title") %>

-