rosa-build/app/controllers/comments_controller.rb

91 lines
2.6 KiB
Ruby
Raw Normal View History

class CommentsController < ApplicationController
before_filter :authenticate_user!
2012-01-14 12:16:42 +00:00
before_filter :set_commentable, :only => [:index, :edit, :create, :update, :destroy]
2012-01-11 18:15:35 +00:00
#before_filter :find_project, :only => [:index, :edit]
2012-01-13 15:58:48 +00:00
before_filter :find_comment, :only => [:edit, :update, :destroy]
authorize_resource :only => [:show, :edit, :update, :destroy]
authorize_resource :project, :only => [:index]
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
@comment.user = current_user
if @comment.save
flash[:notice] = I18n.t("flash.comment.saved")
2012-01-17 18:33:42 +00:00
redirect_to commentable_path
else
flash[:error] = I18n.t("flash.comment.save_error")
render :action => 'new'
end
end
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, @comment)
2012-01-11 18:15:35 +00:00
end
2012-01-17 18:33:42 +00:00
@commentable_path = commentable_path
end
def update
if @comment.update_attributes(params[:comment])
flash[:notice] = I18n.t("flash.comment.saved")
2012-01-17 18:33:42 +00:00
redirect_to commentable_path
else
flash[:error] = I18n.t("flash.comment.save_error")
render :action => 'new'
end
end
def destroy
@comment.destroy
flash[:notice] = t("flash.comment.destroyed")
2012-01-17 18:33:42 +00:00
redirect_to commentable_path
end
private
def find_commentable
#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_by_serial_id_and_project_id(params[:issue_id], params[:project_id])
2012-01-11 18:15:35 +00:00
elsif params[:commit_id].present?
return @project.git_repository.commit(params[:commit_id])
end
end
def set_commentable
2012-01-11 18:15:35 +00:00
find_project
@commentable = find_commentable
end
def find_comment
@comment = Comment.find(params[:id])
2012-01-13 15:58:48 +00:00
@comment.project = @project if @comment.commentable_type == 'Grit::Commit'
end
def find_project
2012-01-11 18:15:35 +00:00
@project = Project.find(params[:project_id])
end
2012-01-17 18:33:42 +00:00
protected
def commentable_path
@commentable.class == Issue ? [@project, @commentable] : commit_path(@project, @commentable.id)
end
end