14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'app/models/decidim/decidim_awesome/vote_weight.rb', line 14
def update_vote_weight_totals!
= Decidim::DecidimAwesome::ProposalExtraField.find_or_initialize_by(proposal:)
.vote_weight_totals = .vote_weight_totals || {}
prev = weight_previous_change&.first
if prev.present?
.vote_weight_totals[prev.to_s] = Decidim::DecidimAwesome::VoteWeight.where(vote: proposal.votes, weight: prev).count
.vote_weight_totals.delete(prev.to_s) if .vote_weight_totals[prev.to_s].zero?
end
.vote_weight_totals[weight.to_s] = Decidim::DecidimAwesome::VoteWeight.where(vote: proposal.votes, weight:).count
.vote_weight_totals.delete(weight.to_s) if .vote_weight_totals[weight.to_s].zero?
.weight_total = .vote_weight_totals.inject(0) { |sum, (weight, count)| sum + (weight.to_i * count) }
.save!
end
|