rosa-build/app/presenters/comment_presenter.rb

121 lines
3.7 KiB
Ruby
Raw Normal View History

2012-02-29 15:04:33 +00:00
class CommentPresenter < ApplicationPresenter
2013-03-26 10:47:42 +00:00
include PullRequestHelper
2012-02-29 15:04:33 +00:00
attr_accessor :comment, :options
attr_reader :header, :image, :date, :caption, :content, :buttons, :is_reference_to_issue,
:reference_project
2012-02-29 15:04:33 +00:00
def initialize(comment, opts = {})
2013-04-01 18:24:19 +01:00
@is_reference_to_issue = !!(comment.automatic && comment.created_from_issue_id) # is it reference issue from another issue
2013-03-26 10:47:42 +00:00
@comment, @user, @options = comment, comment.user, opts
unless @is_reference_to_issue
@content = @comment.body
else
2014-01-21 04:51:49 +00:00
issue = Issue.where(id: comment.created_from_issue_id).first
2013-03-26 10:47:42 +00:00
@referenced_issue = issue.pull_request || issue
@reference_project = issue.project
if issue && (comment.data[:comment_id].nil? || Comment.exists?(comment.data[:comment_id]))
2013-03-26 10:47:42 +00:00
title = if issue == opts[:commentable]
"#{issue.serial_id}"
elsif issue.project.owner == opts[:commentable].project.owner
"#{issue.project.name}##{issue.serial_id}"
else
"#{issue.project.name_with_owner}##{issue.serial_id}"
end
title = "<span style=\"color: #777;\">#{title}</span>:"
issue_link = project_issue_path(issue.project, issue)
@content = "<a href=\"#{issue_link}\">#{title} #{issue.title}</a>".html_safe
else
@content = t 'layout.comments.removed'
end
end
2012-02-29 15:04:33 +00:00
end
def expandable?
false
end
def buttons?
2013-03-26 10:47:42 +00:00
!@is_reference_to_issue # dont show for automatic comment
2012-02-29 15:04:33 +00:00
end
def content?
true
2012-02-29 15:04:33 +00:00
end
def caption?
false
end
2012-10-08 10:14:31 +01:00
2013-03-26 10:47:42 +00:00
def issue_referenced_state?
@referenced_issue # show state of the existing referenced issue
end
2012-02-29 15:04:33 +00:00
def buttons
2012-10-08 10:14:31 +01:00
project, commentable = options[:project], options[:commentable]
2012-02-29 15:04:33 +00:00
link_to_comment = "#{helpers.project_commentable_path(project, commentable)}##{comment_anchor}"
klass = "#{@options[:in_discussion].present? ? 'in_discussion_' : ''}link_to_comment"
res = [ link_to(content_tag(:i, nil, class: 'fa fa-link'),
link_to_comment,
class: klass).html_safe ]
2012-02-29 15:04:33 +00:00
if controller.can? :update, @comment
res << link_to(content_tag(:i, nil, class: 'fa fa-edit'),
"#update-comment#{comment.id}",
'ng-click' => "commentsCtrl.toggleEditForm(#{comment_id})" ).html_safe
2012-02-29 15:04:33 +00:00
end
2012-10-05 07:13:14 +01:00
if controller.can? :destroy, @comment
res << link_to(content_tag(:i, nil, class: 'fa fa-close'),
'',
'ng-click' => "commentsCtrl.remove(#{comment_id})").html_safe
2012-02-29 15:04:33 +00:00
end
2012-03-01 18:35:18 +00:00
res
2012-02-29 15:04:33 +00:00
end
def header
2013-03-26 10:47:42 +00:00
user_link = link_to @user.fullname, user_path(@user.uname)
res = unless @is_reference_to_issue
"#{user_link} #{t 'layout.comments.has_commented'}"
else
2014-01-21 04:51:49 +00:00
t 'layout.comments.reference', user: user_link
2013-03-26 10:47:42 +00:00
end
res.html_safe
2012-02-29 15:04:33 +00:00
end
def image
@image ||= helpers.avatar_url(@user, :medium)
2012-02-29 15:04:33 +00:00
end
def date
2013-09-01 13:06:42 +01:00
@date ||= @comment.created_at
2012-02-29 15:04:33 +00:00
end
def comment_id?
true
end
def comment_id
@comment.id
end
2012-10-08 10:14:31 +01:00
def comment_anchor
# check for pull diff inline comment
before = if @options[:add_anchor].present? && !@options[:in_discussion]
'diff-'
else
''
end
2012-10-08 10:14:31 +01:00
"#{before}comment#{@comment.id}"
end
2013-03-26 10:47:42 +00:00
def issue_referenced_state
if @referenced_issue.is_a? Issue
statuses = {'open' => 'success', 'closed' => 'important'}
content_tag :span, t("layout.issues.status.#{@referenced_issue.status}"), class: "pull-right label label-#{statuses[@referenced_issue.status]}"
2013-03-26 10:47:42 +00:00
else
pull_status_label @referenced_issue.status, class: 'pull-right'
2013-03-26 10:47:42 +00:00
end.html_safe
end
2012-02-29 15:04:33 +00:00
end