Sha256: e936a89dfc793b3172c2afb6e9d3e916139a9ad7dae3e22f8d40b4747eca7445

Contents?: true

Size: 1009 Bytes

Versions: 38

Compression:

Stored size: 1009 Bytes

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
        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

38 entries across 38 versions & 2 rubygems

Version Path
decidim-proposals-0.6.8 app/commands/decidim/proposals/vote_proposal.rb
decidim-0.6.8 decidim-proposals/app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.6.7 app/commands/decidim/proposals/vote_proposal.rb
decidim-0.6.7 decidim-proposals/app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.6.6 app/commands/decidim/proposals/vote_proposal.rb
decidim-0.6.6 decidim-proposals/app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.6.5 app/commands/decidim/proposals/vote_proposal.rb
decidim-0.6.5 decidim-proposals/app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.6.4 app/commands/decidim/proposals/vote_proposal.rb
decidim-0.6.4 decidim-proposals/app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.6.3 app/commands/decidim/proposals/vote_proposal.rb
decidim-0.6.3 decidim-proposals/app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.6.2 app/commands/decidim/proposals/vote_proposal.rb
decidim-0.6.2 decidim-proposals/app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.6.1 app/commands/decidim/proposals/vote_proposal.rb
decidim-0.6.1 decidim-proposals/app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.6.0 app/commands/decidim/proposals/vote_proposal.rb
decidim-0.6.0 decidim-proposals/app/commands/decidim/proposals/vote_proposal.rb
decidim-proposals-0.5.1 app/commands/decidim/proposals/vote_proposal.rb
decidim-0.5.1 decidim-proposals/app/commands/decidim/proposals/vote_proposal.rb