Sha256: db43f8e6d9381e128ed7cf25bd083c1b05a4a13fb3859a6df7c47121b2dcc83c

Contents?: true

Size: 1.55 KB

Versions: 51

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    # A proposal can include a vote per user.
    class ProposalVote < ApplicationRecord
      belongs_to :proposal, foreign_key: "decidim_proposal_id", class_name: "Decidim::Proposals::Proposal"
      belongs_to :author, foreign_key: "decidim_author_id", class_name: "Decidim::User"

      validates :proposal, uniqueness: { scope: :author }
      validate :author_and_proposal_same_organization
      validate :proposal_not_rejected

      after_save :update_proposal_votes_count
      after_destroy :update_proposal_votes_count

      # Temporary votes are used when a minimum amount of votes is configured in
      # a component. They aren't taken into account unless the amount of votes
      # exceeds a threshold - meanwhile, they're marked as temporary.
      def self.temporary
        where(temporary: true)
      end

      # Final votes are votes that will be taken into account, that is, they're
      # not temporary.
      def self.final
        where(temporary: false)
      end

      private

      def update_proposal_votes_count
        proposal.update_votes_count
      end

      # Private: check if the proposal and the author have the same organization
      def author_and_proposal_same_organization
        return if !proposal || !author

        errors.add(:proposal, :invalid) unless author.organization == proposal.organization
      end

      def proposal_not_rejected
        return unless proposal

        errors.add(:proposal, :invalid) if proposal.rejected?
      end
    end
  end
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
decidim-proposals-0.27.9 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.27.8 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.27.7 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.27.6 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.26.10 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.26.9 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.27.5 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.26.8 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.27.4 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.27.3 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.26.7 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.26.5 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.27.2 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.27.1 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.26.4 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.27.0 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.26.3 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.27.0.rc2 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.27.0.rc1 app/models/decidim/proposals/proposal_vote.rb
decidim-proposals-0.26.2 app/models/decidim/proposals/proposal_vote.rb