2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2012-05-02 10:18:07 +01:00
|
|
|
class Projects::CommentsController < Projects::BaseController
|
2011-12-19 15:30:14 +00:00
|
|
|
before_filter :authenticate_user!
|
2012-04-04 22:43:06 +01:00
|
|
|
load_and_authorize_resource :project
|
|
|
|
before_filter :find_commentable
|
|
|
|
before_filter :find_or_build_comment
|
2012-10-05 13:32:56 +01:00
|
|
|
load_and_authorize_resource :new => :new_line
|
2012-03-06 20:51:51 +00:00
|
|
|
|
2012-04-04 22:43:06 +01:00
|
|
|
include CommentsHelper
|
2011-12-19 15:30:14 +00:00
|
|
|
|
|
|
|
def create
|
2012-10-12 20:56:02 +01:00
|
|
|
anchor = ''
|
2012-10-16 10:50:21 +01:00
|
|
|
if !@comment.set_additional_data params
|
2012-10-08 16:31:30 +01:00
|
|
|
flash[:error] = I18n.t("flash.comment.save_error")
|
|
|
|
elsif @comment.save
|
2011-12-19 15:30:14 +00:00
|
|
|
flash[:notice] = I18n.t("flash.comment.saved")
|
2012-10-12 20:56:02 +01:00
|
|
|
anchor = view_context.comment_anchor(@comment)
|
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")
|
2012-09-24 16:45:15 +01:00
|
|
|
flash[:warning] = @comment.errors.full_messages.join('. ')
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|
2012-10-12 20:56:02 +01:00
|
|
|
redirect_to "#{project_commentable_path(@project, @commentable)}##{anchor}"
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|
|
|
|
|
2011-12-20 10:20:00 +00:00
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
2011-12-19 15:30:14 +00:00
|
|
|
def update
|
2012-09-24 18:34:14 +01:00
|
|
|
status, message = if @comment.update_attributes(params[:comment])
|
|
|
|
[200, view_context.markdown(@comment.body)]
|
|
|
|
else
|
|
|
|
[400, view_context.local_alert(@comment.errors.full_messages.join('. '))]
|
|
|
|
end
|
|
|
|
render :inline => message, :status => status
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@comment.destroy
|
|
|
|
flash[:notice] = t("flash.comment.destroyed")
|
2012-04-04 22:43:06 +01:00
|
|
|
redirect_to project_commentable_path(@project, @commentable)
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|
|
|
|
|
2012-10-04 19:40:12 +01:00
|
|
|
def new_line
|
2012-10-16 14:20:55 +01:00
|
|
|
@path = view_context.project_commentable_comments_path(@project, @commentable)
|
2012-10-04 19:40:12 +01:00
|
|
|
render :layout => false
|
|
|
|
end
|
|
|
|
|
2012-04-04 22:43:06 +01:00
|
|
|
protected
|
2011-12-19 15:30:14 +00:00
|
|
|
|
2012-04-04 22:43:06 +01:00
|
|
|
def find_commentable
|
|
|
|
@commentable = params[:issue_id].present? && @project.issues.find_by_serial_id(params[:issue_id]) ||
|
2012-07-17 09:02:56 +01:00
|
|
|
params[:commit_id].present? && @project.repo.commit(params[:commit_id])
|
2011-12-20 10:20:00 +00:00
|
|
|
end
|
2011-12-21 14:48:16 +00:00
|
|
|
|
2012-04-04 22:43:06 +01:00
|
|
|
def find_or_build_comment
|
2013-03-22 16:46:26 +00:00
|
|
|
@comment = params[:id].present? && Comment.where(:automatic => false).find(params[:id]) ||
|
2012-04-05 18:21:25 +01:00
|
|
|
current_user.comments.build(params[:comment]) {|c| c.commentable = @commentable; c.project = @project}
|
2012-01-17 18:33:42 +00:00
|
|
|
end
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|