Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: f185e5070c
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 58 lines (53 sloc) 1.644 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 49 50 51 52 53 54 55 56 57
class CommentsController < ApplicationController
  def create
    render_422 and return if params[:comment].blank?
    @comment = parent.comments.new(params[:comment].merge(:user => current_user))
    if current_user || params[:skip] == 'true' || verify_recaptcha(:model => @comment, :message => @comment.body)
      if @comment.save
        redirect_to parent_url, notice: "Your Comment was successfully created!"
      else
        render failure_page
      end
    else
      flash.delete :recaptcha_error
      render failure_page, error: "Not correct captcha!"
    end
  end

  def index
    @comments = Comment.for_post.includes(:user).page(params[:page] || 1)
  end

  private
    def parent
      if params[:question_id]
        @question = Question.find_cached(params[:question_id])
      elsif params[:answer_id]
        @answer = Answer.find_cached(params[:answer_id])
      elsif params[:post_id]
        @post = Post.find_cached(params[:post_id])
      elsif params[:blog_post_id]
        @blog_post = BlogPost.find_cached(params[:blog_post_id])
      end
    end

    def parent_url
      if params[:question_id]
        question_path(@question)
      elsif params[:answer_id]
        question_path(@answer.cached_question)
      elsif params[:post_id]
        post_path(@post)
      elsif params[:blog_post_id]
        blog_post_path(@blog_post)
      end
    end

    def failure_page
      if params[:question_id]
        'questions/show'
      elsif params[:answer_id]
        'questions/show'
      elsif params[:post_id]
        'posts/show'
      elsif params[:blog_post_id]
        'blog_posts/show'
      end
    end
end
Something went wrong with that request. Please try again.