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

60 lines
1.8 KiB
Ruby
Raw Normal View History

2012-01-30 20:39:34 +00:00
# -*- encoding : utf-8 -*-
class Projects::CommentsController < Projects::BaseController
before_filter :authenticate_user!
load_and_authorize_resource :project
before_filter :find_commentable
before_filter :find_or_build_comment
load_and_authorize_resource :new => :new_line
include CommentsHelper
def create
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
flash[:notice] = I18n.t("flash.comment.saved")
anchor = view_context.comment_anchor(@comment)
else
flash[:error] = I18n.t("flash.comment.save_error")
flash[:warning] = @comment.errors.full_messages.join('. ')
end
redirect_to "#{project_commentable_path(@project, @commentable)}##{anchor}"
end
def edit
end
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
end
def destroy
@comment.destroy
flash[:notice] = t("flash.comment.destroyed")
redirect_to project_commentable_path(@project, @commentable)
end
def new_line
2012-10-16 14:20:55 +01:00
@path = view_context.project_commentable_comments_path(@project, @commentable)
render :layout => false
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
@comment = params[:id].present? && Comment.find(params[:id]) ||
current_user.comments.build(params[:comment]) {|c| c.commentable = @commentable; c.project = @project}
2012-01-17 18:33:42 +00:00
end
end