Skip to content
This repository was archived by the owner on Jun 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2 from G5/fix_total_page_computation
Browse files Browse the repository at this point in the history
Fix total pages computation
  • Loading branch information
ramontayag committed Aug 3, 2015
2 parents 283b54f + b609b44 commit dea93bc
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.1.1

- Fix bug where page count was incorrect

# 0.1.0

- Change `total` key to `total_items`
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model/pagination_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def initialize_with_pagination(object, opts={})
offset = object.offset_value
current_page = offset ? (offset / per_page) + 1 : 1
total_items = object.limit(nil).offset(nil).count
total_pages = per_page ? (total_items / per_page).ceil : 1
total_pages = per_page ? (total_items / per_page.to_f).ceil : 1
prev_page = current_page - 1
prev_page = nil if prev_page < 1
next_page = current_page + 1
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model_serializers_contrib/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ActiveModelSerializersContrib
VERSION = "0.1.0"
VERSION = "0.1.1"
end
4 changes: 2 additions & 2 deletions spec/requests/pagination_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
end

it "contains pagination information" do
get "/books", page: 6, per_page: 2, format: :json
get "/books", page: 6, per_page: 3, format: :json
hash = JSON.parse(response.body).with_indifferent_access
meta = hash[:meta]
expect(meta[:current_page]).to eq 6
expect(meta[:total_pages]).to eq 10
expect(meta[:total_pages]).to eq 7
expect(meta[:total_items]).to eq 20
expect(meta[:prev_page]).to eq 5
expect(meta[:next_page]).to eq 7
Expand Down

0 comments on commit dea93bc

Please sign in to comment.