Class: Decidim::DecidimAwesome::Admin::DestroyProposalCustomField

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

Instance Method Summary collapse

Constructor Details

#initialize(key, organization, config_var = :proposal_custom_fields) ⇒ DestroyProposalCustomField

Public: Initializes the command.

key - the key to destroy inise proposal_custom_fields organization



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

def initialize(key, organization, config_var = :proposal_custom_fields)
  @key = key
  @organization = organization
  @config_var = config_var
end

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid.

  • :invalid if we couldn’t proceed.

Returns nothing.



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

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

  fields.value.except!(@key)
  fields.save!

  # remove constrains associated (a new config var is generated automatically, by removing it, it will trigger destroy on dependents)
  constraint = @config_var == :proposal_custom_fields ? :proposal_custom_field : :proposal_private_custom_field
  constraint = AwesomeConfig.find_by(var: "#{constraint}_#{@key}", organization: @organization)
  constraint.destroy! if constraint.present?

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