class Quiver::Action::PaginationLinkBuilder
Attributes
limit[RW]
offset[RW]
request_path_with_query[RW]
total_count[RW]
Public Class Methods
new(request_path_with_query, offset, limit, total_count)
click to toggle source
# File lib/quiver/action/pagination_link_builder.rb, line 4 def initialize(request_path_with_query, offset, limit, total_count) self.request_path_with_query = request_path_with_query self.offset = offset self.limit = limit self.total_count = total_count end
Public Instance Methods
pagination_links()
click to toggle source
# File lib/quiver/action/pagination_link_builder.rb, line 11 def pagination_links links = {} links[:self] = request_path_with_query links[:last] = build_link(last_page) links[:next] = build_link(next_page) if next_page links[:prev] = build_link(previous_page) if previous_page links end
Private Instance Methods
build_link(offset)
click to toggle source
# File lib/quiver/action/pagination_link_builder.rb, line 26 def build_link(offset) uri = parsed_uri query_params = Rack::Utils.parse_nested_query(uri.query) query_params['page'] ||= {} query_params['page']['limit'] = limit query_params['page']['offset'] = offset uri.query = Rack::Utils.build_nested_query(query_params) uri.to_s end
last_page()
click to toggle source
# File lib/quiver/action/pagination_link_builder.rb, line 56 def last_page if limit == -1 0 else # rounds total_count down to the offset that # represents the last page of the resources (total_count / limit) * limit end end
next_page()
click to toggle source
# File lib/quiver/action/pagination_link_builder.rb, line 40 def next_page if limit != -1 && total_count > offset + limit offset + limit end end
parsed_uri()
click to toggle source
# File lib/quiver/action/pagination_link_builder.rb, line 36 def parsed_uri URI.parse(request_path_with_query) end
previous_page()
click to toggle source
# File lib/quiver/action/pagination_link_builder.rb, line 46 def previous_page if offset > 0 if limit == -1 0 else [offset - limit, 0].max end end end