Sha256: 6819c0a6e2707527189f90d21ebfd58894689477745fdd2994d2a8ce996014f9

Contents?: true

Size: 1002 Bytes

Versions: 13

Compression:

Stored size: 1002 Bytes

Contents

# frozen_string_literal: true

module Decidim
  module Initiatives
    # A command with all the business logic when a user or organization unvotes an initiative.
    class UnvoteInitiative < Decidim::Command
      # Public: Initializes the command.
      #
      # initiative   - A Decidim::Initiative object.
      # current_user - The current user.
      def initialize(initiative, current_user)
        @initiative = initiative
        @current_user = current_user
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid, together with the initiative.
      # - :invalid if the form was not valid and we could not proceed.
      #
      # Returns nothing.
      def call
        destroy_initiative_vote
        broadcast(:ok, @initiative)
      end

      private

      def destroy_initiative_vote
        Initiative.transaction do
          @initiative.votes.where(author: @current_user).destroy_all
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-initiatives-0.29.1 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.28.4 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.29.0 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.28.3 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.29.0.rc4 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.29.0.rc3 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.29.0.rc2 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.29.0.rc1 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.28.2 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.28.1 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.28.0 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.28.0.rc5 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.28.0.rc4 app/commands/decidim/initiatives/unvote_initiative.rb