Skip to content
Extraits de code Groupes Projets
comments_controller.rb 1,98 ko
Newer Older
  • Learn to ignore specific revisions
  • danielgrippi's avatar
    danielgrippi a validé
    #   Copyright (c) 2010-2011, Diaspora Inc.  This file is
    
    Raphael's avatar
    Raphael a validé
    #   licensed under the Affero General Public License version 3 or later.  See
    
    Raphael's avatar
    Raphael a validé
    #   the COPYRIGHT file.
    
    class CommentsController < ApplicationController
    
      before_action :authenticate_user!, except: :index
    
      respond_to :html, :mobile, :json
    
      rescue_from ActiveRecord::RecordNotFound do
    
        render nothing: true, status: 404
    
        @comment = CommentService.new(post_id: params[:post_id], text: params[:text], user: current_user).create_comment
    
          respond_create_success
    
    Raphael's avatar
    Raphael a validé
        else
    
          render nothing: true, status: 404
    
    Raphael's avatar
    Raphael a validé
        end
    
        service = CommentService.new(comment_id: params[:id], user: current_user)
        if service.destroy_comment
          respond_destroy_success
    
          respond_destroy_error
    
    Jonne Haß's avatar
    Jonne Haß a validé
        respond_to do |format|
    
          format.mobile { render layout: false }
    
    Jonne Haß's avatar
    Jonne Haß a validé
        end
    
        service = CommentService.new(post_id: params[:post_id], user: current_user)
        @post = service.post
        @comments = service.comments
    
        respond_with do |format|
    
          format.json  { render json: CommentPresenter.as_collection(@comments), status: 200 }
          format.mobile { render layout: false }
    
      def respond_create_success
        respond_to do |format|
          format.json { render json: CommentPresenter.new(@comment), status: 201 }
          format.html { render nothing: true, status: 201 }
          format.mobile { render partial: "comment", locals: {post: @comment.post, comment: @comment} }
        end
      end
    
      def respond_destroy_success
        respond_to do |format|
          format.mobile { redirect_to :back }
          format.js { render nothing: true, status: 204 }
          format.json { render nothing: true, status: 204 }
        end
      end
    
      def respond_destroy_error
        respond_to do |format|
          format.mobile { redirect_to :back }
          format.js { render nothing: true, status: 403 }
          format.json { render nothing: true, status: 403 }