Sha256: 0fea86a30ef0a239f949c95e064ee9b252fe1e1dcb576bdb5ad9c05f108cf5d6

Contents?: true

Size: 1.66 KB

Versions: 6

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module Decidim
  module EnhancedTextwork
    module Admin
      # A command with all the business logic when an admin imports paragraphs from
      # a participatory text.
      class ImportParticipatoryText < 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?

          transaction do
            save_participatory_text_meta(@form)
            parse_participatory_text_doc(@form)
          end

          broadcast(:ok)
        rescue StandardError
          broadcast(:invalid_file)
        end

        private

        # Persist ParticipatoryText related meta information.
        def save_participatory_text_meta(form)
          document = ParticipatoryText.find_or_initialize_by(component: form.current_component)
          document.update!(title: form.title, description: form.description)
        end

        def parse_participatory_text_doc(form)
          return if form.document.blank?

          document = form.document_text
          document = Decidim::IoEncoder.to_standard_encoding(document)
          markdown = DocToMarkdown.new(document, form.document_type).to_md
          parser = MarkdownToParagraphs.new(form.current_component, form.current_user)
          parser.parse(markdown)
        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/import_participatory_text.rb
decidim-enhanced_textwork-1.0.4 app/commands/decidim/enhanced_textwork/admin/import_participatory_text.rb
decidim-enhanced_textwork-1.0.3 app/commands/decidim/enhanced_textwork/admin/import_participatory_text.rb
decidim-enhanced_textwork-1.0.2 app/commands/decidim/enhanced_textwork/admin/import_participatory_text.rb
decidim-enhanced_textwork-1.0.1 app/commands/decidim/enhanced_textwork/admin/import_participatory_text.rb
decidim-enhanced_textwork-1.0.0 app/commands/decidim/enhanced_textwork/admin/import_participatory_text.rb