2018-11-19 21:01:13 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-09 15:45:49 -05:00
|
|
|
module Gitlab
|
|
|
|
module View
|
|
|
|
module Presenter
|
2017-01-24 11:07:56 -05:00
|
|
|
CannotOverrideMethodError = Class.new(StandardError)
|
|
|
|
|
2017-01-09 15:45:49 -05:00
|
|
|
module Base
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
include Gitlab::Routing
|
|
|
|
include Gitlab::Allowable
|
|
|
|
|
|
|
|
attr_reader :subject
|
|
|
|
|
2018-11-15 05:13:50 -05:00
|
|
|
def can?(user, action, overridden_subject = nil)
|
|
|
|
super(user, action, overridden_subject || subject)
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|
|
|
|
|
2017-06-22 14:08:40 -04:00
|
|
|
# delegate all #can? queries to the subject
|
|
|
|
def declarative_policy_delegate
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
2018-04-19 03:20:53 -04:00
|
|
|
def present(**attributes)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2020-04-06 14:09:37 -04:00
|
|
|
def url_builder
|
|
|
|
Gitlab::UrlBuilder.instance
|
|
|
|
end
|
|
|
|
|
2020-07-21 14:09:45 -04:00
|
|
|
def is_a?(type)
|
|
|
|
super || subject.is_a?(type)
|
|
|
|
end
|
|
|
|
|
2020-07-29 14:09:50 -04:00
|
|
|
def web_url
|
|
|
|
url_builder.build(subject)
|
|
|
|
end
|
|
|
|
|
|
|
|
def web_path
|
|
|
|
url_builder.build(subject, only_path: true)
|
|
|
|
end
|
|
|
|
|
2017-01-09 15:45:49 -05:00
|
|
|
class_methods do
|
2017-01-10 17:41:04 -05:00
|
|
|
def presenter?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2021-09-21 05:12:21 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
define_method(as) { subject } if as
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|