Sha256: 835500026171f90dcb0063fe3e800fbf81a75d4218ce72fd5585ca9f064432e8

Contents?: true

Size: 1.54 KB

Versions: 35

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    module Admin
      # A command with all the business logic to assign proposals to a given
      # valuator.
      class AssignProposalsToValuator < 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?

          assign_proposals
          broadcast(:ok)
        rescue ActiveRecord::RecordInvalid
          broadcast(:invalid)
        end

        private

        attr_reader :form

        def assign_proposals
          transaction do
            form.proposals.flat_map do |proposal|
              find_assignment(proposal) || assign_proposal(proposal)
            end
          end
        end

        def find_assignment(proposal)
          Decidim::Proposals::ValuationAssignment.find_by(
            proposal: proposal,
            valuator_role: form.valuator_role
          )
        end

        def assign_proposal(proposal)
          Decidim.traceability.create!(
            Decidim::Proposals::ValuationAssignment,
            form.current_user,
            proposal: proposal,
            valuator_role: form.valuator_role
          )
        end
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

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