Sha256: 0236c1be4492f458d3ff11bb1cc36b0d6d5a97c3d037233ae431563cf5fa0bef

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module Decidim
  module Debates
    module Admin
      # This command is executed when the user creates a Debate from the admin
      # panel.
      class CreateDebate < Rectify::Command
        def initialize(form)
          @form = form
        end

        # Creates the debate if valid.
        #
        # Broadcasts :ok if successful, :invalid otherwise.
        def call
          return broadcast(:invalid) if form.invalid?

          transaction do
            create_debate
            send_notification_to_space_followers
          end
          broadcast(:ok)
        end

        private

        attr_reader :debate, :form

        def create_debate
          @debate = Decidim.traceability.create!(
            Debate,
            form.current_user,
            category: form.category,
            title: form.title,
            description: form.description,
            information_updates: form.information_updates,
            instructions: form.instructions,
            end_time: form.end_time,
            start_time: form.start_time,
            feature: form.current_feature
          )
        end

        def send_notification_to_space_followers
          Decidim::EventsManager.publish(
            event: "decidim.events.debates.debate_created",
            event_class: Decidim::Debates::CreateDebateEvent,
            resource: debate,
            recipient_ids: form.current_feature.participatory_space.followers.pluck(:id),
            extra: {
              type: "participatory_space"
            }
          )
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
decidim-debates-0.10.1 app/commands/decidim/debates/admin/create_debate.rb
decidim-debates-0.10.0 app/commands/decidim/debates/admin/create_debate.rb