Sha256: 40bbe9e1ce1fda472247a08d72c44bacf2a6180f06e567292137e6a415f7391d

Contents?: true

Size: 1.58 KB

Versions: 6

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module Decidim
  module EnhancedTextwork
    module Admin
      # A command with all the business logic to assign paragraphs to a given
      # valuator.
      class AssignParagraphsToValuator < 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_paragraphs
          broadcast(:ok)
        rescue ActiveRecord::RecordInvalid
          broadcast(:invalid)
        end

        private

        attr_reader :form

        def assign_paragraphs
          transaction do
            form.paragraphs.flat_map do |paragraph|
              find_assignment(paragraph) || assign_paragraph(paragraph)
            end
          end
        end

        def find_assignment(paragraph)
          Decidim::EnhancedTextwork::ValuationAssignment.find_by(
            paragraph: paragraph,
            valuator_role: form.valuator_role
          )
        end

        def assign_paragraph(paragraph)
          Decidim.traceability.create!(
            Decidim::EnhancedTextwork::ValuationAssignment,
            form.current_user,
            paragraph: paragraph,
            valuator_role: form.valuator_role
          )
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-enhanced_textwork-1.0.5 app/commands/decidim/enhanced_textwork/admin/assign_paragraphs_to_valuator.rb
decidim-enhanced_textwork-1.0.4 app/commands/decidim/enhanced_textwork/admin/assign_paragraphs_to_valuator.rb
decidim-enhanced_textwork-1.0.3 app/commands/decidim/enhanced_textwork/admin/assign_paragraphs_to_valuator.rb
decidim-enhanced_textwork-1.0.2 app/commands/decidim/enhanced_textwork/admin/assign_paragraphs_to_valuator.rb
decidim-enhanced_textwork-1.0.1 app/commands/decidim/enhanced_textwork/admin/assign_paragraphs_to_valuator.rb
decidim-enhanced_textwork-1.0.0 app/commands/decidim/enhanced_textwork/admin/assign_paragraphs_to_valuator.rb