Sha256: ee2573b5871acad5356fe21f4ac7e298a29fdac9c7f28221992df3cc61739559

Contents?: true

Size: 1.82 KB

Versions: 39

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    module Admin
      # A command with all the business logic when an admin merges proposals from
      # one component to another.
      class MergeProposals < Rectify::Command
        # Public: Initializes the command.
        #
        # form - A form object with the params.
        def initialize(form)
          @form = form
        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 form.valid?

          broadcast(:ok, merge_proposals)
        end

        private

        attr_reader :form

        def merge_proposals
          transaction do
            merged_proposal = create_new_proposal
            merged_proposal.link_resources(proposals_to_link, "copied_from_component")
            form.proposals.each(&:destroy!) if form.same_component?
            merged_proposal
          end
        end

        def proposals_to_link
          return previous_links if form.same_component?

          form.proposals
        end

        def previous_links
          @previous_links ||= form.proposals.flat_map do |proposal|
            proposal.linked_resources(:proposals, "copied_from_component")
          end
        end

        def create_new_proposal
          original_proposal = form.proposals.first

          Decidim::Proposals::ProposalBuilder.copy(
            original_proposal,
            author: form.current_organization,
            action_user: form.current_user,
            extra_attributes: {
              component: form.target_component
            },
            skip_link: true
          )
        end
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

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