Skip to content
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

[MODEL] Added functionality to provide custom value of total count for pagination #1023

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -106,10 +106,17 @@ def offset(value)
self
end

# Set the "total_count" value
#
def total(value)
@total_count = value.to_i
self
end

# Returns the total number of results
#
def total_count
results.total
@total_count || results.total
end

# Returns the models's `per_page` value or the default
Original file line number Diff line number Diff line change
@@ -55,9 +55,11 @@ def paginate(options)
param_name = options[:param_name] || :page
page = [options[param_name].to_i, 1].max
per_page = (options[:per_page] || __default_per_page).to_i
total = options[:total_entries]

search.definition.update size: per_page,
from: (page - 1) * per_page
@total_entries = total.to_i if total.present?
self
end

@@ -94,7 +96,7 @@ def per_page(num = nil)
# Returns the total number of results
#
def total_entries
results.total
@total_entries || results.total
end

# Returns the models's `per_page` value or the default
Original file line number Diff line number Diff line change
@@ -298,8 +298,20 @@ def self.document_type; 'bar'; end
allow(response.results).to receive(:total).and_return(100)
end

it 'returns the total number of hits' do
expect(response.total_count).to eq(100)
context 'when a custom value is not set' do
it 'returns the total number of hits' do
expect(response.total_count).to eq(100)
end
end

context 'when a value is set via the #total method' do
before do
response.total(50)
end

it 'returns the total number of hits' do
expect(response.total_count).to eq(50)
end
end
end

Original file line number Diff line number Diff line change
@@ -245,8 +245,20 @@ class WillPaginateResponse < Elasticsearch::Model::Response::Response
allow(response).to receive(:results).and_return(double('results', total: 100))
end

it 'returns the total results' do
expect(response.total_entries).to eq(100)
context 'when a custom value is not set' do
it 'returns the total results' do
expect(response.total_entries).to eq(100)
end
end

context 'when a value is set via the #paginate method' do
before do
response.paginate(total_entries: 50)
end

it 'returns the total_entries value' do
expect(response.total_entries).to eq(50)
end
end
end
end