2018-11-09 13:39:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-21 03:52:24 -04:00
|
|
|
module Gitlab
|
|
|
|
module Graphql
|
2018-05-23 03:55:14 -04:00
|
|
|
module Present
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
prepended do
|
|
|
|
def self.present_using(kls)
|
|
|
|
@presenter_class = kls
|
2018-05-21 03:52:24 -04:00
|
|
|
end
|
|
|
|
|
2018-05-23 03:55:14 -04:00
|
|
|
def self.presenter_class
|
2021-04-27 20:10:21 -04:00
|
|
|
@presenter_class || superclass.try(:presenter_class)
|
2018-05-21 03:52:24 -04:00
|
|
|
end
|
2021-03-11 07:09:28 -05:00
|
|
|
|
|
|
|
def self.present(object, attrs)
|
2021-04-27 20:10:21 -04:00
|
|
|
klass = presenter_class
|
2021-03-11 07:09:28 -05:00
|
|
|
return object if !klass || object.is_a?(klass)
|
|
|
|
|
2021-04-27 20:10:21 -04:00
|
|
|
klass.new(object, **attrs)
|
2021-03-11 07:09:28 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def unpresented
|
|
|
|
unwrapped || object
|
2018-05-21 03:52:24 -04:00
|
|
|
end
|
2018-05-23 03:55:14 -04:00
|
|
|
|
2021-03-11 07:09:28 -05:00
|
|
|
def present(object_type, attrs)
|
|
|
|
return unless object_type.respond_to?(:present)
|
|
|
|
|
|
|
|
self.unwrapped ||= object
|
|
|
|
# @object belongs to Schema::Object, which does not expose a writer.
|
|
|
|
@object = object_type.present(unwrapped, attrs) # rubocop: disable Gitlab/ModuleWithInstanceVariables
|
2018-05-23 03:55:14 -04:00
|
|
|
end
|
2021-03-11 07:09:28 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_accessor :unwrapped
|
2018-05-21 03:52:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|