Sha256: 4415a19a612051877084c85576977f78d7a2389902194810b540ed3399cf991c

Contents?: true

Size: 1.38 KB

Versions: 8

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module Decidim
  module Templates
    module Admin
      class CreateProposalAnswerTemplate < Decidim::Command
        # Initializes the command.
        #
        # form - The source for this ProposalAnswerTemplate.
        def initialize(form)
          @form = form
        end

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

          @template = Decidim.traceability.create!(
            Template,
            @form.current_user,
            name: @form.name,
            description: @form.description,
            organization: @form.current_organization,
            field_values: { internal_state: @form.internal_state },
            target: :proposal_answer
          )

          @template.update!(templatable: identify_templateable_resource)

          broadcast(:ok, @template)
        end

        private

        def identify_templateable_resource
          resource = @form.current_organization
          if @form.component_constraint.present?
            found_component = Decidim::Component.find_by(id: @form.component_constraint, manifest_name: "proposals")
            if found_component.present?
              resource = found_component&.participatory_space&.decidim_organization_id == @form.current_organization.id ? found_component : nil
            end
          end
          resource
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
decidim-templates-0.28.5 app/commands/decidim/templates/admin/create_proposal_answer_template.rb
decidim-templates-0.28.4 app/commands/decidim/templates/admin/create_proposal_answer_template.rb
decidim-templates-0.28.3 app/commands/decidim/templates/admin/create_proposal_answer_template.rb
decidim-templates-0.28.2 app/commands/decidim/templates/admin/create_proposal_answer_template.rb
decidim-templates-0.28.1 app/commands/decidim/templates/admin/create_proposal_answer_template.rb
decidim-templates-0.28.0 app/commands/decidim/templates/admin/create_proposal_answer_template.rb
decidim-templates-0.28.0.rc5 app/commands/decidim/templates/admin/create_proposal_answer_template.rb
decidim-templates-0.28.0.rc4 app/commands/decidim/templates/admin/create_proposal_answer_template.rb