Sha256: ba778dae6f045e5583a6569a7555679ca0f7b91c065414562137c558d3974610

Contents?: true

Size: 1.66 KB

Versions: 10

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 < Rectify::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 wasn't valid and we couldn't 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: component)
        )
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
decidim-proposals-0.18.1 app/commands/decidim/proposals/unvote_proposal.rb
decidim-proposals-0.17.2 app/commands/decidim/proposals/unvote_proposal.rb
decidim-proposals-0.18.0 app/commands/decidim/proposals/unvote_proposal.rb
decidim-proposals-0.17.1 app/commands/decidim/proposals/unvote_proposal.rb
decidim-proposals-0.16.1 app/commands/decidim/proposals/unvote_proposal.rb
decidim-proposals-0.17.0 app/commands/decidim/proposals/unvote_proposal.rb
decidim-proposals-0.16.0 app/commands/decidim/proposals/unvote_proposal.rb
decidim-proposals-0.15.2 app/commands/decidim/proposals/unvote_proposal.rb
decidim-proposals-0.15.1 app/commands/decidim/proposals/unvote_proposal.rb
decidim-proposals-0.15.0 app/commands/decidim/proposals/unvote_proposal.rb