Sha256: 5df37f47fcb61af05caf7fd02de4850f977c3afda8f9d238c309862f8b66a2c2
Contents?: true
Size: 1.74 KB
Versions: 14
Compression:
Stored size: 1.74 KB
Contents
# frozen_string_literal: true module Decidim module Debates # This command is executed when the user creates a Debate from the public # views. 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_notifications end broadcast(:ok, debate) end private attr_reader :debate, :form def organization @organization = form.current_component.organization end def i18n_field(field) organization.available_locales.inject({}) do |i18n, locale| i18n.update(locale => field) end end def create_debate @debate = Debate.create!( author: form.current_user, decidim_user_group_id: form.user_group_id, category: form.category, title: i18n_field(form.title), description: i18n_field(form.description), component: form.current_component ) end def send_notifications send_notification(debate.author.followers.pluck(:id), :user) send_notification(debate.participatory_space.followers.pluck(:id), :participatory_space) end def send_notification(recipient_ids, type) Decidim::EventsManager.publish( event: "decidim.events.debates.debate_created", event_class: Decidim::Debates::CreateDebateEvent, resource: debate, recipient_ids: recipient_ids, extra: { type: type.to_s } ) end end end end
Version data entries
14 entries across 14 versions & 1 rubygems