Class: Decidim::DecidimAwesome::Admin::UpdateCookieItem

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form, category_slug) ⇒ UpdateCookieItem

Public: Initializes the command.

form - A form object with the params. category_slug - The slug of the category where the item belongs. config - The AwesomeConfig instance for cookie management.



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

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

Instance Attribute Details

#category_slugObject (readonly)

Returns the value of attribute category_slug.



18
19
20
# File 'app/commands/decidim/decidim_awesome/admin/update_cookie_item.rb', line 18

def category_slug
  @category_slug
end

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'app/commands/decidim/decidim_awesome/admin/update_cookie_item.rb', line 18

def config
  @config
end

#formObject (readonly)

Returns the value of attribute form.



18
19
20
# File 'app/commands/decidim/decidim_awesome/admin/update_cookie_item.rb', line 18

def form
  @form
end

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid.

  • :invalid if the form is invalid, category/item not found, or name already exists.

Returns nothing.



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

def call
  return broadcast(:invalid) if form.invalid?
  return broadcast(:invalid) unless config&.value&.[](category_slug)

  config.value[category_slug]["items"] ||= {}
  # Handle slug change by deleting old key
  config.value[category_slug]["items"][form.name] = config.value[category_slug]["items"].delete(form.id) if form.id && form.name != form.id
  config.value[category_slug]["items"][form.name] = form.to_params
  config.save!

  broadcast(:ok)
rescue ActiveRecord::RecordInvalid => e
  broadcast(:invalid, e.record.errors.full_messages.join(", "))
rescue StandardError => e
  broadcast(:invalid, e.message)
end