Class: Decidim::DecidimAwesome::Admin::RenameScopeLabel

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

Instance Method Summary collapse

Constructor Details

#initialize(params, organization) ⇒ RenameScopeLabel

Public: Initializes the command.

params - A constraint params



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

def initialize(params, organization)
  @text = params[:text]&.strip&.gsub(" ", "_")&.parameterize&.truncate(64)
  @scope = params[:scope]
  @key = params[:key]
  @attribute = params[:attribute]
  @organization = 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.



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

def call
  raise StandardError, "empty value" if @text.blank?
  raise StandardError, "key already exists" if config.value.keys.include? @text

  transaction do
    config.value[@text] = config.value.delete @key
    config.save!
    if config_scope
      config_scope.var = scope
      config_scope.save!
    end
  end

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