Sha256: df4077c32ba345ef8f1faf0cbbdbf0ac82c46681bb603c5d8ebd15c751b56f76

Contents?: true

Size: 1.54 KB

Versions: 6

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module Decidim
  module Comments
    # A command with all the business logic to update an existing comment
    class UpdateComment < Decidim::Command
      delegate :current_user, to: :form
      # Public: Initializes the command.
      #
      # comment - Decidim::Comments::Comment
      # form - A form object with the params.
      def initialize(comment, form)
        @comment = comment
        @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) if form.invalid? || !comment.authored_by?(current_user)

        with_events do
          update_comment
        end

        broadcast(:ok)
      end

      private

      attr_reader :form, :comment

      def event_arguments
        {
          resource: comment,
          extra: {
            event_author: form.current_user,
            locale:
          }
        }
      end

      def update_comment
        parsed = Decidim::ContentProcessor.parse(form.body, current_organization: form.current_organization)

        params = {
          body: { I18n.locale => parsed.rewrite }
        }

        @comment = Decidim.traceability.update!(
          comment,
          current_user,
          params,
          visibility: "public-only",
          edit: true
        )

        CommentCreation.publish(@comment, parsed.metadata)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-comments-0.29.1 app/commands/decidim/comments/update_comment.rb
decidim-comments-0.29.0 app/commands/decidim/comments/update_comment.rb
decidim-comments-0.29.0.rc4 app/commands/decidim/comments/update_comment.rb
decidim-comments-0.29.0.rc3 app/commands/decidim/comments/update_comment.rb
decidim-comments-0.29.0.rc2 app/commands/decidim/comments/update_comment.rb
decidim-comments-0.29.0.rc1 app/commands/decidim/comments/update_comment.rb