Sha256: cc4fb1681714a27431768a244a2938a32e498e7e18f8a52e34d7b8a0905e7da8

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

module Talkie
  class CommentsController < TalkieController
    before_action :current_comment, only: [:destroy]

    def create
      @comment = Talkie::Comment.new(comment_params)
      @comment.creator = current_user

      respond_to do |format|
        if @comment.save
          make_child_comment if reply?
          format.html { redirect_to Talkie.success_redirect_to.call(main_app, @comment.commentable),
                                    notice: t('talkie.controllers.comments.flash.success') }
          format.js
        else
          format.html { redirect_back fallback_location: Talkie.fail_redirect_to.call(main_app),
                        notice: t('talkie.controllers.comments.flash.error') }
          format.js
        end
      end
    end

    def destroy
      current_comment.destroy
      redirect_to main_app.polymorphic_path(current_comment.commentable)
    end

    private

    def comment_params
      params.require(:comment).permit(:body, :commentable_id, :commentable_type)
    end

    def reply?
      params[:parent_comment_id].present?
    end

    def make_child_comment
      parent_comment = Comment.find params[:parent_comment_id]
      @comment.move_to_child_of(parent_comment)
    end

    def current_comment
      @current_comment ||= Talkie::Comment.find_by(id: params[:id])
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
talkie-0.2.0 app/controllers/talkie/comments_controller.rb