Sha256: 9028a6bb245e3d62e0c6798f8e7bd60656074cf9edfcfae49f8e8e0e52a937e7

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true
module Decidim
  module Comments
    # A command with all the business logic to create a new comment
    class CreateComment < Rectify::Command
      # Public: Initializes the command.
      #
      # form - A form object with the params.
      def initialize(form, author, commentable)
        @form = form
        @author = author
        @commentable = commentable
      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) if form.invalid?

        create_comment
        broadcast(:ok, @comment)
      end

      private

      attr_reader :form

      def create_comment
        @comment = Comment.create!(author: @author,
                                   commentable: @commentable,
                                   body: form.body,
                                   alignment: form.alignment)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
decidim-comments-0.0.2 app/commands/decidim/comments/create_comment.rb
decidim-0.0.2 decidim-comments/app/commands/decidim/comments/create_comment.rb
decidim-comments-0.0.1 app/commands/decidim/comments/create_comment.rb
decidim-0.0.1 decidim-comments/app/commands/decidim/comments/create_comment.rb