2019-12-10 02:53:40 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class SnippetPresenter < Gitlab::View::Presenter::Delegated
|
|
|
|
presents :snippet
|
|
|
|
|
|
|
|
def raw_url
|
2020-07-29 14:09:50 -04:00
|
|
|
url_builder.build(snippet, raw: true)
|
2019-12-10 02:53:40 -05:00
|
|
|
end
|
|
|
|
|
2020-03-19 05:09:27 -04:00
|
|
|
def ssh_url_to_repo
|
2020-04-23 14:09:46 -04:00
|
|
|
snippet.ssh_url_to_repo if snippet.repository_exists?
|
2020-03-19 05:09:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def http_url_to_repo
|
2020-04-23 14:09:46 -04:00
|
|
|
snippet.http_url_to_repo if snippet.repository_exists?
|
2020-03-19 05:09:27 -04:00
|
|
|
end
|
|
|
|
|
2019-12-10 02:53:40 -05:00
|
|
|
def can_read_snippet?
|
|
|
|
can_access_resource?("read")
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_update_snippet?
|
|
|
|
can_access_resource?("update")
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_admin_snippet?
|
|
|
|
can_access_resource?("admin")
|
|
|
|
end
|
|
|
|
|
2019-12-16 13:08:22 -05:00
|
|
|
def can_report_as_spam?
|
|
|
|
snippet.submittable_as_spam_by?(current_user)
|
|
|
|
end
|
|
|
|
|
2020-02-25 04:09:10 -05:00
|
|
|
def blob
|
2020-06-10 08:08:58 -04:00
|
|
|
blobs.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def blobs
|
2020-04-23 14:09:46 -04:00
|
|
|
if snippet.empty_repo?
|
2020-06-10 08:08:58 -04:00
|
|
|
[snippet.blob]
|
2020-04-23 14:09:46 -04:00
|
|
|
else
|
2020-06-10 08:08:58 -04:00
|
|
|
snippet.blobs
|
2020-02-25 04:09:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-10 02:53:40 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def can_access_resource?(ability_prefix)
|
|
|
|
can?(current_user, ability_name(ability_prefix), snippet)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ability_name(ability_prefix)
|
2019-12-13 04:08:01 -05:00
|
|
|
"#{ability_prefix}_#{snippet.to_ability_name}".to_sym
|
2019-12-10 02:53:40 -05:00
|
|
|
end
|
|
|
|
end
|