Class: Decidim::DecidimAwesome::Admin::DestroyScopedAdmin

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

Instance Method Summary collapse

Constructor Details

#initialize(key, organization) ⇒ DestroyScopedAdmin

Public: Initializes the command.

key - the key to destroy inside scoped_admins organization



11
12
13
14
# File 'app/commands/decidim/decidim_awesome/admin/destroy_scoped_admin.rb', line 11

def initialize(key, organization)
  @key = key
  @organization = organization
end

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid.

  • :invalid if we couldn’t proceed.

Returns nothing.



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

def call
  admins = AwesomeConfig.find_by(var: :scoped_admins, organization: @organization)
  return broadcast(:invalid, "Not a hash") unless admins&.value.is_a? Hash
  return broadcast(:invalid, "#{key} key invalid") unless admins.value.has_key?(@key)

  admins.value.except!(@key)
  admins.save!
  # remove constrains associated (a new config var is generated automatically, by removing it, it will trigger destroy on dependents)
  constraint = AwesomeConfig.find_by(var: "scoped_admin_#{@key}", organization: @organization)
  constraint.destroy! if constraint.present?

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