• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gregschmit / rails-rest-framework / 24615084524

18 Apr 2026 10:14PM UTC coverage: 87.679% (-0.5%) from 88.164%
24615084524

push

github

gregschmit
Fix rubocop and tests.

2 of 2 new or added lines in 1 file covered. (100.0%)

74 existing lines in 7 files now uncovered.

1103 of 1258 relevant lines covered (87.68%)

205.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.37
/lib/rest_framework/paginators/page_number_paginator.rb
1
# A simple paginator based on page numbers.
2
#
3
# Example: http://example.com/api/users/?page=3&page_size=50
4
class RESTFramework::Paginators::PageNumberPaginator < RESTFramework::Paginators::BasePaginator
2✔
5
  def initialize(**kwargs)
2✔
6
    super
38✔
7
    # Exclude any `select` clauses since that would cause `count` to fail with a SQL `SyntaxError`.
8
    @count = @data.except(:select).count
38✔
9
    @page_size = self._page_size
38✔
10

11
    @total_pages = @count / @page_size
38✔
12
    @total_pages += 1 if @count % @page_size != 0
38✔
13
  end
14

15
  def _page_size
2✔
16
    page_size = nil
38✔
17

18
    # Get from query param, if allowed.
19
    if param = @controller.class.page_size_query_param
38✔
20
      if raw = @controller.params[param].presence
38✔
21
        parsed = raw.to_i
4✔
22
        page_size = parsed if parsed > 0
4✔
23
      end
24
    end
25

26
    # Fall back to the configured page size.
27
    page_size ||= @controller.class.page_size&.to_i || 1
38✔
28

29
    # Ensure we don't exceed the max page size.
30
    max_page_size = @controller.class.max_page_size
38✔
31
    if max_page_size && page_size > max_page_size
38✔
UNCOV
32
      page_size = max_page_size
×
33
    end
34

35
    # Ensure we return at least 1.
36
    [ page_size, 1 ].max
38✔
37
  end
38

39
  # Get the page and return it so the caller can serialize it.
40
  def get_page(page_number = nil)
2✔
41
    # If page number isn't provided, infer from the params or use 1 as a fallback value.
42
    unless page_number
38✔
43
      page_number = @controller&.params&.[](@controller.class.page_query_param&.to_sym)
38✔
44
      if page_number.blank?
38✔
45
        page_number = 1
34✔
46
      else
47
        page_number = page_number.to_i
4✔
48
        if page_number < 1
4✔
49
          page_number = 1
2✔
50
        end
51
      end
52
    end
53
    @page_number = page_number
38✔
54

55
    # Get the data page and return it so the caller can serialize the data in the proper format.
56
    page_index = @page_number - 1
38✔
57
    @data.limit(@page_size).offset(page_index * @page_size)
38✔
58
  end
59

60
  # Wrap the serialized page with appropriate metadata.
61
  def get_paginated_response(serialized_page)
2✔
62
    page_query_param = @controller.class.page_query_param
38✔
63
    base_params = @controller.request.query_parameters.symbolize_keys
38✔
64
    next_url = if @page_number < @total_pages
38✔
65
      @controller.url_for({ **base_params, page_query_param => @page_number + 1 })
34✔
66
    end
67
    previous_url = if @page_number > 1
38✔
68
      @controller.url_for({ **base_params, page_query_param => @page_number - 1 })
2✔
69
    end
70

71
    {
72
      count: @count,
38✔
73
      page: @page_number,
74
      page_size: @page_size,
75
      total_pages: @total_pages,
76
      next: next_url,
77
      previous: previous_url,
78
      results: serialized_page,
79
    }.compact
80
  end
81
end
82

83
# Alias for convenience.
84
RESTFramework::PageNumberPaginator = RESTFramework::Paginators::PageNumberPaginator
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc