Class: Decidim::DecidimAwesome::Admin::UpdateMenuHack

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

Instance Method Summary collapse

Constructor Details

#initialize(form, menu_name) ⇒ UpdateMenuHack

Public: Initializes the command.



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

def initialize(form, menu_name)
  @form = form
  @config_var = menu_name
  @organization = form.current_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
37
38
39
40
41
# File 'app/commands/decidim/decidim_awesome/admin/update_menu_hack.rb', line 22

def call
  return broadcast(:invalid) if form.invalid?

  find_var.value = [] unless find_var.value.is_a? Array
  find_var.value = find_var.value.filter { |i| i.is_a? Hash }
  found = false
  find_var.value.map! do |item|
    if item["url"] == form.url
      found = true
      form.to_params
    else
      item
    end
  end
  find_var.value << form.to_params unless found
  find_var.save!
  broadcast(:ok, find_var)
rescue StandardError => e
  broadcast(:invalid, e.message)
end