diff options
Diffstat (limited to 'lib/api/paging/index_paginator.rb')
-rw-r--r-- | lib/api/paging/index_paginator.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/api/paging/index_paginator.rb b/lib/api/paging/index_paginator.rb new file mode 100644 index 000000000..8dc5108e0 --- /dev/null +++ b/lib/api/paging/index_paginator.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +module Api + module Paging + class IndexPaginator + def initialize(query_base, current_page, limit) + @query_base = query_base + @current_page = current_page.to_i + @limit = limit.to_i + end + + def page_data + @page_data ||= @query_base.paginate(page: @current_page, per_page: @limit) + @max_page = (@query_base.count * 1.0 / @limit * 1.0).ceil + @max_page = 1 if @max_page < 1 + @page_data + end + + def next_page(for_url=true) + page_data + return nil if for_url && @current_page == @max_page + + return "page=#{@current_page + 1}" if for_url + + IndexPaginator.new(@query_base, @current_page + 1, @limit) + end + + def previous_page(for_url=true) + page_data + return nil if for_url && @current_page == 1 + + return "page=#{@current_page - 1}" if for_url + + IndexPaginator.new(@query_base, @current_page - 1, @limit) + end + + def filter_parameters(parameters) + parameters.delete(:page) + end + end + end +end |