Sha256: 26005a6e4239af823ab10a5a6ec65a764bc03216cf6aa01fc9d128c24318a709

Contents?: true

Size: 1.11 KB

Versions: 12

Compression:

Stored size: 1.11 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!
        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

12 entries across 12 versions & 1 rubygems

Version Path
decidim-proposals-0.13.1 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.12.2 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.13.0 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.12.1 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.13.0.pre1 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.12.0 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.11.2 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.12.0.pre app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.11.1 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.11.0.pre1 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.10.1 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.10.0 app/commands/decidim/proposals/vote_proposal.rb