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

gregschmit / rails-rest-framework / 324

pending completion
324

push

travis-ci-com

gregschmit
Add option to allow all nested attributes by default.

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

805 of 887 relevant lines covered (90.76%)

74.33 hits per line

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

95.0
/lib/rest_framework/paginators.rb
1
class RESTFramework::BasePaginator
1✔
2
  def initialize(data:, controller:, **kwargs)
1✔
3
    @data = data
27✔
4
    @controller = controller
27✔
5
  end
6

7
  # Get the page and return it so the caller can serialize it.
8
  def get_page
1✔
9
    raise NotImplementedError
1✔
10
  end
11

12
  # Wrap the serialized page with appropriate metadata.
13
  def get_paginated_response(serialized_page)
1✔
14
    raise NotImplementedError
1✔
15
  end
16
end
17

18
# A simple paginator based on page numbers.
19
#
20
# Example: http://example.com/api/users/?page=3&page_size=50
21
class RESTFramework::PageNumberPaginator < RESTFramework::BasePaginator
1✔
22
  def initialize(**kwargs)
1✔
23
    super
25✔
24
    # Exclude any `select` clauses since that would cause `count` to fail with a SQL `SyntaxError`.
25
    @count = @data.except(:select).count
25✔
26
    @page_size = self._page_size
25✔
27

28
    @total_pages = @count / @page_size
25✔
29
    @total_pages += 1 if @count % @page_size != 0
25✔
30
  end
31

32
  def _page_size
1✔
33
    page_size = nil
25✔
34

35
    # Get from context, if allowed.
36
    if @controller.class.page_size_query_param
25✔
37
      if page_size = @controller.params[@controller.class.page_size_query_param].presence
25✔
38
        page_size = page_size.to_i
2✔
39
      end
40
    end
41

42
    # Otherwise, get from config.
43
    if !page_size && @controller.class.page_size
25✔
44
      page_size = @controller.class.page_size
23✔
45
    end
46

47
    # Ensure we don't exceed the max page size.
48
    if @controller.class.max_page_size && page_size > @controller.class.max_page_size
25✔
49
      page_size = @controller.class.max_page_size
×
50
    end
51

52
    # Ensure we return at least 1.
53
    return page_size.zero? ? 1 : page_size
25✔
54
  end
55

56
  def _page_query_param
1✔
57
    return @controller.class.page_query_param&.to_sym
25✔
58
  end
59

60
  # Get the page and return it so the caller can serialize it.
61
  def get_page(page_number=nil)
1✔
62
    # If page number isn't provided, infer from the params or use 1 as a fallback value.
63
    unless page_number
25✔
64
      page_number = @controller&.params&.[](self._page_query_param)
25✔
65
      if page_number.blank?
25✔
66
        page_number = 1
24✔
67
      else
68
        page_number = page_number.to_i
1✔
69
        if page_number.zero?
1✔
70
          page_number = 1
×
71
        end
72
      end
73
    end
74
    @page_number = page_number
25✔
75

76
    # Get the data page and return it so the caller can serialize the data in the proper format.
77
    page_index = @page_number - 1
25✔
78
    return @data.limit(@page_size).offset(page_index * @page_size)
25✔
79
  end
80

81
  # Wrap the serialized page with appropriate metadata. TODO: include links.
82
  def get_paginated_response(serialized_page)
1✔
83
    return {
84
      count: @count,
25✔
85
      page: @page_number,
86
      page_size: @page_size,
87
      total_pages: @total_pages,
88
      results: serialized_page,
89
    }
90
  end
91
end
92

93
# TODO: implement this
94
# class RESTFramework::CountOffsetPaginator
95
# end
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

© 2025 Coveralls, Inc