Commit c1ed642f authored by James Lopez's avatar James Lopez

added spec and pagination class to emulate paginary for using with Redis returned arrays.

parent f1ddbea6
module Gitlab
class PaginationDelegate
DEFAULT_PER_PAGE = Kaminari.config.default_per_page
MAX_PER_PAGE = Kaminari.config.max_per_page
def initialize(page:, per_page:, count:, options: {})
@count = count
@options = { default_per_page: DEFAULT_PER_PAGE,
max_per_page: MAX_PER_PAGE }.merge(options)
@page = sanitize_page(page)
@per_page = sanitize_per_page(per_page)
end
def total_count
@count
end
def total_pages
(total_count.to_f / @per_page).ceil
end
def next_page
current_page + 1 unless last_page?
end
def prev_page
return nil if first_page?
current_page - 1 unless first_page?
end
def current_page
@page
end
def limit_value
@per_page
end
def first_page?
current_page == 1
end
def last_page?
current_page >= total_pages
end
def offset
(current_page - 1) * limit_value
end
private
def sanitize_per_page(per_page)
return @options[:default_per_page] unless per_page && per_page > 0
per_page > @options[:max_per_page] ? @options[:max_per_page] : per_page
end
def sanitize_page(page)
return 1 unless page && page > 1
page > total_count ? total_count : page
end
end
end
require 'spec_helper'
describe Gitlab::PaginationDelegate, lib: true do
context 'no data' do
let(:delegate) { described_class.new(page: 1,
per_page: 10,
count: 0) }
it 'shows the correct total count' do
expect(delegate.total_count).to eq(0)
end
it 'shows the correct total pages' do
expect(delegate.total_pages).to eq(0)
end
it 'shows the correct next page' do
expect(delegate.next_page).to be_nil
end
it 'shows the correct previous page' do
expect(delegate.prev_page).to be_nil
end
it 'shows the correct current page' do
expect(delegate.current_page).to eq(1)
end
it 'shows the correct limit value' do
expect(delegate.limit_value).to eq(10)
end
it 'shows the correct first page' do
expect(delegate.first_page?).to be true
end
it 'shows the correct last page' do
expect(delegate.last_page?).to be true
end
it 'shows the correct offset' do
expect(delegate.offset).to eq(0)
end
end
context 'with data' do
let(:delegate) { described_class.new(page: 5,
per_page: 100,
count: 1000) }
it 'shows the correct total count' do
expect(delegate.total_count).to eq(1000)
end
it 'shows the correct total pages' do
expect(delegate.total_pages).to eq(10)
end
it 'shows the correct next page' do
expect(delegate.next_page).to eq(6)
end
it 'shows the correct previous page' do
expect(delegate.prev_page).to eq(4)
end
it 'shows the correct current page' do
expect(delegate.current_page).to eq(5)
end
it 'shows the correct limit value' do
expect(delegate.limit_value).to eq(100)
end
it 'shows the correct first page' do
expect(delegate.first_page?).to be false
end
it 'shows the correct last page' do
expect(delegate.last_page?).to be false
end
it 'shows the correct offset' do
expect(delegate.offset).to eq(400)
end
end
context 'last page' do
let(:delegate) { described_class.new(page: 10,
per_page: 100,
count: 1000) }
it 'shows the correct total count' do
expect(delegate.total_count).to eq(1000)
end
it 'shows the correct total pages' do
expect(delegate.total_pages).to eq(10)
end
it 'shows the correct next page' do
expect(delegate.next_page).to be_nil
end
it 'shows the correct previous page' do
expect(delegate.prev_page).to eq(9)
end
it 'shows the correct current page' do
expect(delegate.current_page).to eq(10)
end
it 'shows the correct limit value' do
expect(delegate.limit_value).to eq(100)
end
it 'shows the correct first page' do
expect(delegate.first_page?).to be false
end
it 'shows the correct last page' do
expect(delegate.last_page?).to be true
end
it 'shows the correct offset' do
expect(delegate.offset).to eq(900)
end
end
context 'limits and defaults' do
it 'has a maximum limit per page' do
expect(described_class.new(page: nil,
per_page: 1000,
count: 0).limit_value).to eq(described_class::MAX_PER_PAGE)
end
it 'has a default per page' do
expect(described_class.new(page: nil,
per_page: nil,
count: 0).limit_value).to eq(described_class::DEFAULT_PER_PAGE)
end
it 'has a maximum page' do
expect(described_class.new(page: 100,
per_page: 10,
count: 1).current_page).to eq(1)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment