2011-12-19 15:30:14 +00:00
|
|
|
class CommentsController < ApplicationController
|
|
|
|
before_filter :authenticate_user!
|
2011-12-23 10:56:46 +00:00
|
|
|
before_filter :find_comment, :only => [:edit, :update, :destroy]
|
2012-01-11 18:15:35 +00:00
|
|
|
before_filter :set_commentable, :only => [:index, :edit, :create, :update]
|
|
|
|
#before_filter :find_project, :only => [:index, :edit]
|
2011-12-21 14:48:16 +00:00
|
|
|
|
|
|
|
authorize_resource :only => [:show, :edit, :update, :destroy]
|
|
|
|
authorize_resource :project, :only => [:index]
|
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
|
|
|
|
@comment = Comment.new(params[:comment].merge(:commentable_id => @commentable.id, :commentable_type => @commentable.class.name)) if @commentable.class == Grit::Commit
|
2011-12-20 10:20:00 +00:00
|
|
|
@comment.user = current_user
|
2011-12-19 15:30:14 +00:00
|
|
|
if @comment.save
|
|
|
|
flash[:notice] = I18n.t("flash.comment.saved")
|
2012-01-11 18:15:35 +00:00
|
|
|
redirect_to :back
|
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"
|
|
|
|
project_commit_comment_path(@project, @commentable.id, @comment)
|
|
|
|
end
|
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")
|
2011-12-22 07:41:37 +00:00
|
|
|
#redirect_to :back
|
2012-01-11 18:15:35 +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")
|
2011-12-20 10:20:00 +00:00
|
|
|
redirect_to :back
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_commentable
|
2011-12-21 14:48:16 +00:00
|
|
|
#params.each do |name, value|
|
|
|
|
# if name =~ /(.+)_id$/
|
|
|
|
# return $1.classify.constantize.find(value)
|
|
|
|
# end
|
|
|
|
#end
|
|
|
|
#nil
|
2012-01-11 18:15:35 +00:00
|
|
|
if params[:issue_id].present?
|
|
|
|
return Issue.find(params[:issue_id])
|
|
|
|
elsif params[:commit_id].present?
|
|
|
|
return @project.git_repository.commit(params[:commit_id])
|
|
|
|
end
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|
2011-12-20 10:20:00 +00:00
|
|
|
|
|
|
|
def set_commentable
|
2012-01-11 18:15:35 +00:00
|
|
|
find_project
|
2011-12-20 10:20:00 +00:00
|
|
|
@commentable = find_commentable
|
2012-01-11 18:15:35 +00:00
|
|
|
@commentable_path = @commentable.class == Issue ? project_issue_path(@project, @commentable) : commit_path(@project.id, @commentable.id)
|
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])
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_project
|
2012-01-11 18:15:35 +00:00
|
|
|
@project = Project.find(params[:project_id])
|
2011-12-21 14:48:16 +00:00
|
|
|
end
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|