Sha256: 651505a17f6547bd60b52177832156e834287bd4eb604dd6866238701f40b1e5

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    # A command with all the business logic when a user votes a proposal.
    class VoteProposal < Rectify::Command
      # Public: Initializes the command.
      #
      # proposal     - A Decidim::Proposals::Proposal object.
      # current_user - The current user.
      def initialize(proposal, current_user)
        @proposal = proposal
        @current_user = current_user
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid, together with the proposal vote.
      # - :invalid if the form wasn't valid and we couldn't proceed.
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) if @proposal.maximum_votes_reached? && !@proposal.can_accumulate_supports_beyond_threshold

        build_proposal_vote
        return broadcast(:invalid) unless vote.valid?

        vote.save!

        Decidim::Gamification.increment_score(@current_user, :proposal_votes)

        broadcast(:ok, vote)
      end

      attr_reader :vote

      private

      def build_proposal_vote
        @vote = @proposal.votes.build(author: @current_user)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-proposals-0.14.4 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.14.3 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.14.2 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.14.1 app/commands/decidim/proposals/vote_proposal.rb