rosa-build/app/controllers/projects/comments_controller.rb

62 lines
1.9 KiB
Ruby
Raw Normal View History

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
include CommentsHelper
def create
respond_to do |format|
if !@comment.set_additional_data params
format.json {
render json: {
message: I18n.t("flash.comment.save_error"),
error: @comment.errors.full_messages
}
}
elsif @comment.save
format.json {}
else
format.json { render json: { message: I18n.t("flash.comment.save_error") }, status: 422 }
end
end
end
def edit
end
def update
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
end
def destroy
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
end
protected
def find_commentable
@commentable = params[:issue_id].present? && @project.issues.find_by(serial_id: params[:issue_id]) ||
params[:commit_id].present? && @project.repo.commit(params[:commit_id])
end
def find_or_build_comment
2014-01-21 04:51:49 +00:00
@comment = params[:id].present? && Comment.where(automatic: false).find(params[:id]) ||
current_user.comments.build(params[:comment]) {|c| c.commentable = @commentable; c.project = @project}
authorize @comment
2012-01-17 18:33:42 +00:00
end
end