diff --git a/README.md b/README.md index bb33398..d08ea1d 100644 --- a/README.md +++ b/README.md @@ -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": { @@ -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": { diff --git a/lib/restpack_serializer/serializable/paging.rb b/lib/restpack_serializer/serializable/paging.rb index 94f28b8..e73c294 100644 --- a/lib/restpack_serializer/serializable/paging.rb +++ b/lib/restpack_serializer/serializable/paging.rb @@ -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 diff --git a/restpack_serializer.gemspec b/restpack_serializer.gemspec index 6a3601e..41667cb 100644 --- a/restpack_serializer.gemspec +++ b/restpack_serializer.gemspec @@ -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' diff --git a/spec/serializable/paging_spec.rb b/spec/serializable/paging_spec.rb index daf2fae..be4bf90 100644 --- a/spec/serializable/paging_spec.rb +++ b/spec/serializable/paging_spec.rb @@ -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 == { @@ -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