Sha256: 41c19764838611e05e6ac8eb1f0f2288d4b0c3e38a1c374680823d1d8b44b59b

Contents?: true

Size: 1.94 KB

Versions: 12

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    module Admin
      # A command with all the business logic when an admin splits proposals from
      # one component to another.
      class SplitProposals < Decidim::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, split_proposals)
        end

        private

        attr_reader :form

        def split_proposals
          transaction do
            form.proposals.flat_map do |original_proposal|
              # If copying to the same component we only need one copy
              # but linking to the original proposal links, not the
              # original proposal.
              create_proposal(original_proposal)
              create_proposal(original_proposal) unless form.same_component?
            end
          end
        end

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

          proposals_to_link = links_for(original_proposal)
          split_proposal.link_resources(proposals_to_link, "copied_from_component")
        end

        def links_for(proposal)
          return proposal unless form.same_component?

          proposal.linked_resources(:proposals, "copied_from_component")
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-proposals-0.27.9 app/commands/decidim/proposals/admin/split_proposals.rb
decidim-proposals-0.27.8 app/commands/decidim/proposals/admin/split_proposals.rb
decidim-proposals-0.27.7 app/commands/decidim/proposals/admin/split_proposals.rb
decidim-proposals-0.27.6 app/commands/decidim/proposals/admin/split_proposals.rb
decidim-proposals-0.27.5 app/commands/decidim/proposals/admin/split_proposals.rb
decidim-proposals-0.27.4 app/commands/decidim/proposals/admin/split_proposals.rb
decidim-proposals-0.27.3 app/commands/decidim/proposals/admin/split_proposals.rb
decidim-proposals-0.27.2 app/commands/decidim/proposals/admin/split_proposals.rb
decidim-proposals-0.27.1 app/commands/decidim/proposals/admin/split_proposals.rb
decidim-proposals-0.27.0 app/commands/decidim/proposals/admin/split_proposals.rb
decidim-proposals-0.27.0.rc2 app/commands/decidim/proposals/admin/split_proposals.rb
decidim-proposals-0.27.0.rc1 app/commands/decidim/proposals/admin/split_proposals.rb