Sha256: b2a19ebb3654a1e28daba1f7089e38b7ce291cdfdbc0dadd453e9e7fba39617a

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module Decidim
  module DecidimAwesome
    module HasProposalExtraFields
      extend ActiveSupport::Concern

      included do
        has_one :extra_fields, foreign_key: "decidim_proposal_id", class_name: "Decidim::DecidimAwesome::ProposalExtraField", dependent: :destroy

        def weight_count(weight)
          (extra_fields && extra_fields.vote_weight_totals[weight.to_s]) || 0
        end

        def vote_weights
          @vote_weights ||= all_vote_weights.to_h { |weight| [manifest&.label_for(weight) || weight.to_s, weight_count(weight)] }
        end

        def manifest
          @manifest ||= DecidimAwesome.voting_registry.find(component.settings.awesome_voting_manifest)
        end

        def all_vote_weights
          @all_vote_weights ||= self.class.all_vote_weights_for(component)
        end

        def update_vote_weights!
          extra_fields ||= Decidim::DecidimAwesome::ProposalExtraField.find_or_initialize_by(proposal: self)
          extra_fields.vote_weight_totals = {}
          votes.each do |vote|
            extra_fields.vote_weight_totals[vote.weight] ||= 0
            extra_fields.vote_weight_totals[vote.weight] += 1
          end
          extra_fields.save!
          self.extra_fields = extra_fields
          @vote_weights = nil
          @all_vote_weights = nil
        end

        # collects all different weights stored along the different proposals in a different component
        def self.all_vote_weights_for(component)
          Decidim::DecidimAwesome::VoteWeight.where(
            proposal_vote_id: Decidim::Proposals::ProposalVote.where(
              proposal: Decidim::Proposals::Proposal.where(component: component)
            )
          ).pluck(:weight)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-decidim_awesome-0.10.2 app/models/concerns/decidim/decidim_awesome/has_proposal_extra_fields.rb
decidim-decidim_awesome-0.10.1 app/models/concerns/decidim/decidim_awesome/has_proposal_extra_fields.rb
decidim-decidim_awesome-0.10.0 app/models/concerns/decidim/decidim_awesome/has_proposal_extra_fields.rb