Created
April 1, 2013 14:56
-
-
Save valachi/5285392 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #coding: UTF-8 | |
| class CommentsController < ApplicationController | |
| before_filter :fetch_comment, except: [:create, :new] | |
| before_filter :authenticate_user!, only: :vote | |
| before_filter :prevent_owner_vote, only: :vote | |
| before_filter :get_parent, except: [:vote, :plusminus] | |
| def new | |
| @parent.comments.build | |
| render layout: false | |
| end | |
| def create | |
| @comment = @parent.comments.new(params[:comment]) | |
| @article = @comment.article_parent | |
| @comment.user = current_user | |
| @comment.article_id = @article.id | |
| if @comment.save | |
| respond_to do |format| | |
| format.html { redirect_to article_path(@article), notice: 'Спасибо за комментарий' } | |
| format.js | |
| end | |
| end | |
| end | |
| def destroy | |
| @comment.destroy | |
| redirect_to :back, notice: 'Comment removed' | |
| end | |
| def vote | |
| begin | |
| vote_for_or_against | |
| render text: 'Спасибо за ваш голос', status: 200 | |
| rescue | |
| render text: 'Вы уже голосовали за этот комментарий', status: 404 | |
| end | |
| end | |
| def plusminus | |
| @plusminus = @comment.plusminus | |
| render layout: false | |
| end | |
| private | |
| def get_parent | |
| @parent = Article.find(params[:article_id]) if params[:article_id] | |
| @parent = Comment.find_by_id(params[:comment_id]) if params[:comment_id] | |
| redirect_to root_path, notice: 'Неправильные параметры комментария' unless defined?(@parent) | |
| end | |
| def fetch_comment | |
| @comment = Comment.find(params[:id]) | |
| end | |
| def prevent_owner_vote | |
| if @comment.user == current_user | |
| render text: 'Вы не можете голосовать за собственный комментарий', status: 404 | |
| end | |
| end | |
| def vote_for_or_against | |
| if params[:vote] == 'up' | |
| current_user.vote_for(@comment) | |
| else | |
| current_user.vote_against(@comment) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment