-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support defining paginator on resource #92
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,16 +62,21 @@ def record_count_for(records, options) | |
# | ||
# @api private | ||
def paginator | ||
@paginator ||= paginator_klass.new(page_params) | ||
@paginator ||= begin | ||
paginator_klass = "#{resource_paginator_name}_paginator".classify.constantize | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed this from being its own method to living inside |
||
paginator_klass.new(page_params) | ||
end | ||
end | ||
|
||
# Return the paginator class to be used in the response's pagination. | ||
# Return the name of the resource's paginator. | ||
# Points to default paginator unless paginator explicitly set on resource. | ||
# | ||
# @return [Paginator] | ||
# @return [Symbol] | ||
# e.g.: :paged or :offset | ||
# | ||
# @api private | ||
def paginator_klass | ||
"#{JSONAPI.configuration.default_paginator}_paginator".classify.constantize | ||
def resource_paginator_name | ||
@resource_paginator_name ||= @request.resource_klass._paginator | ||
end | ||
|
||
# Check whether pagination should be applied to the response. | ||
|
@@ -80,8 +85,7 @@ def paginator_klass | |
# | ||
# @api private | ||
def apply_pagination?(options) | ||
JSONAPI.configuration.default_paginator != :none && | ||
(options[:paginate].nil? || options[:paginate]) | ||
resource_paginator_name != :none && options[:paginate] != false | ||
end | ||
|
||
# Creates an instance of ActionController::Parameters for page params. | ||
|
@@ -121,7 +125,7 @@ def paginate_with(kind) | |
# | ||
# @api private | ||
def pagination_range | ||
case JSONAPI.configuration.default_paginator | ||
case resource_paginator_name | ||
when :paged | ||
number = page_params['number'].to_i.nonzero? || 1 | ||
size = page_params['size'].to_i.nonzero? || JSONAPI.configuration.default_page_size | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,7 +101,7 @@ | |
context 'with "page"' do | ||
context 'when using "paged" paginator' do | ||
before(:all) do | ||
JSONAPI.configuration.default_paginator = :paged | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to change the resource paginator instead of the default config because of the way So, in the course of running the "paged" tests, Basically, this change is solving an issue with the tests themselves, not an issue being raised by the code. |
||
UserResource.paginator :paged | ||
end | ||
|
||
context 'at the first page' do | ||
|
@@ -183,7 +183,7 @@ | |
|
||
context 'when using "offset" paginator' do | ||
before(:all) do | ||
JSONAPI.configuration.default_paginator = :offset | ||
UserResource.paginator :offset | ||
end | ||
|
||
context 'at the first page' do | ||
|
@@ -246,9 +246,9 @@ | |
end | ||
end | ||
|
||
context 'when using custom global paginator' do | ||
context 'when using custom paginator' do | ||
before(:all) do | ||
JSONAPI.configuration.default_paginator = :custom_offset | ||
UserResource.paginator :custom_offset | ||
end | ||
|
||
context 'at the first page' do | ||
|
@@ -310,6 +310,24 @@ | |
end | ||
end | ||
end | ||
|
||
context 'without pagination' do | ||
before(:all) do | ||
UserResource.paginator :none | ||
end | ||
|
||
it 'returns all results without pagination links' do | ||
get :index, params: { page: { offset: 0, limit: 2 } } | ||
|
||
expect(response).to have_http_status :ok | ||
expect(response).to have_primary_data('users') | ||
expect(data.size).to eq(User.count) | ||
expect(response).to have_meta_record_count(User.count) | ||
|
||
expect(json.dig('meta', 'page_count')).to eq(1) | ||
expect(json.dig('links')).not_to be_present | ||
end | ||
end | ||
end | ||
|
||
context 'with "sort"' do | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,7 @@ | |
end | ||
end | ||
|
||
describe '#count_pages_for' do | ||
describe '#page_count_for' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed this to match the corresponding method name |
||
shared_examples_for 'counting pages' do | ||
it 'returns the correct page count' do | ||
allow(subject).to receive(:page_params).and_return(page_params) | ||
|
@@ -164,4 +164,39 @@ | |
expect(subject.send(:distinct_count_sql, records)).to eq('DISTINCT foos.id') | ||
end | ||
end | ||
|
||
describe '#apply_pagination?' do | ||
shared_examples_for 'checking pagination' do | ||
it 'returns whether to apply pagination' do | ||
allow(subject).to receive(:resource_paginator_name).and_return(paginator) | ||
expect(subject.send(:apply_pagination?, options)).to eq(apply) | ||
end | ||
end | ||
|
||
context 'with paged paginator' do | ||
let(:paginator) { :paged } | ||
let(:apply) { true } | ||
it_behaves_like 'checking pagination' | ||
end | ||
|
||
context 'without paginator' do | ||
let(:paginator) { :none } | ||
let(:apply) { false } | ||
it_behaves_like 'checking pagination' | ||
end | ||
|
||
context 'when options[:paginate] is false' do | ||
let(:paginator) { :paged } | ||
let(:options) { { paginate: false } } | ||
let(:apply) { false } | ||
it_behaves_like 'checking pagination' | ||
end | ||
|
||
context 'when options[:paginate] is nil' do | ||
let(:paginator) { :paged } | ||
let(:options) { { paginate: nil } } | ||
let(:apply) { true } | ||
it_behaves_like 'checking pagination' | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
JSONAPI.configuration.top_level_meta_include_page_count
is true butapply_pagination?
is false, then page count should always equal 1