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 main_app.polymorphic_path(@comment.commentable), notice: "Comment was successfully added." } format.js else format.html { redirect_to :back, notice: "Something went wrong, blank comments are not allowed" } 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