Sha256: bac73bf6d88a04000a875d90082825177a4e0114e72528ca86c82510d1dd11c5

Contents?: true

Size: 1017 Bytes

Versions: 63

Compression:

Stored size: 1017 Bytes

Contents

# frozen_string_literal: true

module Decidim
  module Consultations
    # A command with all the business logic when a user votes a question.
    class VoteQuestion < Rectify::Command
      # Public: Initializes the command.
      #
      # form   - A Decidim::Consultations::VoteForm object.
      # current_user - The current user.
      def initialize(form)
        @form = form
      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
        vote = build_vote
        if vote.save
          broadcast(:ok, vote)
        else
          broadcast(:invalid, vote)
        end
      end

      private

      attr_reader :form

      def build_vote
        form.context.current_question.votes.build(
          author: form.context.current_user,
          response: form.response
        )
      end
    end
  end
end

Version data entries

63 entries across 63 versions & 1 rubygems

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