Skip to content

Commit bc904de

Browse files
committed
Add big table limited pagination
1 parent 5c5044d commit bc904de

File tree

6 files changed

+91
-20
lines changed

6 files changed

+91
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- if current_page.last?
2+
%li.next.disabled= link_to raw(t 'admin.pagination.next'), '#'
3+
- else
4+
%li.next= link_to raw(t 'admin.pagination.next'), url, class: (remote ? 'pjax' : '')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
= paginator.render do
2+
%ul.pagination
3+
= prev_page_tag if !current_page.first?
4+
= next_page_tag
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- if current_page.first?
2+
%li.prev.disabled= link_to raw(t 'admin.pagination.previous'), '#'
3+
- else
4+
%li.prev= link_to raw(t 'admin.pagination.previous'), url, class: (remote ? 'pjax' : '')

app/views/rails_admin/main/index.html.haml

+5-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@
106106
%td.last.links
107107
%ul.inline.list-inline= menu_for :member, @abstract_model, object, true
108108

109-
- if @objects.respond_to?(:total_count)
109+
- if @model_config.list.limited_pagination
110+
.row
111+
.col-md-6= paginate(@objects, theme: 'ra-twitter-bootstrap/without_count', total_pages: Float::INFINITY, remote: true)
112+
113+
- elsif @objects.respond_to?(:total_count)
110114
- total_count = @objects.total_count.to_i
111115
.row
112116
.col-md-6= paginate(@objects, theme: 'ra-twitter-bootstrap', remote: true)

lib/rails_admin/config/sections/list.rb

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ class List < RailsAdmin::Config::Sections::Base
1818
RailsAdmin::Config.default_items_per_page
1919
end
2020

21+
# Positive value shows only prev, next links in pagination.
22+
# This is for avoiding count(*) query.
23+
register_instance_option :limited_pagination do
24+
false
25+
end
26+
2127
register_instance_option :sort_by do
2228
parent.abstract_model.primary_key
2329
end

spec/integration/basic/list/rails_admin_basic_list_spec.rb

+68-19
Original file line numberDiff line numberDiff line change
@@ -338,32 +338,81 @@
338338
end
339339
end
340340

341-
describe 'GET /admin/player with 3 pages, page 2' do
341+
context 'List with 3 pages' do
342+
def visit_page(page)
343+
visit index_path(model_name: 'player', page: page)
344+
end
345+
342346
before do
343347
RailsAdmin.config.default_items_per_page = 1
344-
items_per_page = RailsAdmin.config.default_items_per_page
345-
(items_per_page * 3).times { FactoryGirl.create(:player) }
346-
visit index_path(model_name: 'player', page: 2)
348+
(RailsAdmin.config.default_items_per_page * 3).times { FactoryGirl.create(:player) }
347349
end
348350

349-
it 'paginates correctly' do
350-
expect(find('ul.pagination li:first')).to have_content('« Prev')
351-
expect(find('ul.pagination li:last')).to have_content('Next »')
352-
expect(find('ul.pagination li.active')).to have_content('2')
353-
end
354-
end
351+
describe 'with limited_pagination=false' do
352+
it 'page 1' do
353+
visit_page(1)
355354

356-
describe 'list with 3 pages, page 3' do
357-
before do
358-
items_per_page = RailsAdmin.config.default_items_per_page
359-
@players = Array.new((items_per_page * 3)) { FactoryGirl.create(:player) }
360-
visit index_path(model_name: 'player', page: 3)
355+
within('ul.pagination') do
356+
expect(find('li:first')).to have_content('« Prev')
357+
expect(find('li:last')).to have_content('Next »')
358+
expect(find('li.active')).to have_content('1')
359+
end
360+
end
361+
362+
it 'page 2' do
363+
visit_page(2)
364+
365+
within('ul.pagination') do
366+
expect(find('li:first')).to have_content('« Prev')
367+
expect(find('li:last')).to have_content('Next »')
368+
expect(find('li.active')).to have_content('2')
369+
end
370+
end
371+
372+
it 'page 3' do
373+
visit_page(3)
374+
375+
within('ul.pagination') do
376+
expect(find('li:first')).to have_content('« Prev')
377+
expect(find('li:last')).to have_content('Next »')
378+
expect(find('li.active')).to have_content('3')
379+
end
380+
end
361381
end
362382

363-
it 'paginates correctly and contain the right item' do
364-
expect(find('ul.pagination li:first')).to have_content('« Prev')
365-
expect(find('ul.pagination li:last')).to have_content('Next »')
366-
expect(find('ul.pagination li.active')).to have_content('3')
383+
context 'with limited_pagination=true' do
384+
before do
385+
allow(RailsAdmin::AbstractModel.new(Player).config.list).
386+
to receive(:limited_pagination).
387+
and_return(true)
388+
end
389+
390+
it 'page 1' do
391+
visit_page(1)
392+
393+
within('ul.pagination') do
394+
expect(find('li:first')).not_to have_content('« Prev')
395+
expect(find('li:last')).to have_content('Next »')
396+
end
397+
end
398+
399+
it 'page 2' do
400+
visit_page(2)
401+
402+
within('ul.pagination') do
403+
expect(find('li:first')).to have_content('« Prev')
404+
expect(find('li:last')).to have_content('Next »')
405+
end
406+
end
407+
408+
it 'page 3' do
409+
visit_page(3)
410+
411+
within('ul.pagination') do
412+
expect(find('li:first')).to have_content('« Prev')
413+
expect(find('li:last')).to have_content('Next »')
414+
end
415+
end
367416
end
368417
end
369418

0 commit comments

Comments
 (0)