2012-05-02 10:18:07 +01:00
|
|
|
class Projects::CommentsController < Projects::BaseController
|
2015-03-04 23:19:19 +00:00
|
|
|
before_action :authenticate_user!
|
|
|
|
before_action :find_commentable
|
|
|
|
before_action :find_or_build_comment
|
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
|
2014-12-18 09:59:02 +00:00
|
|
|
respond_to do |format|
|
|
|
|
if !@comment.set_additional_data params
|
|
|
|
format.json {
|
|
|
|
render json: {
|
2015-04-10 12:50:14 +01:00
|
|
|
message: I18n.t("flash.comment.save_error"),
|
|
|
|
error: @comment.errors.full_messages
|
2014-12-18 09:59:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif @comment.save
|
|
|
|
format.json {}
|
|
|
|
else
|
2015-04-10 12:50:14 +01:00
|
|
|
format.json { render json: { message: I18n.t("flash.comment.save_error") }, status: 422 }
|
2014-12-18 09:59:02 +00:00
|
|
|
end
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-20 10:20:00 +00:00
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
2011-12-19 15:30:14 +00:00
|
|
|
def update
|
2015-04-10 12:50:14 +01:00
|
|
|
respond_to do |format|
|
|
|
|
if @comment.update_attributes(params[:comment])
|
|
|
|
format.json { render json: {message:t('flash.comment.updated'), body: view_context.markdown(@comment.body)} }
|
|
|
|
else
|
|
|
|
format.json { render json: {message:t('flash.comment.error_in_updating')}, status: 422 }
|
|
|
|
end
|
2012-09-24 18:34:14 +01:00
|
|
|
end
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2015-04-10 12:50:14 +01:00
|
|
|
respond_to do |format|
|
|
|
|
if @comment.present? && @comment.destroy
|
|
|
|
format.json { render json: {message: I18n.t('flash.comment.destroyed')} }
|
|
|
|
else
|
|
|
|
format.json {
|
|
|
|
render json: {message: t('flash.comment.error_in_deleting')}, status: 422 }
|
|
|
|
end
|
|
|
|
end
|
2011-12-19 15:30:14 +00:00
|
|
|
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
|
2014-03-18 13:58:51 +00:00
|
|
|
@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
|
2014-01-21 04:51:49 +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}
|
2015-04-06 21:42:51 +01:00
|
|
|
authorize @comment
|
2012-01-17 18:33:42 +00:00
|
|
|
end
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|