Sha256: 2bcdb52dc15dbde5d02233e13a6def3a0ed6ce6499290dc8fbf39577b675db6d

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module Decidim
  module Templates
    module Admin
      class UpdateProposalAnswerTemplate < Decidim::Command
        # Initializes the command.
        #
        # template - The Template to update.
        # form        - The form object containing the data to update.
        # user        - The user that updates the template.
        def initialize(template, form, user)
          @template = template
          @form = form
          @user = user
        end

        def call
          return broadcast(:invalid) unless @form.valid?
          return broadcast(:invalid) unless @user.organization == @template.organization

          @template = Decidim.traceability.update!(
            @template,
            @user,
            name: @form.name,
            description: @form.description,
            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/update_proposal_answer_template.rb
decidim-reporting_proposals-0.5.1 app/commands/decidim/templates/admin/update_proposal_answer_template.rb
decidim-reporting_proposals-0.5.0 app/commands/decidim/templates/admin/update_proposal_answer_template.rb