From 15de5eea923678f534d78ca562da200539f0e670 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Wed, 12 Aug 2020 00:51:15 +0800 Subject: [PATCH] chore: remove method that call count(*) --- .../fast_jsonapi/pagination_meta_generator.rb | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/app/lib/fast_jsonapi/pagination_meta_generator.rb b/app/lib/fast_jsonapi/pagination_meta_generator.rb index 4a1c3a17b..a534e187d 100644 --- a/app/lib/fast_jsonapi/pagination_meta_generator.rb +++ b/app/lib/fast_jsonapi/pagination_meta_generator.rb @@ -27,8 +27,8 @@ def call hash[:links][:self] = generate_url(page) if page < total_pages - hash[:links][:next] = generate_url(records.next_page) - hash[:links][:last] = generate_url(records.total_pages) + hash[:links][:next] = generate_url(next_page) + hash[:links][:last] = generate_url(total_pages) end hash @@ -39,6 +39,26 @@ def call attr_reader :page, :page_size, :records, :records_counter, :total_count attr_accessor :url, :hash + def current_page + records.current_page + end + + def last_page? + current_page == total_pages + end + + def next_page + current_page + 1 unless last_page? || out_of_range? + end + + def last_page? + current_page == total_pages + end + + def out_of_range? + current_page > total_pages + end + def generate_url(page) [url, url_params(page)].join("?") end