File Coverage

blib/lib/Dancer2/Plugin/PageHistory.pm
Criterion Covered Total %
statement 36 38 94.7
branch 2 2 100.0
condition 5 8 62.5
subroutine 12 13 92.3
pod 2 3 66.6
total 57 64 89.0


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::PageHistory;
2 4     4   868711 use utf8;
  4         13  
  4         23  
3 4     4   112 use strict;
  4         6  
  4         123  
4 4     4   15 use warnings;
  4         5  
  4         178  
5              
6             =encoding utf8
7              
8             =head1 NAME
9              
10             Dancer2::Plugin::PageHistory - store recent page history for user into session
11              
12             =head1 VERSION
13              
14             Version 0.210
15              
16             =cut
17              
18             our $VERSION = '0.210';
19              
20 4     4   426 use Dancer2::Core::Types qw/Bool HashRef Str/;
  4         8260  
  4         264  
21 4     4   1902 use Dancer2::Plugin;
  4         75688  
  4         23  
22 4     4   20657 use Dancer2::Plugin::PageHistory::PageSet;
  4         7  
  4         103  
23 4     4   1712 use Dancer2::Plugin::PageHistory::Page;
  4         7  
  4         108  
24 4     4   1779 use Data::Structure::Util qw/unbless/;
  4         8312  
  4         1501  
25              
26             my $history_name = 'page_history';
27              
28             =head1 SYNOPSIS
29              
30             get '/product/:sku/:name' => sub {
31             add_to_history(
32             type => 'product',
33             title => param('name'),
34             attributes => { sku => param('sku') }
35             );
36             };
37              
38             hook 'before_template_render' => sub {
39             my $tokens = shift;
40             $tokens->{previous_page} = history->previous_page->uri;
41             };
42              
43             =head1 DESCRIPTION
44              
45             The C keyword which is exported by this plugin allows you to
46             add interesting items to the history lists which are returned using the
47             C keyword.
48              
49             =head1 KEYWORDS
50              
51             =head2 add_to_history
52              
53             Adds a page via L. Both of
54             L and
55             L are optional
56             arguments
57             which will be set automatically from the current request if they are not
58             supplied.
59              
60             =head2 history
61              
62             Returns the current L object from the
63             user's session.
64              
65             =head1 SUPPORTED SESSION ENGINES
66              
67             L,
68             L,
69             L,
70             L,
71             L,
72             L,
73             L,
74             L,
75             L,
76             L,
77             L
78              
79             =head1 CONFIGURATION
80              
81             No configuration is necessarily required.
82              
83             If you wish to have arguments passed to
84             L these can be added to your
85             configuration along with configuration for the plugin itself, e.g.:
86              
87             plugins:
88             PageHistory:
89             add_all_pages: 1
90             ingore_ajax: 1
91             history_name: someothername
92             PageSet:
93             default_type: all
94             fallback_page:
95             path: "/"
96             max_items: 20
97             methods:
98             - default
99             - product
100             - navigation
101            
102             Configuration options for the plugin itself:
103              
104             =over
105              
106             =item * add_all_pages
107              
108             Defaults to 0. Set to 1 to have all pages added to the list
109             L in the L hook.
110              
111             =item * ignore_ajax
112              
113             If L is true this controls whether ajax requests are added to
114             the list L in the
115             L hook.
116              
117             Defaults to 0. Set to 1 to have ajax requests ignored.
118              
119             =item * history_name
120              
121             This setting can be used to change the name of the key used to store
122             the history object in the session from the default C to
123             something else. This is also the key used for name of the token
124             containing the history object that is passed to templates.
125              
126             =back
127              
128             =head1 HOOKS
129              
130             This plugin makes use of the following hooks:
131              
132             =head2 before
133              
134             Add current page to history. See L and L.
135              
136             =head2 before_template_render
137              
138             Puts history into the token C.
139              
140             =cut
141              
142             has add_all_pages => (
143             is => 'ro',
144             isa => Bool,
145             from_config => sub { 0 },
146             );
147              
148             has ignore_ajax => (
149             is => 'ro',
150             isa => Bool,
151             from_config => sub { 0 },
152             );
153              
154             has history_name => (
155             is => 'ro',
156             isa => Str,
157             from_config => sub { 'page_history' },
158             );
159              
160             has page_set_args => (
161             is => 'ro',
162             isa => HashRef,
163             from_config => 'PageSet',
164             default => sub { +{} },
165             );
166              
167             plugin_keywords 'add_to_history', 'history';
168              
169             sub BUILD {
170 3     3 0 5850 my $plugin = shift;
171              
172             $plugin->app->add_hook(
173             Dancer2::Core::Hook->new(
174             name => 'before',
175             code => sub {
176              
177             return
178 18 100 66 18   216204 if ( !$plugin->add_all_pages
      33        
179             || ( $plugin->ignore_ajax
180             && $plugin->app->request->is_ajax ) );
181              
182 14         3399 $plugin->add_to_history;
183             },
184             )
185 3         77 );
186              
187             $plugin->app->add_hook(
188             Dancer2::Core::Hook->new(
189             name => 'before_template_render',
190             code => sub {
191 0     0   0 my $tokens = shift;
192 0         0 $tokens->{$plugin->history_name} = $plugin->history;
193             },
194             )
195 3         1353 );
196             };
197              
198             sub add_to_history {
199 16     16 1 393 my ( $plugin, @args ) = @_;
200              
201 16         36 my $history = $plugin->history;
202              
203 16         1298 $history->add( request => $plugin->app->request, @args );
204              
205 16         327 $plugin->app->session->write(
206             $plugin->history_name => unbless( $history->pages ) );
207             }
208              
209             sub history {
210 18     18 1 1911 my $plugin = shift;
211              
212             return Dancer2::Plugin::PageHistory::PageSet->new(
213 18   100     19 %{ $plugin->page_set_args },
  18         249  
214             pages => $plugin->app->session->read( $plugin->history_name ) || {},
215             );
216             }
217              
218             =head1 TODO
219              
220             =over
221              
222             =item * Add more tests
223              
224             =back
225              
226             =head1 AUTHOR
227              
228             Peter Mottram (SysPete), C<< >>
229              
230             =head1 CONTRIBUTORS
231              
232             Slaven Rezić (eserte) - GH issues #1, #2, #3
233             Andreas J. König (andk) - GH issue #4
234              
235             =head1 BUGS
236              
237             Please report any bugs or feature requests via the project's GitHub
238             issue tracker:
239              
240             L
241              
242             I will be notified, and then you'll automatically be notified of
243             progress on your bug as I make changes. PRs are always welcome.
244              
245             =head1 SUPPORT
246              
247             You can find documentation for this module with the perldoc command.
248              
249             perldoc Dancer2::Plugin::PageHistory
250              
251             You can also look for information at:
252              
253             =over 4
254              
255             =item * L
256              
257             =item * L
258              
259             =back
260              
261             =head1 LICENSE AND COPYRIGHT
262              
263             Copyright 2015-2016 Peter Mottram (SysPete).
264              
265             This program is free software; you can redistribute it and/or modify
266             it under the same terms as the Perl 5 programming language system itself.
267              
268             See http://dev.perl.org/licenses/ for more information.
269              
270             =cut
271              
272             1; # End of Dancer2::Plugin::PageHistory