Sha256: f471b005e77d0f38b14ca43437088acc520b992a1ae112c64ccd171b5088985b

Contents?: true

Size: 1.66 KB

Versions: 13

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    # A command with all the business logic when a user unvotes a proposal.
    class UnvoteProposal < Decidim::Command
      # Public: Initializes the command.
      #
      # proposal     - A Decidim::Proposals::Proposal object.
      # current_user - The current user.
      def initialize(proposal, current_user)
        @proposal = proposal
        @current_user = current_user
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid, together with the proposal.
      # - :invalid if the form was not valid and we could not proceed.
      #
      # Returns nothing.
      def call
        ActiveRecord::Base.transaction do
          ProposalVote.where(
            author: @current_user,
            proposal: @proposal
          ).destroy_all

          update_temporary_votes
        end

        Decidim::Gamification.decrement_score(@current_user, :proposal_votes)

        broadcast(:ok, @proposal)
      end

      private

      def component
        @component ||= @proposal.component
      end

      def minimum_votes_per_user
        component.settings.minimum_votes_per_user
      end

      def minimum_votes_per_user?
        minimum_votes_per_user.positive?
      end

      def update_temporary_votes
        return unless minimum_votes_per_user? && user_votes.count < minimum_votes_per_user

        user_votes.each { |vote| vote.update(temporary: true) }
      end

      def user_votes
        @user_votes ||= ProposalVote.where(
          author: @current_user,
          proposal: Proposal.where(component:)
        )
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

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