Sha256: d68375db888f04aa2833a5395371c08bc9fa93d21e84baa7fec1070d8b49b277

Contents?: true

Size: 1.99 KB

Versions: 4

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module Decidim
  module Plans
    # A command with all the business logic when a user creates a new plan.
    class PublishPlan < Rectify::Command
      # Public: Initializes the command.
      #
      # plan         - The plan to publish.
      # current_user - The current user.
      def initialize(plan, current_user)
        @plan = plan
        @current_user = current_user
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid and the plan is published.
      # - :invalid if the plan's author is not the current user.
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) unless @plan.authored_by?(@current_user)

        transaction do
          publish_plan
          send_notification
          send_notification_to_participatory_space
        end

        broadcast(:ok, @plan)
      end

      private

      def publish_plan
        Decidim.traceability.perform_action!(
          "publish",
          @plan,
          @current_user,
          visibility: "public-only"
        ) do
          @plan.update published_at: Time.current
        end
      end

      def send_notification
        return if @plan.coauthorships.empty?

        Decidim::EventsManager.publish(
          event: "decidim.events.plans.plan_published",
          event_class: Decidim::Plans::PublishPlanEvent,
          resource: @plan,
          followers: coauthors_followers
        )
      end

      def send_notification_to_participatory_space
        Decidim::EventsManager.publish(
          event: "decidim.events.plans.plan_published",
          event_class: Decidim::Plans::PublishPlanEvent,
          resource: @plan,
          followers: @plan.participatory_space.followers - coauthors_followers,
          extra: {
            participatory_space: true
          }
        )
      end

      def coauthors_followers
        @coauthors_followers ||= @plan.authors.flat_map(&:followers)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-plans-0.16.3 app/commands/decidim/plans/publish_plan.rb
decidim-plans-0.16.2 app/commands/decidim/plans/publish_plan.rb
decidim-plans-0.16.1 app/commands/decidim/plans/publish_plan.rb
decidim-plans-0.16.0 app/commands/decidim/plans/publish_plan.rb