debian-mirror-gitlab/lib/gitlab/graphql/present.rb
2021-04-17 20:07:23 +05:30

41 lines
947 B
Ruby

# frozen_string_literal: true
module Gitlab
module Graphql
module Present
extend ActiveSupport::Concern
prepended do
def self.present_using(kls)
@presenter_class = kls
end
def self.presenter_class
@presenter_class
end
def self.present(object, attrs)
klass = @presenter_class
return object if !klass || object.is_a?(klass)
@presenter_class.new(object, **attrs)
end
end
def unpresented
unwrapped || object
end
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
end
private
attr_accessor :unwrapped
end
end
end