Class: Decidim::DecidimAwesome::Admin::CreateCookieItemPreset

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

Instance Method Summary collapse

Constructor Details

#initialize(forms, category_slug) ⇒ CreateCookieItemPreset

Public: Initializes the command.

forms - An array of CookieItemForm objects to create. category_slug - The slug of the category where the items will be created.



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

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

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when all items were created (skipping duplicates).

  • :invalid if any item fails validation or an unexpected error occurs.

Returns nothing.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/commands/decidim/decidim_awesome/admin/create_cookie_item_preset.rb', line 23

def call
  errors = []
  forms.each do |form|
    form_with_context = form.with_context(
      current_organization: form.current_organization,
      current_user: form.current_user,
      category:
    )

    UpdateCookieItem.call(form_with_context, category_slug) do
      on(:ok) do
        existing_items.merge!(form.name => form.to_params)
      end
      on(:invalid) do |error_message|
        errors << "#{form.name}: #{error_message.presence || form.errors.full_messages.join(", ")}"
      end
    end
  end

  return broadcast(:invalid, errors.join(" | ")) if errors.any?

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