Sha256: 3df2f5466e074063b54148bafcdb5ac2db423a4bd7c711b5c656edce59172b1f

Contents?: true

Size: 1.27 KB

Versions: 39

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module Decidim
  module Consultations
    # A command with all the business logic when a user votes a multivote question.
    class MultipleVoteQuestion < Rectify::Command
      # Public: Initializes the command.
      #
      # form   - A Decidim::Consultations::MultiVoteForm object.
      # current_user - The current user.
      def initialize(form, current_user)
        @form = form
        @current_user = current_user
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid, together with the vote.
      # - :invalid if the form wasn't valid and we couldn't proceed.
      #
      # Returns nothing.
      def call
        return broadcast(:invalid, form, form.errors[:responses].first) if form.invalid?

        ActiveRecord::Base.transaction do
          form.vote_forms.each do |form|
            create_vote! form
          end
          broadcast(:ok, form)
        rescue StandardError => e
          broadcast(:invalid, form, e.message)
        end
      end

      private

      attr_reader :form

      def create_vote!(vote_form)
        @form.context.current_question.votes.create!(
          author: @current_user,
          response: vote_form.response
        )
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
decidim-consultations-0.26.10 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.26.9 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.26.8 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.26.7 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.26.5 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.26.4 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.26.3 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.26.2 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.26.1 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.26.0 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.26.0.rc2 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.26.0.rc1 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.25.2 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.25.1 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.25.0 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.25.0.rc4 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.25.0.rc3 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.25.0.rc2 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.25.0.rc1 app/commands/decidim/consultations/multiple_vote_question.rb
decidim-consultations-0.24.3 app/commands/decidim/consultations/multiple_vote_question.rb