Sha256: fbcefe2c0994cc7b1951ef7e8de3ba6e4f4cf8acf5b5efa240f9d9b63e041db2

Contents?: true

Size: 1.8 KB

Versions: 12

Compression:

Stored size: 1.8 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 = user_groups.find(@current_group_id) if @current_group_id.present?
        endorsement
      end

      def user_groups
        Decidim::UserGroups::ManageableUserGroups.for(@current_user).verified
      end

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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-proposals-0.21.0 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.20.1 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.20.0 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.19.1 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.18.1 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.19.0 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.17.2 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.18.0 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.17.1 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.16.1 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.17.0 app/commands/decidim/proposals/endorse_proposal.rb
decidim-proposals-0.16.0 app/commands/decidim/proposals/endorse_proposal.rb