Sha256: 798e9c87da788004ae3e05cf57170e8fa734b8acbcdbec8621f2af77b2166c07

Contents?: true

Size: 1.06 KB

Versions: 14

Compression:

Stored size: 1.06 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?

        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

14 entries across 14 versions & 1 rubygems

Version Path
decidim-proposals-0.9.3 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.9.2 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.9.1 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.9.0 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.8.4 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.8.3 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.8.2 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.8.1 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.8.0 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.7.4 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.7.3 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.7.2 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.7.1 app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.7.0 app/commands/decidim/proposals/vote_proposal.rb