Sha256: e905544718d00d7863be066ddf50432ad22daeb3e28cef4668ede4ea82df0fc9

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 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
          )

          resource = identify_templateable_resource
          @template.update!(templatable: resource)

          broadcast(:ok, @template)
        end

        private

        def identify_templateable_resource
          resource = @form.scope_for_availability.split("-")
          case resource.first
          when "organizations"
            @form.current_organization
          when "components"
            component = Decidim::Component.find_by(id: resource.last)
            component&.participatory_space&.decidim_organization_id == @form.current_organization.id ? component : nil
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-reporting_proposals-0.5.2 app/commands/decidim/templates/admin/create_proposal_answer_template.rb
decidim-reporting_proposals-0.5.1 app/commands/decidim/templates/admin/create_proposal_answer_template.rb
decidim-reporting_proposals-0.5.0 app/commands/decidim/templates/admin/create_proposal_answer_template.rb