Sha256: 98152e6ed8efde374bf15bcdeb134a8053a285ad54ef61a6a8b75c7dbe138f35

Contents?: true

Size: 1.39 KB

Versions: 13

Compression:

Stored size: 1.39 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 ApplyQuestionnaireTemplate < Decidim::Command
        include Decidim::Templates::Admin::QuestionnaireCopier

        # Public: Initializes the command.
        #
        # template - The template we want to apply
        # questionnaire - The questionnaire we want to use the template
        def initialize(questionnaire, template)
          @questionnaire = questionnaire
          @template = template
        end

        # 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.
        def call
          return broadcast(:invalid) unless @template && @template.valid?

          Template.transaction do
            apply_template
            copy_questionnaire_questions(@template.templatable, @questionnaire)
          end

          broadcast(:ok, @questionnaire)
        end

        private

        attr_reader :form

        def apply_template
          @questionnaire.update!(
            title: @template.templatable.title,
            description: @template.templatable.description,
            tos: @template.templatable.tos
          )
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-templates-0.29.1 app/commands/decidim/templates/admin/apply_questionnaire_template.rb
decidim-templates-0.28.4 app/commands/decidim/templates/admin/apply_questionnaire_template.rb
decidim-templates-0.29.0 app/commands/decidim/templates/admin/apply_questionnaire_template.rb
decidim-templates-0.28.3 app/commands/decidim/templates/admin/apply_questionnaire_template.rb
decidim-templates-0.29.0.rc4 app/commands/decidim/templates/admin/apply_questionnaire_template.rb
decidim-templates-0.29.0.rc3 app/commands/decidim/templates/admin/apply_questionnaire_template.rb
decidim-templates-0.29.0.rc2 app/commands/decidim/templates/admin/apply_questionnaire_template.rb
decidim-templates-0.29.0.rc1 app/commands/decidim/templates/admin/apply_questionnaire_template.rb
decidim-templates-0.28.2 app/commands/decidim/templates/admin/apply_questionnaire_template.rb
decidim-templates-0.28.1 app/commands/decidim/templates/admin/apply_questionnaire_template.rb
decidim-templates-0.28.0 app/commands/decidim/templates/admin/apply_questionnaire_template.rb
decidim-templates-0.28.0.rc5 app/commands/decidim/templates/admin/apply_questionnaire_template.rb
decidim-templates-0.28.0.rc4 app/commands/decidim/templates/admin/apply_questionnaire_template.rb