Skip to content

Commit

Permalink
Merge pull request RestPack#89 from cover/paging
Browse files Browse the repository at this point in the history
Improve paging
  • Loading branch information
GavinJoyce committed Aug 16, 2014
2 parents 1b6983d + 744b4fc commit 699714f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,10 @@ http://restpack-serializer-sample.herokuapp.com/api/v1/songs.json?page=2&page_si
"page_count": 14,
"previous_page": 1,
"next_page": 3,
"previous_href": "/api/v1/songs?page_size=3",
"next_href": "/api/v1/songs?page=3&page_size=3"
"first_href": "/songs?page_size=3",
"previous_href": "/songs?page_size=3",
"next_href": "/songs?page=3&page_size=3",
"last_href": "/songs?page=14&page_size=3"
}
},
"links": {
Expand Down Expand Up @@ -266,8 +268,10 @@ which yields:
"page_count": 1,
"previous_page": null,
"next_page": null,
"first_href": '/albums',
"previous_href": null,
"next_href": null
"next_href": null,
"last_href": '/albums'
}
},
"links": {
Expand Down
17 changes: 9 additions & 8 deletions lib/restpack_serializer/serializable/paging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,19 @@ def serialize_page(page, options)

def serialize_meta(page, options)
meta = {
page: options.page,
page_size: options.page_size,
count: page.total_count,
include: options.include
page: page.current_page,
page_size: page.limit_value,
count: page.total_count,
include: options.include,
page_count: page.total_pages,
previous_page: page.prev_page,
next_page: page.next_page
}

meta[:page_count] = ((page.total_count - 1) / options.page_size) + 1
meta[:previous_page] = meta[:page] > 1 ? meta[:page] - 1 : nil
meta[:next_page] = meta[:page] < meta[:page_count] ? meta[:page] + 1 : nil

meta[:first_href] = page_href(1, options)
meta[:previous_href] = page_href(meta[:previous_page], options)
meta[:next_href] = page_href(meta[:next_page], options)
meta[:last_href] = page_href(meta[:page_count], options)
meta
end

Expand Down
2 changes: 1 addition & 1 deletion restpack_serializer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |gem|

gem.add_dependency 'activesupport', ['>= 4.0.3', '< 5.0']
gem.add_dependency 'activerecord', ['>= 4.0.3', '< 5.0']
gem.add_dependency 'kaminari', '~> 0.15.1'
gem.add_dependency 'kaminari', '~> 0.16.1'

gem.add_development_dependency 'restpack_gem', '~> 0.0.9'
gem.add_development_dependency 'rake', '~> 10.0.3'
Expand Down
3 changes: 3 additions & 0 deletions spec/serializable/paging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
it "includes valid paging meta data" do
page[:meta][:songs][:count].should == 18
page[:meta][:songs][:page_count].should == 2
page[:meta][:songs][:first_href].should == '/songs'
page[:meta][:songs][:previous_page].should == nil
page[:meta][:songs][:previous_href].should == nil
page[:meta][:songs][:next_page].should == 2
page[:meta][:songs][:next_href].should == '/songs?page=2'
page[:meta][:songs][:last_href].should == '/songs?page=2'
end
it "includes links" do
page[:links].should == {
Expand All @@ -44,6 +46,7 @@
it "includes the custom page size in the page hrefs" do
page[:meta][:songs][:next_page].should == 2
page[:meta][:songs][:next_href].should == '/songs?page=2&page_size=3'
page[:meta][:songs][:last_href].should == '/songs?page=6&page_size=3'
end
end

Expand Down

0 comments on commit 699714f

Please sign in to comment.