Skip to content

Commit

Permalink
Add total_count; fix rounding on total_pages
Browse files Browse the repository at this point in the history
  • Loading branch information
toddkummer committed May 5, 2020
1 parent 38134c0 commit 41d6128
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/next_page/pagination_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion spec/next_page/pagination_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 41d6128

Please sign in to comment.