2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
module Gitlab
|
|
|
|
module Graphql
|
|
|
|
module Present
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
prepended do
|
|
|
|
def self.present_using(kls)
|
|
|
|
@presenter_class = kls
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.presenter_class
|
2021-06-08 01:23:25 +05:30
|
|
|
@presenter_class || superclass.try(:presenter_class)
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
def self.present(object, attrs)
|
2021-06-08 01:23:25 +05:30
|
|
|
klass = presenter_class
|
2021-04-17 20:07:23 +05:30
|
|
|
return object if !klass || object.is_a?(klass)
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
klass.new(object, **attrs)
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def unpresented
|
|
|
|
unwrapped || object
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
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-11-08 19:23:39 +05:30
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_accessor :unwrapped
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|