Sha256: 61a62ec8775e9c6eb50a2aebf82f6f988848e1a2b672cced568672dd1dc7f46b

Contents?: true

Size: 943 Bytes

Versions: 63

Compression:

Stored size: 943 Bytes

Contents

# frozen_string_literal: true

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

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid, together with the question.
      # - :invalid if the form wasn't valid and we couldn't proceed.
      #
      # Returns nothing.
      def call
        destroy_vote
        broadcast(:ok, @question)
      end

      private

      def destroy_vote
        @question
          .votes
          .where(author: @current_user)
          .destroy_all
      end
    end
  end
end

Version data entries

63 entries across 63 versions & 1 rubygems

Version Path
decidim-consultations-0.12.0.pre app/commands/decidim/consultations/unvote_question.rb
decidim-consultations-0.11.1 app/commands/decidim/consultations/unvote_question.rb
decidim-consultations-0.11.0.pre1 app/commands/decidim/consultations/unvote_question.rb