Skip to content
This repository
tree: d7ff92f45b
Fetching contributors…

Cannot retrieve contributors at this time

file 48 lines (41 sloc) 1.158 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
class Posts::CommentsController < ApplicationController
  cache_sweeper :user_sweeper
  cache_sweeper :comment_sweeper
  
  def create
    @post = find_post(params[:post_id])
    @comment = @post.comments.build(params[:comment]) do |comment|
      comment.user = current_user
    end
    if @comment.save
      render json: { html: @comment.html, id: @comment.id }, status: :created
    else
      errors = @comment.errors.to_a.to_sentence
      render text: errors, status: :unprocessable_entity
    end
  end
  
  def show
    @comment = Comment.find(params[:id])
    render json: { html: @comment.html }
  end
  
  def edit
    @comment = Comment.find(params[:id])
    authorize! :edit, @comment
    render json: { content: @comment.content }
  end
  
  def update
    @comment = Comment.find(params[:id])
    authorize! :edit, @comment
    @comment.update_attribute(:content, params[:content])
    render json: { html: @comment.html }
  end

  def destroy
    @comment = Comment.find(params[:id])
    authorize! :destroy, @comment
    @comment.mark_deleted
    render json: {}
  end
  
protected
  
  def find_post(id)
    Post.find_by_param id
  end
end
Something went wrong with that request. Please try again.