blib/lib/Mojolicious/Plugin/Pager.pm

filter: Statementstmt 100% 63 / 63 Branchbran 90.0% 45 / 50 Conditioncond 64.5% 31 / 48 MC/DCmcdc 33.3% 7 / 21 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 81.3% 157 / 193 CC 48 SCAR 30.7
CC
48
Cov
86%
pages_for
32.4
pager_link
12.0
register
10.3
CRAP 21.5
↑ Summary
1 package Mojolicious::Plugin::Pager;
2 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use constant PAGE_PARAM => 'page_param_name';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use constant SHOW_PREV_AND_NEXT => 'pager.show_prev_next';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '0.06';
10  
11 165 ▶sub pager_link {
Subroutine pager_link: called CC=12 SCAR=24.9
Pod documented
12 165 my ($self, $c, $page, @args) = @_;
13 165 my $url = $c->url_with;
14 165 ▶ my @text = (@args and ref $args[-1] eq 'CODE') ? () : ($page->{n});
Branch: 1 branch
BranchTrueFalseTotal
@args && ref $args[-1] eq 'CODE' ? : 146 19 165
Condition66%2/3
@args and ref $args[-1] eq "CODE"
!ll&&!rl&&r
190146
MC/DC50%1/2
@args and ref $args[-1] eq "CODE"
@args ref $args[-1] eq "CODE"
Truth table66%2/3
A and B
A= @argsB= ref $args[-1] eq "CODE"
ABresult
0X0
100
111
15 165 my (@extra, @classes);
16  
17 165 ▶ push @classes, $self->{classes}{current} if $page->{current};
Branch: 1 branch
BranchTrueFalseTotal
if $page->{"current"} 18 147 165
18 165 ▶ push @classes, $self->{classes}{first} if $page->{first};
Branch: 1 branch
BranchTrueFalseTotal
if $page->{"first"} 7 158 165
19 165 ▶ push @classes, $self->{classes}{last} if $page->{last};
Branch: 1 branch
BranchTrueFalseTotal
if $page->{"last"} 11 154 165
20 165 ▶ push @classes, $self->{classes}{next} if $page->{next};
Branch: 1 branch
BranchTrueFalseTotal
if $page->{"next"} 15 150 165
21 165 ▶ push @classes, $self->{classes}{prev} if $page->{prev};
Branch: 1 branch
BranchTrueFalseTotal
if $page->{"prev"} 15 150 165
22 165 ▶ push @classes, $self->{classes}{normal} unless @classes;
Branch: 1 branch
BranchTrueFalseTotal
unless @classes 104 61 165
23 165 ▶ push @extra, rel => 'next' if $page->{next};
Branch: 1 branch
BranchTrueFalseTotal
if $page->{"next"} 15 150 165
24 165 ▶ push @extra, rel => 'prev' if $page->{prev};
Branch: 1 branch
BranchTrueFalseTotal
if $page->{"prev"} 15 150 165
25  
26 165 ▶ $url->query->param($c->stash(PAGE_PARAM) => $page->{n} || 1);
Condition50%1/2
$page->{'n'} || 1
l!l
1650
MC/DC0%0/1
$page->{'n'} || 1
$page->{'n'}
Truth table50%1/2
A
A= $page->{'n'}
Aresult
11
00
27 165 return $c->link_to(@text, $url, class => join(' ', @classes), @extra, @args);
28 }
29  
30 22 ▶sub pages_for {
Subroutine pages_for: called CC=29 SCAR=34.8
Pod documented
31 22 my $c = shift;
32 22 ▶ my $args = ref $_[0] ? shift : {total_pages => @_ ? shift // 1 : undef};
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] ? : 0 22 22
@_ ? : 18 4 22
Condition50%1/2
shift() // 1
l!l
180
MC/DC0%0/1
shift() // 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
33 22 ▶ my $current_page = $args->{current} || $c->param($c->stash(PAGE_PARAM)) || 1;
Condition80%4/5
$args->{'current'} || $c->param($c->stash('page_param_name')) || 1
l!l
193
$args->{"current"} or $c->param($c->stash("page_param_name"))
l!l&&r!l&&!r
0193
MC/DC50%1/2
$args->{'current'} || $c->param($c->stash('page_param_name')) || 1
$args->{"current"} $c->param($c->stash("page_param_name"))
Truth table66%2/3
A or B
A= $args->{"current"}B= $c->param($c->stash("page_param_name"))
ABresult
1X1
011
000
34 22 ▶ my $pager_size = $args->{size} || 8;
Condition50%1/2
$args->{'size'} || 8
l!l
022
MC/DC0%0/1
$args->{'size'} || 8
$args->{'size'}
Truth table50%1/2
A
A= $args->{'size'}
Aresult
11
00
35 22 my $window_size = ($pager_size / 2) - 1;
36 22 my ($start_page, $total_pages, @pages);
37  
38 $total_pages
39 = POSIX::ceil($args->{total} // $args->{total_pages}
40 // ($args->{total_items} || $c->stash('total_items') || 0)
41 22 ▶ / ($args->{items_per_page} || $c->stash('items_per_page') || 20));
Condition66%10/15
($args->{"total"} // $args->{"total_pages"}) // ($args->{"total_items"} || $c->stash("total_items") || 0) / ($args->{"items_per_page"} || $c->stash("items_per_page") || 20)
l!l
184
$args->{"total"} // $args->{"total_pages"}
l!l&&r!l&&!r
0184
$args->{'total_items'} || $c->stash('total_items') || 0
l!l
40
$args->{"total_items"} or $c->stash("total_items")
l!l&&r!l&&!r
040
$args->{'items_per_page'} || $c->stash('items_per_page') || 20
l!l
13
$args->{"items_per_page"} or $c->stash("items_per_page")
l!l&&r!l&&!r
013
MC/DC50%1/2
($args->{"total"} // $args->{"total_pages"}) // ($args->{"total_items"} || $c->stash("total_items") || 0) / ($args->{"items_per_page"} || $c->stash("items_per_page") || 20)
$args->{"total"} $args->{"total_pages"}
MC/DC50%1/2
$args->{'items_per_page'} || $c->stash('items_per_page') || 20
$args->{"items_per_page"} $c->stash("items_per_page")
Truth table66%2/3
A // B
A= $args->{"total"}B= $args->{"total_pages"}
ABresult
1X1
011
000
Truth table66%2/3
A or B
A= $args->{"items_per_page"}B= $c->stash("items_per_page")
ABresult
1X1
011
000
42  
43 22 ▶ if ($current_page < $window_size) {
Branch: 2 branches
BranchTrueFalseTotal
if ($current_page < $window_size) { } 7 15 22
elsif ($current_page + $pager_size - $window_size > $total_pages) { } 7 8 15
44 7 $start_page = 1;
45 }
46 elsif ($current_page + $pager_size - $window_size > $total_pages) {
47 7 $start_page = 1 + $total_pages - $pager_size;
48 }
49 else {
50 8 $start_page = 1 + $current_page - $window_size;
51 }
52  
53 22 ▶ $start_page = 1 if $start_page < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $start_page < 1 2 20 22
54 22 for my $n ($start_page .. $total_pages) {
55 152 ▶ last if @pages >= $pager_size;
Branch: 1 branch
BranchTrueFalseTotal
if @pages >= $pager_size 8 144 152
56 144 push @pages, {n => $n};
57 144 ▶ $pages[-1]{first} = 1 if $n == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 1 10 134 144
58 144 ▶ $pages[-1]{last} = 1 if $n == $total_pages;
Branch: 1 branch
BranchTrueFalseTotal
if $n == $total_pages 14 130 144
59 144 ▶ $pages[-1]{current} = 1 if $n == $current_page;
Branch: 1 branch
BranchTrueFalseTotal
if $n == $current_page 21 123 144
60 }
61  
62 22 ▶ return $c->stash('pages_as_array_ref') ? \@pages : @pages unless @pages;
Branch: 2 branches
BranchTrueFalseTotal
unless @pages 0 22 22
$c->stash('pages_as_array_ref') ? : 0 0 0
63 22 ▶ return $c->stash('pages_as_array_ref') ? \@pages : @pages
Branch: 2 branches
BranchTrueFalseTotal
if $total_pages <= $pager_size and not $c->stash("pager.show_prev_next") 5 17 22
$c->stash('pages_as_array_ref') ? : 1 4 5
Condition100%3/3
$total_pages <= $pager_size and not $c->stash("pager.show_prev_next")
!ll&&!rl&&r
1435
MC/DC100%2/2
$total_pages <= $pager_size and not $c->stash("pager.show_prev_next")
$total_pages <= $pager_size not $c->stash("pager.show_prev_next")
Truth table100%3/3
A and not(B)
A= $total_pages <= $pager_sizeB= not $c->stash("pager.show_prev_next")
ABresult
0X0
100
111
64 if $total_pages <= $pager_size and !$c->stash(SHOW_PREV_AND_NEXT);
65  
66 17 ▶ unshift @pages, {prev => 1, n => $current_page - 1} if $current_page > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $current_page > 1 15 2 17
67 17 ▶ push @pages, {next => 1, n => $current_page + 1} if $current_page < $total_pages;
Branch: 1 branch
BranchTrueFalseTotal
if $current_page < $total_pages 15 2 17
68  
69 17 ▶ return $c->stash('pages_as_array_ref') ? \@pages : @pages;
Branch: 1 branch
BranchTrueFalseTotal
$c->stash('pages_as_array_ref') ? : 0 17 17
70 }
71  
72 2 ▶sub register {
Subroutine register: called CC=9 SCAR=23.3
Pod documented
73 2 my ($self, $app, $config) = @_;
74  
75 2 ▶ $app->defaults(PAGE_PARAM, $config->{param_name} || 'page');
Condition50%1/2
$config->{'param_name'} || 'page'
l!l
02
MC/DC0%0/1
$config->{'param_name'} || 'page'
$config->{'param_name'}
Truth table50%1/2
A
A= $config->{'param_name'}
Aresult
11
00
76 2 ▶ $app->defaults(SHOW_PREV_AND_NEXT, $config->{always_show_prev_next} || 0);
Condition100%2/2
$config->{'always_show_prev_next'} || 0
l!l
11
MC/DC100%1/1
$config->{'always_show_prev_next'} || 0
$config->{'always_show_prev_next'}
Truth table100%2/2
A
A= $config->{'always_show_prev_next'}
Aresult
11
00
77  
78 2 ▶ $self->{classes}{current} = $config->{classes}{current} || 'active';
Condition50%1/2
$config->{'classes'}{'current'} || 'active'
l!l
02
MC/DC0%0/1
$config->{'classes'}{'current'} || 'active'
$config->{'classes'}{'current'}
Truth table50%1/2
A
A= $config->{'classes'}{'current'}
Aresult
11
00
79 2 ▶ $self->{classes}{first} = $config->{classes}{first} || 'first';
Condition50%1/2
$config->{'classes'}{'first'} || 'first'
l!l
02
MC/DC0%0/1
$config->{'classes'}{'first'} || 'first'
$config->{'classes'}{'first'}
Truth table50%1/2
A
A= $config->{'classes'}{'first'}
Aresult
11
00
80 2 ▶ $self->{classes}{last} = $config->{classes}{last} || 'last';
Condition50%1/2
$config->{'classes'}{'last'} || 'last'
l!l
02
MC/DC0%0/1
$config->{'classes'}{'last'} || 'last'
$config->{'classes'}{'last'}
Truth table50%1/2
A
A= $config->{'classes'}{'last'}
Aresult
11
00
81 2 ▶ $self->{classes}{next} = $config->{classes}{next} || 'next';
Condition50%1/2
$config->{'classes'}{'next'} || 'next'
l!l
02
MC/DC0%0/1
$config->{'classes'}{'next'} || 'next'
$config->{'classes'}{'next'}
Truth table50%1/2
A
A= $config->{'classes'}{'next'}
Aresult
11
00
82 2 ▶ $self->{classes}{prev} = $config->{classes}{prev} || 'prev';
Condition50%1/2
$config->{'classes'}{'prev'} || 'prev'
l!l
02
MC/DC0%0/1
$config->{'classes'}{'prev'} || 'prev'
$config->{'classes'}{'prev'}
Truth table50%1/2
A
A= $config->{'classes'}{'prev'}
Aresult
11
00
83 2 ▶ $self->{classes}{normal} = $config->{classes}{normal} || 'page';
Condition50%1/2
$config->{'classes'}{'normal'} || 'page'
l!l
02
MC/DC0%0/1
$config->{'classes'}{'normal'} || 'page'
$config->{'classes'}{'normal'}
Truth table50%1/2
A
A= $config->{'classes'}{'normal'}
Aresult
11
00
84  
85 2 ▶ $app->helper(pager_link => sub { $self->pager_link(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
86 2 $app->helper(pages_for => \&pages_for);
87 }
88  
89 2 1;
90  
91 =encoding utf8
92  
93 =head1 NAME
94  
95 Mojolicious::Plugin::Pager - Pagination plugin for Mojolicious
96  
97 =head1 SYNOPSIS
98  
99 =head2 Example lite app
100  
101 use Mojolicious::Lite;
102  
103 plugin "pager";
104  
105 get "/" => sub {
106 my $c = shift;
107 $c->stash(total_items => 1431, items_per_page => 20);
108 };
109  
110 =head2 Example template
111  
112 <ul class="pager">
113 % for my $page (pages_for $total_items / $items_per_page) {
114 <li><%= pager_link $page %></li>
115 % }
116 </ul>
117  
118 =head2 Custom template
119  
120 <ul class="pager">
121 % for my $page (pages_for $total_items / $items_per_page) {
122 % my $url = url_with; $url->query->param(x => $page->{n});
123 <li><%= link_to "hey!", $url %></li>
124 % }
125 </ul>
126  
127 =head1 DESCRIPTION
128  
129 L<Mojolicious::Plugin::Pager> is a L<Mojolicious> plugin for creating paged
130 navigation, without getting in the way. There are other plugins which ship with
131 complete markup, but this is often not the markup that I<you> want.
132  
133 =head1 HELPERS
134  
135 =head2 pager_link
136  
137 $bytestream = $c->pager_link(\%page, @args);
138 $bytestream = $c->pager_link(\%page, @args, sub { int(rand 100) });
139  
140 Takes a C<%page> hash and creates an anchor using
141 L<Mojolicious::Controller/link_to>. C<@args> is passed on, without
142 modification, to C<link_to()>. The anchor generated has some classes added.
143  
144 See L</pages_for> for detail about C<%page>.
145  
146 Examples output:
147  
148 <a href="?page=2" class="prev" rel="prev">12</a>
149 <a href="?page=1" class="first">1</a>
150 <a href="?page=2" class="page">2</a>
151 <a href="?page=3" class="active">3</a>
152 <a href="?page=4" class="page">4</a>
153 <a href="?page=5" class="page">5</a>
154 <a href="?page=6" class="last">6</a>
155 <a href="?page=3" class="next" rel="next">3</a>
156  
157 =head2 pages_for
158  
159 @pages = $self->pages_for($total_pages);
160 @pages = $self->pages_for(\%args)
161 @pages = $self->pages_for;
162  
163 Returns a list of C<%page> hash-refs, that can be passed on to L</pager_link>.
164  
165 Example C<%page>:
166  
167 {
168 n => 2, # page number
169 current => 1, # if page number matches "page" query parameter
170 first => 1, # if this is the first page
171 last => 1, # if this is the last page
172 next => 1, # if this is last, that brings you to the next page
173 prev => 1, # if this is first, that brings you to the previous page
174 }
175  
176 C<%args> can contain:
177  
178 =over 2
179  
180 =item * current
181  
182 Default to the "page" query param or "1".
183  
184 =item * items_per_page
185  
186 Only useful unless C<total> is specified. Default to 20.
187  
188 =item * size
189  
190 The max number of pages to show in the pagination. Default to 8 + "Previous"
191 and "Next" links.
192  
193 =item * total
194  
195 The total number of pages. Default to "1" or...
196  
197 $total = $args->{total_items} / $args->{items_per_page}
198 $total = $c->stash('total_items') / $c->stash('items_per_page')
199  
200 =back
201  
202 =head1 METHODS
203  
204 =head2 register
205  
206 $app->plugin(pager => \%config);
207  
208 Used to register this plugin and the L</HELPERS> above. C<%config> can be:
209  
210 =over 4
211  
212 =item * classes
213  
214 Used to set default class names, used by L</pager_link>.
215  
216 Default:
217  
218 {
219 current => "active",
220 first => "first",
221 last => "last",
222 next => "next",
223 prev => "prev",
224 normal => "page",
225 }
226  
227 =item * param_name
228  
229 The query parameter that will be looked up to figure out which page you are on.
230 Can also be set in L<Mojolicious::Controller/stash> on each request under the
231 name "page_param_name".
232  
233 Default: "page"
234  
235 =back
236  
237 =head1 AUTHOR
238  
239 Jan Henning Thorsen
240  
241 =head1 COPYRIGHT AND LICENSE
242  
243 Copyright (C) 2017, Jan Henning Thorsen
244  
245 This program is free software, you can redistribute it and/or modify it under
246 the terms of the Artistic License version 2.0.
247  
248 =cut
↑ Summary