Skip to content
Extraits de code Groupes Projets
comments_controller.rb 2,01 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
    
        head :not_found
    
        begin
          comment = comment_service.create(params[:post_id], params[:text])
        rescue ActiveRecord::RecordNotFound
    
          render text: I18n.t("comments.create.error"), status: 404
    
        if comment
          respond_create_success(comment)
    
    Raphael's avatar
    Raphael a validé
        else
    
          render text: I18n.t("comments.create.error"), status: 422
    
    Raphael's avatar
    Raphael a validé
        end
    
        if comment_service.destroy(params[:id])
    
          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
    
        comments = comment_service.find_for_post(params[:post_id])
    
        respond_with do |format|
    
          format.json { render json: CommentPresenter.as_collection(comments), status: 200 }
          format.mobile { render layout: false, locals: {comments: comments} }
    
      def comment_service
        @comment_service ||= CommentService.new(current_user)
      end
    
      def respond_create_success(comment)
    
        respond_to do |format|
    
          format.json { render json: CommentPresenter.new(comment), status: 201 }
    
          format.html { head :created }
    
          format.mobile { render partial: "comment", locals: {comment: comment} }
    
        end
      end
    
      def respond_destroy_success
        respond_to do |format|
    
          format.mobile { redirect_back fallback_location: stream_path }
    
          format.js { head :no_content }
          format.json { head :no_content }
    
        end
      end
    
      def respond_destroy_error
        respond_to do |format|
    
          format.mobile { redirect_back fallback_location: stream_path }
    
          format.js { head :forbidden }
          format.json { head :forbidden }