2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2011-12-19 15:30:14 +00:00
|
|
|
class CommentsController < ApplicationController
|
|
|
|
before_filter :authenticate_user!
|
2012-03-06 20:51:51 +00:00
|
|
|
|
|
|
|
load_resource :project
|
2012-03-06 21:49:29 +00:00
|
|
|
before_filter :set_commentable
|
2012-01-13 15:58:48 +00:00
|
|
|
before_filter :find_comment, :only => [:edit, :update, :destroy]
|
2012-03-06 20:51:51 +00:00
|
|
|
authorize_resource
|
2011-12-19 15:30:14 +00:00
|
|
|
|
|
|
|
def index
|
|
|
|
@comments = @commentable.comments
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2012-01-11 18:15:35 +00:00
|
|
|
@comment = @commentable.comments.build(params[:comment]) if @commentable.class == Issue
|
2012-03-06 20:51:51 +00:00
|
|
|
if @commentable.class == Grit::Commit
|
|
|
|
@comment = Comment.new(params[:comment].merge(:commentable_id => @commentable.id.hex, :commentable_type => @commentable.class.name))
|
|
|
|
@comment.project = @project
|
|
|
|
end
|
|
|
|
@comment.user_id = current_user.id
|
2011-12-19 15:30:14 +00:00
|
|
|
if @comment.save
|
|
|
|
flash[:notice] = I18n.t("flash.comment.saved")
|
2012-01-17 18:33:42 +00:00
|
|
|
redirect_to commentable_path
|
2011-12-19 15:30:14 +00:00
|
|
|
else
|
2011-12-28 02:57:42 +00:00
|
|
|
flash[:error] = I18n.t("flash.comment.save_error")
|
2011-12-19 15:30:14 +00:00
|
|
|
render :action => 'new'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-20 10:20:00 +00:00
|
|
|
def edit
|
2012-01-11 18:15:35 +00:00
|
|
|
@update_url = case @commentable.class.name
|
|
|
|
when "Issue"
|
|
|
|
project_issue_comment_path(@project, @commentable, @comment)
|
|
|
|
when "Grit::Commit"
|
2012-01-16 18:51:56 +00:00
|
|
|
project_commit_comment_path(@project, @commentable, @comment)
|
2012-01-11 18:15:35 +00:00
|
|
|
end
|
2012-01-17 18:33:42 +00:00
|
|
|
@commentable_path = commentable_path
|
2011-12-20 10:20:00 +00:00
|
|
|
end
|
|
|
|
|
2011-12-19 15:30:14 +00:00
|
|
|
def update
|
|
|
|
if @comment.update_attributes(params[:comment])
|
|
|
|
flash[:notice] = I18n.t("flash.comment.saved")
|
2012-01-17 18:33:42 +00:00
|
|
|
redirect_to commentable_path
|
2011-12-19 15:30:14 +00:00
|
|
|
else
|
2011-12-28 02:57:42 +00:00
|
|
|
flash[:error] = I18n.t("flash.comment.save_error")
|
2011-12-19 15:30:14 +00:00
|
|
|
render :action => 'new'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@comment.destroy
|
|
|
|
|
|
|
|
flash[:notice] = t("flash.comment.destroyed")
|
2012-01-17 18:33:42 +00:00
|
|
|
redirect_to commentable_path
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2011-12-20 10:20:00 +00:00
|
|
|
def set_commentable
|
2012-03-06 20:51:51 +00:00
|
|
|
@commentable = if params[:issue_id].present?
|
|
|
|
@project.issues.find_by_serial_id params[:issue_id]
|
|
|
|
elsif params[:commit_id].present?
|
|
|
|
@project.git_repository.commit params[:commit_id]
|
|
|
|
end
|
2011-12-20 10:20:00 +00:00
|
|
|
end
|
2011-12-21 14:48:16 +00:00
|
|
|
|
|
|
|
def find_comment
|
|
|
|
@comment = Comment.find(params[:id])
|
2012-03-03 00:19:31 +00:00
|
|
|
if @comment.commit_comment?
|
2012-01-20 15:17:05 +00:00
|
|
|
@comment.project = @project
|
|
|
|
@comment.helper
|
|
|
|
end
|
2011-12-21 14:48:16 +00:00
|
|
|
end
|
|
|
|
|
2012-01-17 18:33:42 +00:00
|
|
|
def commentable_path
|
|
|
|
@commentable.class == Issue ? [@project, @commentable] : commit_path(@project, @commentable.id)
|
|
|
|
end
|
|
|
|
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|