Sha256: 9d45e81366e88662e6e0f0cb2393b2b0df4e9ff0855f3773053213dea88131af

Contents?: true

Size: 1.77 KB

Versions: 16

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    # A command with all the business logic when a user endorses a proposal.
    class EndorseProposal < Rectify::Command
      # Public: Initializes the command.
      #
      # proposal     - A Decidim::Proposals::Proposal object.
      # current_user - The current user.
      # current_group_id- (optional) The current_grup that is endorsing the Proposal.
      def initialize(proposal, current_user, current_group_id = nil)
        @proposal = proposal
        @current_user = current_user
        @current_group_id = current_group_id
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid, together with the proposal vote.
      # - :invalid if the form wasn't valid and we couldn't proceed.
      #
      # Returns nothing.
      def call
        endorsement = build_proposal_endorsement
        if endorsement.save
          notify_endorser_followers
          broadcast(:ok, endorsement)
        else
          broadcast(:invalid)
        end
      end

      private

      def build_proposal_endorsement
        endorsement = @proposal.endorsements.build(author: @current_user)
        endorsement.user_group = @current_user.user_groups.verified.find(@current_group_id) if @current_group_id.present?
        endorsement
      end

      def notify_endorser_followers
        recipient_ids = @current_user.followers.pluck(:id)
        Decidim::EventsManager.publish(
          event: "decidim.events.proposals.proposal_endorsed",
          event_class: Decidim::Proposals::ProposalEndorsedEvent,
          resource: @proposal,
          recipient_ids: recipient_ids.uniq,
          extra: {
            endorser_id: @current_user.id
          }
        )
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
decidim-proposals-0.14.4 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.14.3 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.14.2 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.14.1 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.13.1 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.12.2 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.13.0 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.12.1 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.13.0.pre1 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.12.0 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.11.2 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.12.0.pre app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.11.1 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.11.0.pre1 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.10.1 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.10.0 app/commands/decidim/proposals/endorse_proposal.rb