summaryrefslogtreecommitdiff
path: root/lib/api/helpers/presentable.rb
blob: 40e1b266df5c05d2a9a1239e34e1c9a5f6b22560 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true

module API
  module Helpers
    ##
    # This module makes it possible to use `app/presenters` with
    # Grape Entities. It instantiates the model presenter and passes
    # 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 = {})
        options = options.opts_hash if options.is_a?(Grape::Entity::Options)
        super(object.present(**options), options)
      end
    end
  end
end