Sha256: 1b4dc96443de6a7282dae51e0715fd8de65ab2463a1eaca5a4170cab25f7dc2f

Contents?: true

Size: 1.51 KB

Versions: 19

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Decidim
  module Templates
    # A command with all the business logic when duplicating a questionnaire template
    module Admin
      class CopyQuestionnaireTemplate < Rectify::Command
        include Decidim::Templates::Admin::QuestionnaireCopier

        # Public: Initializes the command.
        #
        # template - A template we want to duplicate
        def initialize(template)
          @template = template
        end

        # Executes the command. Broadcasts these events:
        #
        # - :ok when everything is valid.
        # - :invalid if the form wasn't valid and we couldn't proceed.
        #
        # Returns nothing.
        def call
          return broadcast(:invalid) unless @template.valid?

          Template.transaction do
            copy_template
            copy_questionnaire_questions(@template.templatable, @copied_template.templatable)
          end

          broadcast(:ok, @copied_template)
        end

        private

        attr_reader :form

        def copy_template
          @copied_template = Template.create!(
            organization: @template.organization,
            name: @template.name,
            description: @template.description
          )
          @resource = Decidim::Forms::Questionnaire.create!(
            @template.templatable.dup.attributes.merge(
              questionnaire_for: @copied_template
            )
          )

          @copied_template.update!(templatable: @resource)
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
decidim-templates-0.26.10 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.26.9 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.26.8 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.26.7 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.26.5 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.26.4 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.26.3 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.26.2 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.26.1 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.26.0 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.26.0.rc2 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.26.0.rc1 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.25.2 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.25.1 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.25.0 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.25.0.rc4 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.25.0.rc3 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.25.0.rc2 app/commands/decidim/templates/admin/copy_questionnaire_template.rb
decidim-templates-0.25.0.rc1 app/commands/decidim/templates/admin/copy_questionnaire_template.rb