Sha256: 33b39c7bfe5517846dff9e42dad83c67e1bf1450aeccb6f4e4b74842de8d1046

Contents?: true

Size: 1.59 KB

Versions: 46

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    # A command with all the business logic when a user requests access
    # to edit a collaborative draft.
    class RequestAccessToCollaborativeDraft < Rectify::Command
      # Public: Initializes the command.
      #
      # form         - A form object with the params.
      # collaborative_draft     - A Decidim::Proposals::CollaborativeDraft object.
      # current_user - The current user and requester user
      def initialize(form, current_user)
        @form = form
        @collaborative_draft = form.collaborative_draft
        @current_user = current_user
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid.
      # - :invalid if it wasn't valid and we couldn't proceed.
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) if @form.invalid?
        return broadcast(:invalid) if @current_user.nil?

        @collaborative_draft.collaborator_requests.create!(user: @current_user)
        notify_collaborative_draft_authors
        broadcast(:ok, @collaborative_draft)
      end

      private

      def notify_collaborative_draft_authors
        Decidim::EventsManager.publish(
          event: "decidim.events.proposals.collaborative_draft_access_requested",
          event_class: Decidim::Proposals::CollaborativeDraftAccessRequestedEvent,
          resource: @collaborative_draft,
          affected_users: @collaborative_draft.authors,
          extra: {
            requester_id: @current_user.id
          }
        )
      end
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
decidim-proposals-0.26.10 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.26.9 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.26.8 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.26.7 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.26.5 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.26.4 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.26.3 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.26.2 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.26.1 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.26.0 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.26.0.rc2 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.26.0.rc1 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.25.2 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.25.1 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.25.0 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.25.0.rc4 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.25.0.rc3 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.25.0.rc2 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.25.0.rc1 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb
decidim-proposals-0.24.3 app/commands/decidim/proposals/request_access_to_collaborative_draft.rb