Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Initialize will paginate collection array correctly #556

Merged
merged 1 commit into from
Aug 6, 2017
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion lib/chewy/search/pagination/will_paginate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def page(page)
private

def paginated_collection(collection)
::WillPaginate::Collection.create(current_page, per_page, total_entries) do |pager|
page = current_page || 1
per = per_page || ::WillPaginate.per_page
::WillPaginate::Collection.create(page, per, total_entries) do |pager|
pager.replace collection
end
end
Expand Down
10 changes: 7 additions & 3 deletions spec/chewy/search/pagination/kaminari_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
describe Chewy::Search::Pagination::Kaminari do
it_behaves_like :kaminari, Chewy::Search::Request do
describe '#objects' do
let(:data) { Array.new(10) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
let(:data) { Array.new(12) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }

before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
before { allow(::Kaminari.config).to receive_messages(default_per_page: 3) }
before { allow(::Kaminari.config).to receive_messages(default_per_page: 17) }

specify { expect(search.objects.class).to eq(Kaminari::PaginatableArray) }
specify { expect(search.objects.total_count).to eq(12) }
specify { expect(search.objects.limit_value).to eq(17) }
specify { expect(search.objects.offset_value).to eq(0) }
specify { expect(search.per(2).page(3).objects.class).to eq(Kaminari::PaginatableArray) }
specify { expect(search.per(2).page(3).objects.total_count).to eq(10) }
specify { expect(search.per(2).page(3).objects.total_count).to eq(12) }
specify { expect(search.per(2).page(3).objects.limit_value).to eq(2) }
specify { expect(search.per(2).page(3).objects.offset_value).to eq(4) }
end
Expand Down
12 changes: 9 additions & 3 deletions spec/chewy/search/pagination/will_paginate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
describe Chewy::Search::Pagination::WillPaginate do
it_behaves_like :will_paginate, Chewy::Search::Request do
describe '#objects' do
let(:data) { Array.new(10) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
let(:data) { Array.new(12) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }

before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
before { allow(::WillPaginate).to receive_messages(per_page: 3) }
before { allow(::WillPaginate).to receive_messages(per_page: 17) }

# specify { expect(search.current_page).to eq(1) }
# specify { expect(search.per_page).to eq(0) }
specify { expect(search.objects.class).to eq(WillPaginate::Collection) }
specify { expect(search.objects.total_entries).to eq(12) }
specify { expect(search.objects.per_page).to eq(17) }
specify { expect(search.objects.current_page).to eq(1) }
specify { expect(search.paginate(per_page: 2, page: 3).objects.class).to eq(WillPaginate::Collection) }
specify { expect(search.paginate(per_page: 2, page: 3).objects.total_entries).to eq(10) }
specify { expect(search.paginate(per_page: 2, page: 3).objects.total_entries).to eq(12) }
specify { expect(search.paginate(per_page: 2, page: 3).objects.per_page).to eq(2) }
specify { expect(search.paginate(per_page: 2, page: 3).objects.current_page).to eq(3) }
end
Expand Down