PageHistory.pm

filter: Statementstmt 94.1% 48 / 51 Branchbran 87.5% 7 / 8 Conditioncond 40.0% 6 / 15 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 88.8% 8 / 9 Podpod n/a totaltotal 76.0% 70 / 92 CC 10 SCAR 12.5
CC
10
Cov
82%
__ANON__
6.0
history
5.1
__ANON__
4.1
CRAP 3.5
« PageHistory/PageSet.pm ↑ Summary PageHistory/Page.pm »
1 package Dancer::Plugin::PageHistory;
2  
3 =head1 NAME
4  
5 Dancer::Plugin::PageHistory - store recent page history for user into session
6  
7 =head1 VERSION
8  
9 Version 0.102
10  
11 =cut
12  
13 2 our $VERSION = '0.102';
14  
15 2 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Dancer::Plugin::PageHistory::PageSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Dancer::Plugin::PageHistory::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Data::Structure::Util qw/unbless/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 my $history_name = 'page_history';
22  
23 =head1 SYNOPSIS
24  
25 get '/product/:sku/:name' => sub {
26 add_to_history(
27 type => 'product',
28 title => param('name'),
29 attributes => { sku => param('sku') }
30 );
31 };
32  
33 hook 'before_template_render' => sub {
34 my $tokens = shift;
35 $tokens->{previous_page} = history->previous_page->uri;
36 };
37  
38 =head1 DESCRIPTION
39  
40 The C<add_to_history> keyword which is exported by this plugin allows you to
41 add interesting items to the history lists which are returned using the
42 C<history> keyword.
43  
44 =head1 KEYWORDS
45  
46 =head2 add_to_history
47  
48 Adds a page via L<Dancer::Plugin::PageHistory::PageSet/add>. Both of
49 L<path|Dancer::Plugin::PageHistory::Page/path> and
50 L<query|Dancer::Plugin::PageHistory::Page/query> are optional arguments
51 which will be set automatically from the current request if they are not
52 supplied.
53  
54 =head2 history
55  
56 Returns the current L<Dancer::Plugin::PageHistory::PageSet> object from the
57 user's session.
58  
59 =head1 SUPPORTED SESSION ENGINES
60  
61 L<CHI|Dancer::Session::CHI>,
62 L<Cookie|Dancer::Session::Cookie>,
63 L<DBIC|Dancer::Session::DBIC>,
64 L<JSON|Dancer::Session::JSON>,
65 L<Memcached|Dancer::Session::Memcached>,
66 L<Memcached::Fast|Dancer::Session::Memcached::Fast>,
67 L<MongoDB|Dancer::Session::MongoDB>,
68 L<PSGI|Dancer::Session::PSGI>,
69 L<Simple|Dancer::Session::Simple>,
70 L<Storable|Dancer::Session::Storable>,
71 L<YAML|Dancer::Session::YAML>
72  
73 =head1 CAVEATS
74  
75 L<Dancer::Session::Cookie> and L<Dancer::Session::PSGI> either don't handle
76 destroy at all or else do it wrong so I suggest you avoid those modules if
77 you want things like logout to work.
78  
79 See L</TODO>.
80  
81 =head1 CONFIGURATION
82  
83 No configuration is necessarily required.
84  
85 If you wish to have arguments passed to
86 L<Dancer::Plugin::PageHistory::PageSet/new> these can be added to your
87 configuration along with configuration for the plugin itself, e.g.:
88  
89 plugins:
90 PageHistory:
91 add_all_pages: 1
92 ingore_ajax: 1
93 history_name: someothername
94 PageSet:
95 default_type: all
96 fallback_page:
97 path: "/"
98 max_items: 20
99 methods:
100 - default
101 - product
102 - navigation
103
104 Configuration options for the plugin itself:
105  
106 =over
107  
108 =item * add_all_pages
109  
110 Defaults to 0. Set to 1 to have all pages added to the list
111 L<Dancer::Plugin::PageHistory::PageSet/default_type> in the L</before> hook.
112  
113 =item * ignore_ajax
114  
115 If L</add_all_pages> is true this controls whether ajax requests are added to
116 the list L<Dancer::Plugin::PageHistory::PageSet/default_type> in the
117 L</before> hook.
118  
119 Defaults to 0. Set to 1 to have ajax requests ignored.
120  
121 =item * history_name
122  
123 This setting can be used to change the name of the key used to store
124 the history object in the session from the default C<page_history> to
125 something else. This is also the key used for name of the token
126 containing the history object that is passed to templates and also the var
127 used to cache the history object during the request lifetime.
128  
129 =back
130  
131 =head1 HOOKS
132  
133 This plugin makes use of the following hooks:
134  
135 =head2 before
136  
137 Add current page to history. See L</add_all_pages> and L</ignore_ajax>.
138  
139 =cut
140  
141 14 ▶hook before => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
142 14 my $conf = plugin_setting;
143 return
144 if ( !$conf->{add_all_pages}
145 14 ▶ || ( $conf->{ignore_ajax} && request->is_ajax ) );
Branch: 1 branch
BranchTrueFalseTotal
if not $conf->{"add_all_pages"} or $conf->{"ignore_ajax"} and request()->is_ajax 4 10 14
Condition66%4/6
not $conf->{"add_all_pages"} or $conf->{"ignore_ajax"} and request()->is_ajax
l!l&&r!l&&!r
0410
$conf->{"ignore_ajax"} and request()->is_ajax
!ll&&!rl&&r
0104
MC/DC33%1/3
not $conf->{"add_all_pages"} or $conf->{"ignore_ajax"} and request()->is_ajax
not $conf->{"add_all_pages"} $conf->{"ignore_ajax"} request()->is_ajax
Truth table50%2/4
not(A) or B and C
A= not $conf->{"add_all_pages"}B= $conf->{"ignore_ajax"}C= request()->is_ajax
ABCresult
1XX1
0111
00X0
0100
146 10 add_to_history();
147 2 };
148  
149 =head2 before_template_render
150  
151 Puts history into the token C<page_history>.
152  
153 =cut
154  
155 0 ▶hook before_template_render => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
156 0 my $tokens = shift;
157 0 ▶ my $name = plugin_setting->{history_name} || $history_name;
Condition0%0/3
plugin_setting()->{'history_name'} || $history_name
l!l&&r!l&&!r
000
MC/DC0%0/2
plugin_setting()->{'history_name'} || $history_name
plugin_setting()->{'history_name'} $history_name
Truth table0%0/3
A || B
A= plugin_setting()->{'history_name'}B= $history_name
ABresult
1X1
011
000
158 0 $tokens->{$name} = history();
159 2 };
160  
161 12 ▶sub add_to_history {
Subroutine add_to_history: called CC=2 SCAR=7.0
162 12 ▶ my $name = plugin_setting->{history_name} || $history_name;
Condition33%1/3
plugin_setting()->{'history_name'} || $history_name
l!l&&r!l&&!r
0120
MC/DC0%0/2
plugin_setting()->{'history_name'} || $history_name
plugin_setting()->{'history_name'} $history_name
Truth table33%1/3
A || B
A= plugin_setting()->{'history_name'}B= $history_name
ABresult
1X1
011
000
163 12 my ( $self, @args ) = plugin_args(@_);
164  
165 12 my $path = request->path;
166 12 my $query = params('query');
167  
168 12 my %args = (
169 path => $path,
170 query => $query,
171 @args,
172 );
173  
174 12 debug "adding page to history: ", \%args;
175  
176 12 my $history = history();
177  
178 # add the page and save back to session with pages all unblessed
179 12 $history->add( %args );
180 12 session $name => unbless( $history->pages );
181 }
182  
183 12 ▶sub history {
Subroutine history: called CC=5 SCAR=16.2
184 12 my $conf = plugin_setting;
185 12 ▶ my $name = $conf->{history_name} || $history_name;
Condition33%1/3
$conf->{'history_name'} || $history_name
l!l&&r!l&&!r
0120
MC/DC0%0/2
$conf->{'history_name'} || $history_name
$conf->{'history_name'} $history_name
Truth table33%1/3
A || B
A= $conf->{'history_name'}B= $history_name
ABresult
1X1
011
000
186 12 my $history;
187  
188 12 ▶ if ( defined var($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined var($name)) { } 2 10 12
189 2 $history = var($name);
190 }
191 else {
192  
193 10 my $session_history = session($name);
194 10 ▶ $session_history = {} unless ref($session_history) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $session_history eq "HASH" 4 6 10
195  
196 10 ▶ my %args = $conf->{PageSet} ? %{ $conf->{PageSet} } : ();
Branch: 1 branch
BranchTrueFalseTotal
$conf->{'PageSet'} ? : 10 0 10
197 10 $args{pages} = $session_history;
198  
199 10 $history = Dancer::Plugin::PageHistory::PageSet->new(%args);
200 10 var $name => $history;
201 }
202  
203 12 return $history;
204 }
205  
206 2 register add_to_history => \&add_to_history;
207  
208 2 register history => \&history;
209  
210 2 register_plugin;
211  
212 =head1 TODO
213  
214 =over
215  
216 =item * Add more tests
217  
218 =item * Add support for more session engines
219  
220 =item * Create Dancer2 plugin
221  
222 =item * investigate C<destroy> problems with L<Dancer::Session::Cookie>
223 and L<Dancer::Session::PSGI>
224  
225 =back
226  
227 =head1 AUTHOR
228  
229 Peter Mottram (SysPete), "peter@sysnix.com"
230  
231 =head1 BUGS
232  
233 Please report any bugs or feature requests via the project's GitHub
234 issue tracker:
235  
236 L<https://github.com/SysPete/Dancer-Plugin-PageHistory/issues>
237  
238 I will be notified, and then you'll automatically be notified of
239 progress on your bug as I make changes. PRs are always welcome.
240  
241 =head1 SUPPORT
242  
243 You can find documentation for this module with the perldoc command.
244  
245 perldoc Dancer::Plugin::PageHistory
246  
247 You can also look for information at:
248  
249 =over 4
250  
251 =item * L<GitHub repository|https://github.com/SysPete/Dancer-Plugin-PageHistory>
252  
253 =item * L<meta::cpan|https://metacpan.org/pod/Dancer::Plugin::PageHistory>
254  
255 =back
256  
257 =head1 LICENSE AND COPYRIGHT
258  
259 Copyright 2015 Peter Mottram (SysPete).
260  
261 This program is free software; you can redistribute it and/or modify
262 it under the same terms as the Perl 5 programming language system itself.
263  
264 See http://dev.perl.org/licenses/ for more information.
265  
266 =cut
267  
268 2 1; # End of Dancer::Plugin::PageHistory
« PageHistory/PageSet.pm ↑ Summary PageHistory/Page.pm »