debian-mirror-gitlab/lib/gitlab/view/presenter/base.rb

79 lines
2 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Gitlab
module View
module Presenter
CannotOverrideMethodError = Class.new(StandardError)
module Base
extend ActiveSupport::Concern
include Gitlab::Routing
include Gitlab::Allowable
2022-06-21 17:19:12 +05:30
# Presenters should always access the subject through an explicit getter defined with
# `presents ..., as:`, the `__subject__` method is only intended for internal use.
def __subject__
@subject
end
2017-08-17 22:00:37 +05:30
2019-02-15 15:39:39 +05:30
def can?(user, action, overridden_subject = nil)
2022-06-21 17:19:12 +05:30
super(user, action, overridden_subject || __subject__)
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
# delegate all #can? queries to the subject
def declarative_policy_delegate
2022-06-21 17:19:12 +05:30
__subject__
2017-09-10 17:25:29 +05:30
end
2018-10-15 14:42:47 +05:30
def present(**attributes)
self
end
2020-04-22 19:07:51 +05:30
def url_builder
Gitlab::UrlBuilder.instance
end
2020-10-24 23:57:45 +05:30
def is_a?(type)
2022-06-21 17:19:12 +05:30
super || __subject__.is_a?(type)
2020-10-24 23:57:45 +05:30
end
def web_url
2022-06-21 17:19:12 +05:30
url_builder.build(__subject__)
2020-10-24 23:57:45 +05:30
end
def web_path
2022-06-21 17:19:12 +05:30
url_builder.build(__subject__, only_path: true)
2020-10-24 23:57:45 +05:30
end
2022-10-11 01:57:18 +05:30
def path_with_line_numbers(path, start_line, end_line)
path.tap do |complete_path|
complete_path << "#L#{start_line}"
complete_path << "-#{end_line}" if end_line && end_line != start_line
end
end
2017-08-17 22:00:37 +05:30
class_methods do
def presenter?
true
end
2021-11-18 22:05:49 +05:30
def presents(*target_classes, as: nil)
if target_classes.any? { |k| k.is_a?(Symbol) }
raise ArgumentError, "Unsupported target class type: #{target_classes}."
end
if self < ::Gitlab::View::Presenter::Delegated
target_classes.each { |k| delegator_target(k) }
elsif self < ::Gitlab::View::Presenter::Simple
# no-op
end
2022-06-21 17:19:12 +05:30
define_method(as) { __subject__ } if as
2017-08-17 22:00:37 +05:30
end
end
end
end
end
end