debian-mirror-gitlab/app/presenters/gitlab/blame_presenter.rb

91 lines
2.8 KiB
Ruby
Raw Normal View History

2020-06-23 00:09:42 +05:30
# frozen_string_literal: true
module Gitlab
class BlamePresenter < Gitlab::View::Presenter::Simple
include ActionView::Helpers::TranslationHelper
include ActionView::Context
include AvatarsHelper
include BlameHelper
include CommitsHelper
include ApplicationHelper
include TreeHelper
include IconsHelper
2021-11-18 22:05:49 +05:30
presents nil, as: :blame
2020-06-23 00:09:42 +05:30
CommitData = Struct.new(
:author_avatar,
:age_map_class,
:commit_link,
:commit_author_link,
:project_blame_link,
:time_ago_tooltip)
2022-06-21 17:19:12 +05:30
def initialize(blame, **attributes)
2020-06-23 00:09:42 +05:30
super
@commits = {}
precalculate_data_by_commit!
end
2022-06-21 17:19:12 +05:30
def first_line
blame.first_line
end
2020-06-23 00:09:42 +05:30
def groups
@groups ||= blame.groups
end
2022-06-21 17:19:12 +05:30
def commit_data(commit, previous_path = nil)
@commits[commit.id] ||= get_commit_data(commit, previous_path)
2020-06-23 00:09:42 +05:30
end
private
# Huge source files with a high churn rate (e.g. 'locale/gitlab.pot') could have
# 10x times more blame groups than unique commits across all the groups.
# That means we could cache per-commit data we need
# to avoid recalculating it multiple times.
# For such files, it could significantly improve the performance of the Blame.
def precalculate_data_by_commit!
2022-06-21 17:19:12 +05:30
groups.each { |group| commit_data(group[:commit], group[:previous_path]) }
2020-06-23 00:09:42 +05:30
end
2022-06-21 17:19:12 +05:30
def get_commit_data(commit, previous_path = nil)
2020-06-23 00:09:42 +05:30
CommitData.new.tap do |data|
2021-09-30 23:02:18 +05:30
data.author_avatar = author_avatar(commit, size: 36, has_tooltip: false, lazy: true)
2020-06-23 00:09:42 +05:30
data.age_map_class = age_map_class(commit.committed_date, project_duration)
data.commit_link = link_to commit.title, project_commit_path(project, commit.id), class: "cdark", title: commit.title
data.commit_author_link = commit_author_link(commit, avatar: false)
2022-06-21 17:19:12 +05:30
data.project_blame_link = project_blame_link(commit, previous_path)
2020-06-23 00:09:42 +05:30
data.time_ago_tooltip = time_ago_with_tooltip(commit.committed_date)
end
end
2022-06-21 17:19:12 +05:30
def project_blame_link(commit, previous_path = nil)
2020-06-23 00:09:42 +05:30
previous_commit_id = commit.parent_id
2022-06-21 17:19:12 +05:30
return unless previous_commit_id && !previous_path.nil?
2020-06-23 00:09:42 +05:30
2022-08-13 15:12:31 +05:30
link_to project_blame_path(project, tree_join(previous_commit_id, previous_path), page: page),
2020-06-23 00:09:42 +05:30
title: _('View blame prior to this change'),
aria: { label: _('View blame prior to this change') },
2021-10-27 15:23:28 +05:30
class: 'version-link',
2020-06-23 00:09:42 +05:30
data: { toggle: 'tooltip', placement: 'right', container: 'body' } do
2021-10-27 15:23:28 +05:30
'&nbsp;'.html_safe
2020-06-23 00:09:42 +05:30
end
end
def project_duration
@project_duration ||= age_map_duration(groups, project)
end
2022-05-07 20:08:51 +05:30
def link_to(*args, &block)
ActionController::Base.helpers.link_to(*args, &block)
end
def mail_to(*args, &block)
ActionController::Base.helpers.mail_to(*args, &block)
end
2020-06-23 00:09:42 +05:30
end
end