Class: Decidim::Admin::DeliverNewsletter

Inherits:
Command
  • Object
show all
Defined in:
app/commands/decidim/admin/deliver_newsletter.rb

Overview

Delivers the newsletter to its recipients.

Instance Method Summary collapse

Constructor Details

#initialize(newsletter, form, user) ⇒ DeliverNewsletter

Initializes the command.

newsletter - The newsletter to deliver. form - A form object with the params. user - the Decidim::User that delivers the newsletter



12
13
14
15
16
# File 'app/commands/decidim/admin/deliver_newsletter.rb', line 12

def initialize(newsletter, form, user)
  @newsletter = newsletter
  @form = form
  @user = user
end

Instance Method Details

#callObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/commands/decidim/admin/deliver_newsletter.rb', line 18

def call
  return broadcast(:invalid) if @form.send_to_all_users && !@user.admin?
  return broadcast(:invalid) unless @form.valid?
  return broadcast(:invalid) if @newsletter.sent?
  return broadcast(:no_recipients) if recipients.blank?

  @newsletter.with_lock do
    send_newsletter!
  end

  broadcast(:ok, @newsletter)
end