Skip to content

Commit 5e9e601

Browse files
authored
Merge pull request #13 from blocknotes/feat/pagination-improvements
feat: pagination component improvements
2 parents fb404f3 + 8e1ac44 commit 5e9e601

File tree

5 files changed

+71
-19
lines changed

5 files changed

+71
-19
lines changed

lib/tiny_admin/views/components/pagination.rb

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,38 @@ def template
1616
div(class: 'pagination-div') {
1717
nav('aria-label' => 'Pagination') {
1818
ul(class: 'pagination justify-content-center') {
19-
1.upto(pages) do |i|
20-
li_class = (i == current ? 'page-item active' : 'page-item')
21-
li(class: li_class) {
22-
href = query_string.empty? ? "?p=#{i}" : "?#{query_string}&p=#{i}"
23-
a(class: 'page-link', href: href) { i }
24-
}
19+
if pages <= 10
20+
pages_range(1..pages)
21+
elsif current <= 4 || current >= pages - 3
22+
pages_range(1..(current <= 4 ? current + 2 : 4), with_dots: true)
23+
pages_range((current > pages - 4 ? current - 2 : pages - 2)..pages)
24+
else
25+
pages_range(1..1, with_dots: true)
26+
pages_range(current - 2..current + 2, with_dots: true)
27+
pages_range(pages..pages)
2528
end
2629
}
2730
}
2831
}
2932
end
33+
34+
private
35+
36+
def pages_range(range, with_dots: false)
37+
range.each do |page|
38+
li(class: page == current ? 'page-item active' : 'page-item') {
39+
href = query_string.empty? ? "?p=#{page}" : "?#{query_string}&p=#{page}"
40+
a(class: 'page-link', href: href) { page }
41+
}
42+
end
43+
dots if with_dots
44+
end
45+
46+
def dots
47+
li(class: 'page-item disabled') {
48+
a(class: 'page-link') { '...' }
49+
}
50+
end
3051
end
3152
end
3253
end

spec/dummy/config/tiny_admin.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ sections:
2929
type: resource
3030
model: Post
3131
index:
32+
pagination: 5
3233
attributes:
3334
- id
3435
- title

spec/features/components/pagination_spec.rb

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,28 @@
33
require 'rails_helper'
44

55
RSpec.describe 'Pagination component', type: :feature do
6-
before { setup_data }
6+
def page_links
7+
find_all('a.page-link').map(&:text)
8+
end
79

8-
context 'when clicking the Posts navbar link' do
10+
context 'with a collection with less than 10 pages' do
911
before do
12+
setup_data(posts_count: 15)
13+
visit '/admin'
14+
log_in
15+
click_link('Posts')
16+
end
17+
18+
it 'shows the pagination widget', :aggregate_failures do
19+
expect(page).to have_css('ul', class: 'pagination')
20+
expect(page).to have_link('1', class: 'page-link', href: '?p=1')
21+
expect(page_links).to eq(['1', '2', '3'])
22+
end
23+
end
24+
25+
context 'with a collection with more than 10 pages' do
26+
before do
27+
setup_data(posts_count: 60)
1028
visit '/admin'
1129
log_in
1230
end
@@ -15,9 +33,20 @@
1533
click_link('Posts')
1634

1735
expect(page).to have_css('ul', class: 'pagination')
18-
expect(page).to have_link('1', class: 'page-link', href: '?p=1')
19-
expect(page).to have_link('2', class: 'page-link', href: '?p=2')
20-
expect(page).to have_link('3', class: 'page-link', href: '?p=3')
36+
expect(page).to have_link('1', class: 'page-link', href: '?p=12')
37+
expect(page_links).to eq(['1', '2', '3', '...', '10', '11', '12'])
38+
end
39+
40+
context 'when opening a specific page' do
41+
before do
42+
visit '/admin/posts?p=6'
43+
end
44+
45+
it 'shows the pagination widget', :aggregate_failures do
46+
expect(page).to have_css('ul', class: 'pagination')
47+
expect(page).to have_link('6', class: 'page-link', href: '?p=6')
48+
expect(page_links).to eq(['1', '...', '4', '5', '6', '7', '8', '...', '12'])
49+
end
2150
end
2251
end
2352
end

spec/features/resources_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
context "when filtering by title in the post's listing page" do
4444
before do
45+
setup_data(posts_count: 15)
4546
visit '/admin/posts?some_var=1'
4647
log_in
4748
end

spec/support/setup_data.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# frozen_string_literal: true
22

33
RSpec.shared_context 'with some data' do
4-
def setup_data
5-
5.times do
6-
# Authors
4+
def setup_data(posts_count: 5)
5+
# Authors
6+
authors = Array.new(3) do
77
author_ref = Author.count + 1
8-
author = Author.create!(name: "An author #{author_ref}", age: 24 + (author_ref * 3), email: "aaa#{author_ref}@bbb.ccc")
8+
Author.create!(name: "An author #{author_ref}", age: 24 + (author_ref * 3), email: "aaa#{author_ref}@bbb.ccc")
9+
end
910

10-
# Posts
11+
# Posts
12+
posts_count.times do |i|
1113
post_ref = Post.count + 1
12-
5.times do |i|
13-
Post.create!(author: author, title: "A post #{post_ref + i}", description: 'Some post content')
14-
end
14+
Post.create!(author: authors[i % 3], title: "A post #{post_ref + i}", description: 'Some post content')
1515
end
1616
end
1717
end

0 commit comments

Comments
 (0)