summaryrefslogtreecommitdiff
path: root/app/serializers/pipeline_serializer.rb
blob: cfa86cc2553f228f816f14e66d3515ee23796d08 (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
31
32
33
34
35
36
37
38
39
40
class PipelineSerializer < BaseSerializer
  entity PipelineEntity
  class InvalidResourceError < StandardError; end
  include API::Helpers::Pagination
  Struct.new('Pagination', :request, :response)

  def represent(resource, opts = {})
    if paginated?
      raise InvalidResourceError unless resource.respond_to?(:page)

      super(paginate(resource.includes(project: :namespace)), opts)
    else
      super(resource, opts)
    end
  end

  def paginated?
    defined?(@pagination)
  end

  def with_pagination(request, response)
    tap { @pagination = Struct::Pagination.new(request, response) }
  end

  private

  # Methods needed by `API::Helpers::Pagination`
  #
  def params
    @pagination.request.query_parameters
  end

  def request
    @pagination.request
  end

  def header(header, value)
    @pagination.response.headers[header] = value
  end
end