Class: Decidim::DecidimAwesome::Admin::CreateAwesomeAuthorizationMembers

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

Instance Method Summary collapse

Constructor Details

#initialize(form) ⇒ CreateAwesomeAuthorizationMembers

Public: Initializes the command.

form - The form object containing the authorization group and emails.



10
11
12
# File 'app/commands/decidim/decidim_awesome/admin/create_awesome_authorization_members.rb', line 10

def initialize(form)
  @form = form
end

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when all members were added or already existed.
  • :invalid if some members could not be created.

Returns nothing.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/commands/decidim/decidim_awesome/admin/create_awesome_authorization_members.rb', line 20

def call
  authorization_group.members.destroy_all if form.remove_previous_members

  previous_members_count = authorization_group.members.count
  authorization_group.members.insert_all( # rubocop:disable Rails/SkipsModelValidations
    form.data.map { |email| { email: email.strip.downcase } },
    unique_by: :index_auth_members_group_email
  )
  created_count = authorization_group.members.count - previous_members_count

  if created_count.zero? && form.remove_previous_members.blank?
    return broadcast(:invalid,
                     I18n.t("decidim.decidim_awesome.admin.awesome_authorization_users.update.empty"))
  end

  broadcast(:ok, created_count)
rescue StandardError => e
  broadcast(:invalid, e.message)
end