Class: Decidim::DecidimAwesome::Admin::DestroyCookieItem

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(category_slug, item_name, organization) ⇒ DestroyCookieItem

Public: Initializes the command.

category_slug - The slug of the category where the item belongs. item_name - The name of the item to destroy. organization - The organization where the item belongs. config - The AwesomeConfig instance for cookie management.



13
14
15
16
17
18
# File 'app/commands/decidim/decidim_awesome/admin/destroy_cookie_item.rb', line 13

def initialize(category_slug, item_name, organization)
  @category_slug = category_slug.to_s
  @item_name = item_name.to_s
  @organization = organization
  @config = AwesomeConfig.find_by(organization: organization, var: :cookie_management)
end

Instance Attribute Details

#category_slugObject (readonly)

Returns the value of attribute category_slug.



20
21
22
# File 'app/commands/decidim/decidim_awesome/admin/destroy_cookie_item.rb', line 20

def category_slug
  @category_slug
end

#configObject (readonly)

Returns the value of attribute config.



20
21
22
# File 'app/commands/decidim/decidim_awesome/admin/destroy_cookie_item.rb', line 20

def config
  @config
end

#item_nameObject (readonly)

Returns the value of attribute item_name.



20
21
22
# File 'app/commands/decidim/decidim_awesome/admin/destroy_cookie_item.rb', line 20

def item_name
  @item_name
end

#organizationObject (readonly)

Returns the value of attribute organization.



20
21
22
# File 'app/commands/decidim/decidim_awesome/admin/destroy_cookie_item.rb', line 20

def organization
  @organization
end

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid.

  • :invalid if the category is not found or the item is not found.

Returns nothing.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/commands/decidim/decidim_awesome/admin/destroy_cookie_item.rb', line 28

def call
  return broadcast(:invalid) unless config&.value

  category = config.value[category_slug]
  return broadcast(:invalid) unless category
  return broadcast(:invalid) unless category["items"]&.has_key?(item_name)

  category["items"].delete(item_name)
  config.save!

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