Skip to content
This repository has been archived by the owner on Oct 27, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from barkbox/pagination-links
Browse files Browse the repository at this point in the history
Pagination links in ams
  • Loading branch information
becsegal authored Apr 6, 2017
2 parents 579067f + f8302f9 commit 52bede2
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 68 deletions.
144 changes: 81 additions & 63 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,104 +1,122 @@
PATH
remote: .
specs:
paging_cursor (0.0.4)
paging_cursor (0.2.0)

GEM
remote: https://rubygems.org/
specs:
actionmailer (4.0.13)
actionpack (= 4.0.13)
actionmailer (4.1.16)
actionpack (= 4.1.16)
actionview (= 4.1.16)
mail (~> 2.5, >= 2.5.4)
actionpack (4.0.13)
activesupport (= 4.0.13)
builder (~> 3.1.0)
erubis (~> 2.7.0)
actionpack (4.1.16)
actionview (= 4.1.16)
activesupport (= 4.1.16)
rack (~> 1.5.2)
rack-test (~> 0.6.2)
activemodel (4.0.13)
activesupport (= 4.0.13)
builder (~> 3.1.0)
activerecord (4.0.13)
activemodel (= 4.0.13)
activerecord-deprecated_finders (~> 1.0.2)
activesupport (= 4.0.13)
arel (~> 4.0.0)
activerecord-deprecated_finders (1.0.4)
activesupport (4.0.13)
actionview (4.1.16)
activesupport (= 4.1.16)
builder (~> 3.1)
erubis (~> 2.7.0)
active_model_serializers (0.10.4)
actionpack (>= 4.1, < 6)
activemodel (>= 4.1, < 6)
case_transform (>= 0.2)
jsonapi (= 0.1.1.beta6)
activemodel (4.1.16)
activesupport (= 4.1.16)
builder (~> 3.1)
activerecord (4.1.16)
activemodel (= 4.1.16)
activesupport (= 4.1.16)
arel (~> 5.0.0)
activesupport (4.1.16)
i18n (~> 0.6, >= 0.6.9)
minitest (~> 4.2)
multi_json (~> 1.3)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
arel (4.0.2)
builder (3.1.4)
concurrent-ruby (1.0.1)
diff-lcs (1.2.5)
tzinfo (~> 1.1)
arel (5.0.1.20140414130214)
builder (3.2.3)
case_transform (0.2)
activesupport
concurrent-ruby (1.0.5)
diff-lcs (1.3)
erubis (2.7.0)
i18n (0.7.0)
i18n (0.8.1)
json (1.8.6)
jsonapi (0.1.1.beta6)
jsonapi-parser (= 0.1.1.beta3)
jsonapi-renderer (= 0.1.1.beta1)
jsonapi-parser (0.1.1.beta3)
jsonapi-renderer (0.1.1.beta1)
mail (2.6.4)
mime-types (>= 1.16, < 4)
mime-types (3.0)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0221)
minitest (4.7.5)
multi_json (1.11.3)
mime-types-data (3.2016.0521)
minitest (5.10.1)
rack (1.5.5)
rack-test (0.6.3)
rack (>= 1.0)
rails (4.0.13)
actionmailer (= 4.0.13)
actionpack (= 4.0.13)
activerecord (= 4.0.13)
activesupport (= 4.0.13)
rails (4.1.16)
actionmailer (= 4.1.16)
actionpack (= 4.1.16)
actionview (= 4.1.16)
activemodel (= 4.1.16)
activerecord (= 4.1.16)
activesupport (= 4.1.16)
bundler (>= 1.3.0, < 2.0)
railties (= 4.0.13)
railties (= 4.1.16)
sprockets-rails (~> 2.0)
railties (4.0.13)
actionpack (= 4.0.13)
activesupport (= 4.0.13)
railties (4.1.16)
actionpack (= 4.1.16)
activesupport (= 4.1.16)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (11.1.2)
rspec (3.4.0)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-core (3.4.4)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
rake (12.0.0)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-mocks (3.4.1)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-rails (3.4.2)
actionpack (>= 3.0, < 4.3)
activesupport (>= 3.0, < 4.3)
railties (>= 3.0, < 4.3)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-support (~> 3.4.0)
rspec-support (3.4.1)
sprockets (3.6.0)
rspec-support (~> 3.5.0)
rspec-rails (3.5.2)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
sprockets (3.7.1)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (2.3.3)
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (>= 2.8, < 4.0)
sqlite3 (1.3.11)
thor (0.19.1)
thread_safe (0.3.5)
tzinfo (0.3.49)
thor (0.19.4)
thread_safe (0.3.6)
tzinfo (1.2.3)
thread_safe (~> 0.1)

PLATFORMS
ruby

DEPENDENCIES
active_model_serializers (= 0.10.4)
paging_cursor!
rails (~> 4.0.0)
rails (~> 4.1.0)
rspec
rspec-rails
sqlite3
Expand Down
2 changes: 1 addition & 1 deletion lib/paging_cursor.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module PagingCursor
end


require 'paging_cursor/config'
require 'paging_cursor/direction'
require 'paging_cursor/active_record'
require 'paging_cursor/active_model_serializer'
49 changes: 49 additions & 0 deletions lib/paging_cursor/active_model_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'active_model_serializers'

# See https://github.com/rails-api/active_model_serializers/blob/a032201a91cbca407211bca0392ba881eef1f7ba/lib/active_model/serializer/collection_serializer.rb
# for original version
module ActiveModel
class Serializer
class CollectionSerializer

def paginated?
ActiveModelSerializers.config.jsonapi_pagination_links_enabled &&
object.respond_to?(:cursor_before) &&
object.respond_to?(:cursor_after)
end
end
end
end

# See https://github.com/rails-api/active_model_serializers/blob/a032201a91cbca407211bca0392ba881eef1f7ba/lib/active_model_serializers/adapter/json_api/pagination_links.rb
# for the original version
module ActiveModelSerializers
module Adapter
class JsonApi < Base
class PaginationLinks

def as_json
pages_from.each_with_object({}) do |(key, value), hash|
params = query_parameters.merge(cursor: value)
params = Rack::Utils.build_nested_query(params)
hash[key] = "#{url(adapter_options)}?#{params}"
end
end

private

def pages_from
{}.tap do |pages|
return {} if collection.empty?

pages[:self] = { before: (collection.cursor_after + 1).to_s }
pages[:first] = { before: nil }
pages[:prev] = { after: collection.cursor_after.to_s }
pages[:next] = { before: collection.cursor_before.to_s }
pages[:last] = { after: nil }
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/paging_cursor/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def to_a
# # support pagination on associations and scopes
klasses.each do |klass|
klass.send(:prepend, SortedResults)
klass.send(:include, FinderMethods)
klass.send(:include, FinderMethods)
klass.send(:include, ::PagingCursor::Direction)
end
end
Expand Down
Binary file removed paging_cursor-0.1.0.gem
Binary file not shown.
Binary file added paging_cursor-0.2.0.gem
Binary file not shown.
5 changes: 3 additions & 2 deletions paging_cursor.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'paging_cursor'
s.version = '0.1.0'
s.version = '0.2.0'
s.date = '2016-04-23'
s.summary = "ActiveRecord and ActiveController extensions for cursor pagination"
s.description = "ActiveRecord and ActiveController extensions for cursor pagination"
Expand All @@ -12,7 +12,8 @@ Gem::Specification.new do |s|
s.files = Dir['{lib,spec}/**/*', 'README*', 'LICENSE*']

s.add_development_dependency "rspec"
s.add_development_dependency "rails", "~> 4.0.0"
s.add_development_dependency "rails", "~> 4.1.0"
s.add_development_dependency "sqlite3"
s.add_development_dependency "rspec-rails"
s.add_development_dependency "active_model_serializers", "0.10.4"
end
65 changes: 65 additions & 0 deletions spec/active_model_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'rails_helper'
include ActiveModelSerializers::Adapter

describe JsonApi::PaginationLinks do

before do
10.times do
User.create
end
@per_page = 5
end

after do
User.delete_all
end

shared_examples_for 'it returns the correct links' do
specify do
serialized_object = User.cursor(before: cursor, limit: @per_page)
options = {
serialization_context: OpenStruct.new(
request_url: 'www.example.com',
query_parameters: {'user_id' => '7'} # arbitrary string
)
}

expected_links = {
first: 'www.example.com?user_id=7&cursor[before]',
last: 'www.example.com?user_id=7&cursor[after]',
next: "www.example.com?user_id=7&cursor[before]=#{cursor - @per_page}",
prev: "www.example.com?user_id=7&cursor[after]=#{cursor - 1}",
self: "www.example.com?user_id=7&cursor[before]=#{cursor}"
}

response_links = JsonApi::PaginationLinks.new(serialized_object, options).as_json
expect(response_links).to eq(expected_links)
end
end

context 'cursor is the max id of serialized collection' do
it_behaves_like 'it returns the correct links' do
let(:cursor) { 10 }
end
end

context 'cursor is less than the max id of serialized collection' do
it_behaves_like 'it returns the correct links' do
let(:cursor) { 18 }
end
end

context 'empty serialized object' do
it 'returns an empty links hash' do
serialized_object = User.cursor(before: 1)
options = {
serialization_context: OpenStruct.new(
request_url: 'www.example.com',
query_parameters: {'user_id' => '7'}
)
}
response_links = JsonApi::PaginationLinks.new(serialized_object, options).as_json
expect(response_links).to eq({})
end
end
end
2 changes: 1 addition & 1 deletion spec/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
expect(Post.cursor(limit: 2).size).to eq(2)
end

it 'accepts string and symbol eys' do
it 'accepts string and symbol keys' do
expect(Post.cursor('limit' => 2)).to eq(Post.cursor(limit: 2))
end

Expand Down

0 comments on commit 52bede2

Please sign in to comment.