2018-12-19 13:15:58 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
|
|
|
module Helpers
|
|
|
|
##
|
|
|
|
# This module makes it possible to use `app/presenters` with
|
2020-03-24 12:09:42 +00:00
|
|
|
# Grape Entities. It instantiates the model presenter and passes
|
2018-12-19 13:15:58 +00:00
|
|
|
# options defined in the API endpoint to the presenter itself.
|
|
|
|
#
|
|
|
|
# present object, with: Entities::Something,
|
|
|
|
# current_user: current_user,
|
|
|
|
# another_option: 'my options'
|
|
|
|
#
|
|
|
|
# Example above will make `current_user` and `another_option`
|
|
|
|
# values available in the subclass of `Gitlab::View::Presenter`
|
|
|
|
# thorough a separate method in the presenter.
|
|
|
|
#
|
|
|
|
# The model class needs to have `::Presentable` module mixed in
|
|
|
|
# if you want to use `API::Helpers::Presentable`.
|
|
|
|
#
|
|
|
|
module Presentable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
def initialize(object, options = {})
|
2020-03-24 12:09:42 +00:00
|
|
|
options = options.opts_hash if options.is_a?(Grape::Entity::Options)
|
2020-09-30 21:10:09 +00:00
|
|
|
super(object.present(options), **options)
|
2018-12-19 13:15:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|