2012-02-18 22:47:57 +00:00
|
|
|
class GitPresenters::CommitAsMessagePresenter < ApplicationPresenter
|
2012-02-29 01:30:22 +00:00
|
|
|
include CommitHelper
|
|
|
|
|
2013-03-22 16:46:26 +00:00
|
|
|
attr_accessor :commit
|
2013-04-09 20:26:27 +01:00
|
|
|
attr_reader :header, :image, :date, :caption, :content, :expandable,
|
2013-04-10 13:26:23 +01:00
|
|
|
:is_reference_to_issue, :committer
|
2012-02-18 22:47:57 +00:00
|
|
|
|
|
|
|
def initialize(commit, opts = {})
|
2013-03-22 16:46:26 +00:00
|
|
|
comment = opts[:comment]
|
2013-03-27 13:37:55 +00:00
|
|
|
@is_reference_to_issue = !!comment # is it reference issue from commit
|
2013-03-22 16:46:26 +00:00
|
|
|
@project = if comment
|
2014-01-21 04:51:49 +00:00
|
|
|
Project.where(id: comment.data[:from_project_id]).first
|
2013-04-09 20:26:27 +01:00
|
|
|
else
|
|
|
|
opts[:project]
|
|
|
|
end
|
2013-04-25 16:00:30 +01:00
|
|
|
commit = commit || @project.repo.commit(Comment.hex_to_commit_hash comment.created_from_commit_hash) if @project
|
2013-03-22 16:46:26 +00:00
|
|
|
|
2013-04-09 12:47:45 +01:00
|
|
|
if @project && commit
|
2014-01-21 04:51:49 +00:00
|
|
|
@committer = User.where(email: commit.committer.email).first || commit.committer
|
2013-03-22 16:46:26 +00:00
|
|
|
@commit_hash = commit.id
|
|
|
|
@committed_date, @authored_date = commit.committed_date, commit.authored_date
|
|
|
|
@commit_message = commit.message
|
|
|
|
else
|
|
|
|
@committer = t('layout.commits.unknown_committer')
|
2013-04-25 16:00:30 +01:00
|
|
|
@commit_hash = Comment.hex_to_commit_hash comment.created_from_commit_hash
|
2013-03-22 16:46:26 +00:00
|
|
|
@committed_date = @authored_date = comment.created_at
|
|
|
|
@commit_message = t('layout.commits.deleted')
|
|
|
|
end
|
2012-02-18 22:47:57 +00:00
|
|
|
prepare_message
|
|
|
|
end
|
|
|
|
|
|
|
|
def header
|
2013-03-27 13:37:55 +00:00
|
|
|
@header ||= if @is_reference_to_issue
|
2014-01-21 04:51:49 +00:00
|
|
|
I18n.t('layout.commits.reference', committer: committer_link, commit: commit_link)
|
2013-03-22 16:46:26 +00:00
|
|
|
elsif @project.present?
|
|
|
|
I18n.t('layout.messages.commits.header',
|
2014-01-21 04:51:49 +00:00
|
|
|
committer: committer_link, commit: commit_link, project: @project.name)
|
2013-03-22 16:46:26 +00:00
|
|
|
end.html_safe
|
2012-02-18 22:47:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def image
|
2013-03-22 16:46:26 +00:00
|
|
|
@image ||= if committer.is_a? User
|
|
|
|
helpers.avatar_url(committer, :medium)
|
|
|
|
elsif committer.is_a? Grit::Actor
|
|
|
|
helpers.avatar_url_by_email(committer.email, :medium)
|
2012-03-21 21:51:39 +00:00
|
|
|
else
|
2013-04-02 16:14:32 +01:00
|
|
|
helpers.gravatar_url('Unknown', User::AVATAR_SIZES[:medium])
|
2012-03-21 21:51:39 +00:00
|
|
|
end
|
2012-02-18 22:47:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def date
|
2013-09-01 13:06:42 +01:00
|
|
|
@date ||= @committed_date || @authored_date
|
2012-02-18 22:47:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def expandable?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2012-02-29 15:04:33 +00:00
|
|
|
def buttons?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2012-02-18 22:47:57 +00:00
|
|
|
def content?
|
|
|
|
!content.blank?
|
|
|
|
end
|
|
|
|
|
2012-03-01 20:33:48 +00:00
|
|
|
def caption?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2012-03-13 18:47:37 +00:00
|
|
|
def comment_id?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2013-03-26 10:47:42 +00:00
|
|
|
def issue_referenced_state?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2013-04-10 11:30:53 +01:00
|
|
|
def reference_project
|
2013-04-10 13:26:23 +01:00
|
|
|
@project if @is_reference_to_issue
|
2013-04-09 20:26:27 +01:00
|
|
|
end
|
|
|
|
|
2012-02-18 22:47:57 +00:00
|
|
|
protected
|
|
|
|
|
2013-03-22 16:46:26 +00:00
|
|
|
def committer_link
|
|
|
|
@committer_link ||= if committer.is_a? User
|
|
|
|
link_to committer.uname, user_path(committer)
|
|
|
|
elsif committer.is_a? Grit::Actor
|
|
|
|
mail_to committer.email, committer.name
|
|
|
|
else # unknown committer
|
|
|
|
committer
|
2012-02-18 22:47:57 +00:00
|
|
|
end
|
2013-03-22 16:46:26 +00:00
|
|
|
end
|
2012-02-18 22:47:57 +00:00
|
|
|
|
2013-03-22 16:46:26 +00:00
|
|
|
def commit_link
|
|
|
|
if @project
|
|
|
|
link_to shortest_hash_id(@commit_hash), commit_path(@project, @commit_hash)
|
|
|
|
else
|
|
|
|
shortest_hash_id(@commit_hash)
|
2012-02-18 22:47:57 +00:00
|
|
|
end
|
2013-03-22 16:46:26 +00:00
|
|
|
end
|
2012-02-18 22:47:57 +00:00
|
|
|
|
2013-03-22 16:46:26 +00:00
|
|
|
def prepare_message
|
|
|
|
(@caption, @content) = @commit_message.split("\n\n", 2)
|
|
|
|
@caption = 'empty message' unless @caption.present?
|
|
|
|
if @caption.length > 72
|
|
|
|
tmp = '...' + @caption[69..-1]
|
|
|
|
@content = (@content.present?) ? tmp + @content : tmp
|
|
|
|
@caption = @caption[0..68] + '...'
|
2012-02-29 01:30:22 +00:00
|
|
|
end
|
2012-03-01 01:07:31 +00:00
|
|
|
# @content = @content.gsub("\n", "<br />").html_safe if @content
|
2014-01-21 04:51:49 +00:00
|
|
|
@content = simple_format(@content, {}, sanitize: true).html_safe if @content
|
2013-03-22 16:46:26 +00:00
|
|
|
end
|
2012-02-18 22:47:57 +00:00
|
|
|
end
|