From 41d6128252e48fc8b9c117f6b36df27dce664c08 Mon Sep 17 00:00:00 2001 From: Todd Kummer Date: Tue, 5 May 2020 07:55:50 -0700 Subject: [PATCH] Add total_count; fix rounding on total_pages --- lib/next_page/pagination_attributes.rb | 6 +++++- spec/next_page/pagination_attributes_spec.rb | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/next_page/pagination_attributes.rb b/lib/next_page/pagination_attributes.rb index baebd32..1276803 100644 --- a/lib/next_page/pagination_attributes.rb +++ b/lib/next_page/pagination_attributes.rb @@ -14,8 +14,12 @@ def next_page current_page + 1 end + def total_count + @total_count ||= unscope(:limit).unscope(:offset).count + end + def total_pages - @total_pages ||= unscope(:limit).unscope(:offset).count / per_page + total_count.fdiv(per_page).ceil end def per_page diff --git a/spec/next_page/pagination_attributes_spec.rb b/spec/next_page/pagination_attributes_spec.rb index 9c74a2f..aea3894 100644 --- a/spec/next_page/pagination_attributes_spec.rb +++ b/spec/next_page/pagination_attributes_spec.rb @@ -6,7 +6,11 @@ let(:instance) { relation.tap { |cp| cp.extend(described_class) } } context 'with limit 5' do - let(:relation) { Jersey.limit(5) } + let(:relation) { Jersey.limit(6) } + + it '#total_count' do + expect(instance.total_count).to eq 20 + end it '#total_pages' do expect(instance.total_pages).to eq 4