Class: Decidim::Templates::Admin::CopyTemplate

Inherits:
Command
  • Object
show all
Defined in:
decidim-templates/app/commands/decidim/templates/admin/copy_template.rb

Direct Known Subclasses

CopyQuestionnaireTemplate

Instance Method Summary collapse

Methods inherited from Command

call, #evaluate, #method_missing, #respond_to_missing?, #transaction, #with_events

Constructor Details

#initialize(template, user) ⇒ CopyTemplate

Returns a new instance of CopyTemplate.



7
8
9
10
# File 'decidim-templates/app/commands/decidim/templates/admin/copy_template.rb', line 7

def initialize(template, user)
  @template = template
  @user = user
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Decidim::Command

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid.

  • :invalid if the form was not valid and we could not proceed.

Returns nothing.



18
19
20
21
22
23
24
25
26
27
28
# File 'decidim-templates/app/commands/decidim/templates/admin/copy_template.rb', line 18

def call
  return broadcast(:invalid) unless @template.valid?

  Decidim.traceability.perform_action!("duplicate", @template, @user) do
    Template.transaction do
      copy_template
    end
  end

  broadcast(:ok, @copied_template)
end

#copy_templateObject



30
31
32
33
34
35
36
37
38
39
# File 'decidim-templates/app/commands/decidim/templates/admin/copy_template.rb', line 30

def copy_template
  @copied_template = Template.create!(
    organization: @template.organization,
    name: @template.name,
    description: @template.description,
    target: @template.target,
    field_values: @template.field_values,
    templatable: @template.templatable
  )
end