Sha256: 1696964bc98ddf8a6b7b2359e34464ae8796fb0139fb75ac4856cc126303532a

Contents?: true

Size: 1.65 KB

Versions: 13

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    module Admin
      # A command with all the business logic when an admin imports proposals from
      # a participatory text.
      class ImportParticipatoryText < 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 was not valid and we could not 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 = MarkdownToProposals.new(form.current_component, form.current_user)
          parser.parse(markdown)
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-proposals-0.29.1 app/commands/decidim/proposals/admin/import_participatory_text.rb
decidim-proposals-0.28.4 app/commands/decidim/proposals/admin/import_participatory_text.rb
decidim-proposals-0.29.0 app/commands/decidim/proposals/admin/import_participatory_text.rb
decidim-proposals-0.28.3 app/commands/decidim/proposals/admin/import_participatory_text.rb
decidim-proposals-0.29.0.rc4 app/commands/decidim/proposals/admin/import_participatory_text.rb
decidim-proposals-0.29.0.rc3 app/commands/decidim/proposals/admin/import_participatory_text.rb
decidim-proposals-0.29.0.rc2 app/commands/decidim/proposals/admin/import_participatory_text.rb
decidim-proposals-0.29.0.rc1 app/commands/decidim/proposals/admin/import_participatory_text.rb
decidim-proposals-0.28.2 app/commands/decidim/proposals/admin/import_participatory_text.rb
decidim-proposals-0.28.1 app/commands/decidim/proposals/admin/import_participatory_text.rb
decidim-proposals-0.28.0 app/commands/decidim/proposals/admin/import_participatory_text.rb
decidim-proposals-0.28.0.rc5 app/commands/decidim/proposals/admin/import_participatory_text.rb
decidim-proposals-0.28.0.rc4 app/commands/decidim/proposals/admin/import_participatory_text.rb