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) ⇒ DeliverNewsletter

Initializes the command.

newsletter - The newsletter to deliver. form - A form object with the params.



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

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

Instance Method Details

#callObject



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

def call
  return broadcast(:invalid) if @form.send_to_all_users && !current_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