Sha256: ae29ed57f5d79a17fb92e2c3e6401373f19971d37c109ea950a31b94f3d9a612

Contents?: true

Size: 1.14 KB

Versions: 31

Compression:

Stored size: 1.14 KB

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 < Rectify::Command
      # Public: Initializes the command.
      #
      # initiative   - A Decidim::Initiative object.
      # current_user - The current user.
      # group_id     - Decidim user group id
      def initialize(initiative, current_user, group_id)
        @initiative = initiative
        @current_user = current_user
        @decidim_user_group_id = group_id
      end

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

      private

      def destroy_initiative_vote
        @initiative
          .votes
          .where(
            author: @current_user,
            decidim_user_group_id: @decidim_user_group_id
          )
          .destroy_all
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
decidim-initiatives-0.22.0 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.21.0 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.20.1 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.20.0 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.19.1 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.18.1 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.19.0 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.17.2 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.18.0 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.17.1 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.16.1 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.17.0 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.16.0 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.15.2 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.15.1 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.15.0 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.14.4 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.14.3 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.14.2 app/commands/decidim/initiatives/unvote_initiative.rb
decidim-initiatives-0.14.1 app/commands/decidim/initiatives/unvote_initiative.rb