From 54580dcc41a3e4b9f3cb64168c73caedc07bb773 Mon Sep 17 00:00:00 2001 From: magnusfiorepalm Date: Tue, 9 May 2017 21:20:59 -0700 Subject: [PATCH] Adding will_paginate support for total_entries being passed in as an option * In order to support a total_entries size different from the size of the collection we will need to pass the options all the way down to the WillPaginate::Collection#paginate method. --- lib/api-pagination.rb | 2 +- spec/api-pagination_spec.rb | 78 ++++++++++++++++++++++++------------- 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/lib/api-pagination.rb b/lib/api-pagination.rb index 39cae5e..e413300 100644 --- a/lib/api-pagination.rb +++ b/lib/api-pagination.rb @@ -60,7 +60,7 @@ def paginate_with_will_paginate(collection, options) if defined?(Sequel::Dataset) && collection.kind_of?(Sequel::Dataset) collection.paginate(options[:page], options[:per_page]) else - collection.paginate(:page => options[:page], :per_page => options[:per_page]) + collection.paginate(options) end end diff --git a/spec/api-pagination_spec.rb b/spec/api-pagination_spec.rb index 6d151e9..b424a5e 100644 --- a/spec/api-pagination_spec.rb +++ b/spec/api-pagination_spec.rb @@ -1,39 +1,65 @@ require 'spec_helper' describe ApiPagination do - let(:collection) { (1..100).to_a } - let(:paginate_array_options) { { total_count: 1000 } } + let(:collection) {(1..100).to_a} + let(:paginate_array_options) {{ total_count: 1000 }} - context 'Using kaminari' do - before do - ApiPagination.config.paginator = :kaminari - end + describe "#paginate" do + context 'Using kaminari' do + before do + ApiPagination.config.paginator = :kaminari + end - after do - ApiPagination.config.paginator = ENV['PAGINATOR'].to_sym - end + after do + ApiPagination.config.paginator = ENV['PAGINATOR'].to_sym + end + + it 'should accept paginate_array_options option' do + expect(Kaminari).to receive(:paginate_array) + .with(collection, paginate_array_options) + .and_call_original + + ApiPagination.paginate( + collection, + { + per_page: 30, + paginate_array_options: paginate_array_options + } + ) + end - it 'should accept paginate_array_options option' do - expect(Kaminari).to receive(:paginate_array) - .with(collection, paginate_array_options) - .and_call_original - - ApiPagination.paginate( - collection, - { - per_page: 30, - paginate_array_options: paginate_array_options - } - ) + describe '.pages_from' do + subject {described_class.pages_from collection} + + context 'on empty collection' do + let(:collection) {ApiPagination.paginate [], page: 1} + + it {is_expected.to be_empty} + end + end end - describe '.pages_from' do - subject { described_class.pages_from collection } + context 'Using will_paginate' do + before do + ApiPagination.config.paginator = :will_paginate + end - context 'on empty collection' do - let(:collection) { ApiPagination.paginate [], page: 1 } + after do + ApiPagination.config.paginator = ENV['PAGINATOR'].to_sym + end + + context 'passing in total_entries in options' do + it 'should set total_entries using the passed in value' do + paginated_collection = ApiPagination.paginate(collection, total_entries: 3000) + expect(paginated_collection.total_entries).to eq(3000) + end + end - it { is_expected.to be_empty } + context 'passing in collection only' do + it 'should set total_entries using the size of the collection ' do + paginated_collection = ApiPagination.paginate(collection) + expect(paginated_collection.total_entries).to eq(100) + end end end end