Module: WCC::JsonAPI::PaginationHelper

Defined in:
lib/wcc/jsonapi/concerns/pagination_helper.rb

Instance Method Summary collapse

Instance Method Details

#index_meta(total) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/wcc/jsonapi/concerns/pagination_helper.rb', line 15

def index_meta(total)
  {
    total: total,
    limit: limit,
    skip: skip,
  }
end

#index_serializer_options(total) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/wcc/jsonapi/concerns/pagination_helper.rb', line 42

def index_serializer_options(total)
  {
    is_collection: true,
    meta: index_meta(total),
    links: pagination_links(total),
  }.merge(serializer_options)
end

#limitObject



4
5
6
7
8
9
# File 'lib/wcc/jsonapi/concerns/pagination_helper.rb', line 4

def limit
  @limit ||= [
    params[:limit]&.to_i || 100,
    100,
  ].min
end

Gets the pagination links for the current skip and limit. This method is inherently complex due to the math involved. Better to encapsulate the complexity here.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wcc/jsonapi/concerns/pagination_helper.rb', line 26

def pagination_links(total)
  prev_skip = skip == 0 ? nil : [skip - limit, 0].max
  next_skip = (skip + limit) > total ? nil : skip + limit
  last_skip = (total / limit).floor * limit

  options = permitted_params.merge(only_path: true)

  {
    self: url_for(options.merge(limit: limit, skip: skip)),
    first: url_for(options.merge(limit: limit, skip: 0)),
    prev: prev_skip && url_for(options.merge(limit: limit, skip: prev_skip)),
    next: next_skip && url_for(options.merge(limit: limit, skip: next_skip)),
    last: url_for(options.merge(limit: limit, skip: last_skip)),
  }
end

#skipObject



11
12
13
# File 'lib/wcc/jsonapi/concerns/pagination_helper.rb', line 11

def skip
  @skip ||= params[:skip]&.to_i || 0
end