Class: Decidim::DecidimAwesome::Admin::CreateProposalCustomField

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

Instance Method Summary collapse

Constructor Details

#initialize(organization, config_var = :proposal_custom_fields) ⇒ CreateProposalCustomField

Public: Initializes the command.



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

def initialize(organization, config_var = :proposal_custom_fields)
  @organization = organization
  @ident = rand(36**8).to_s(36)
  @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
# File 'app/commands/decidim/decidim_awesome/admin/create_proposal_custom_field.rb', line 23

def call
  fields = AwesomeConfig.find_or_initialize_by(var: @config_var, organization: @organization)
  fields.value = {} unless fields.value.is_a? Hash
  # TODO: prevent (unlikely) colisions with exisiting values
  fields.value[@ident] = default_definition
  fields.save!

  create_constraint_never(@config_var == :proposal_custom_fields ? :proposal_custom_field : :proposal_private_custom_field)

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