Sha256: b160a0725b97df4ded166010ff34fdba8f8e81e05e1628d8ea7d77a4cb52c69c

Contents?: true

Size: 1.37 KB

Versions: 14

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Decidim
  module Plans
    # A command with all the business logic when a user requests
    # access to edit a plan.
    class RequestAccessToPlan < Rectify::Command
      # Public: Initializes the command.
      #
      # form         - A form object with the params.
      # plan     - A Decidim::Plans::Plan object.
      # current_user - The current user and requester user
      def initialize(form, current_user)
        @form = form
        @plan = form.plan
        @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?

        @plan.collaborator_requests.create!(user: @current_user)
        notify_plan_authors
        broadcast(:ok, @plan)
      end

      private

      def notify_plan_authors
        Decidim::EventsManager.publish(
          event: "decidim.events.plans.plan_access_requested",
          event_class: Decidim::Plans::PlanAccessRequestedEvent,
          resource: @plan,
          followers: @plan.authors,
          extra: {
            requester_id: @current_user.id
          }
        )
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
decidim-plans-0.18.2 app/commands/decidim/plans/request_access_to_plan.rb
decidim-plans-0.18.1 app/commands/decidim/plans/request_access_to_plan.rb
decidim-plans-0.18.0 app/commands/decidim/plans/request_access_to_plan.rb
decidim-plans-0.17.0 app/commands/decidim/plans/request_access_to_plan.rb
decidim-plans-0.16.9 app/commands/decidim/plans/request_access_to_plan.rb
decidim-plans-0.16.8 app/commands/decidim/plans/request_access_to_plan.rb
decidim-plans-0.16.7 app/commands/decidim/plans/request_access_to_plan.rb
decidim-plans-0.16.6 app/commands/decidim/plans/request_access_to_plan.rb
decidim-plans-0.16.5 app/commands/decidim/plans/request_access_to_plan.rb
decidim-plans-0.16.4 app/commands/decidim/plans/request_access_to_plan.rb
decidim-plans-0.16.3 app/commands/decidim/plans/request_access_to_plan.rb
decidim-plans-0.16.2 app/commands/decidim/plans/request_access_to_plan.rb
decidim-plans-0.16.1 app/commands/decidim/plans/request_access_to_plan.rb
decidim-plans-0.16.0 app/commands/decidim/plans/request_access_to_plan.rb