File Coverage

blib/lib/Dancer/Plugin/PageHistory.pm
Criterion Covered Total %
statement 37 37 100.0
branch 5 6 83.3
condition 2 6 33.3
subroutine 7 7 100.0
pod n/a
total 51 56 91.0


line stmt bran cond sub pod time code
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             our $VERSION = '0.102';
14              
15 2     2   303840 use Dancer ':syntax';
  2         356118  
  2         14  
16 2     2   2120 use Dancer::Plugin;
  2         2385  
  2         142  
17 2     2   590 use Dancer::Plugin::PageHistory::PageSet;
  2         5  
  2         125  
18 2     2   1358 use Dancer::Plugin::PageHistory::Page;
  2         6  
  2         79  
19 2     2   1804 use Data::Structure::Util qw/unbless/;
  2         16393  
  2         1217  
20              
21             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 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 keyword.
43              
44             =head1 KEYWORDS
45              
46             =head2 add_to_history
47              
48             Adds a page via L. Both of
49             L and
50             L 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 object from the
57             user's session.
58              
59             =head1 SUPPORTED SESSION ENGINES
60              
61             L,
62             L,
63             L,
64             L,
65             L,
66             L,
67             L,
68             L,
69             L,
70             L,
71             L
72              
73             =head1 CAVEATS
74              
75             L and L 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.
80              
81             =head1 CONFIGURATION
82              
83             No configuration is necessarily required.
84              
85             If you wish to have arguments passed to
86             L 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 in the L hook.
112              
113             =item * ignore_ajax
114              
115             If L is true this controls whether ajax requests are added to
116             the list L in the
117             L 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 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 and L.
138              
139             =cut
140              
141             hook before => sub {
142             my $conf = plugin_setting;
143             return
144             if ( !$conf->{add_all_pages}
145             || ( $conf->{ignore_ajax} && request->is_ajax ) );
146             add_to_history();
147             };
148              
149             =head2 before_template_render
150              
151             Puts history into the token C.
152              
153             =cut
154              
155             hook before_template_render => sub {
156             my $tokens = shift;
157             my $name = plugin_setting->{history_name} || $history_name;
158             $tokens->{$name} = history();
159             };
160              
161             sub add_to_history {
162 12   33 12   798004 my $name = plugin_setting->{history_name} || $history_name;
163 12         431 my ( $self, @args ) = plugin_args(@_);
164              
165 12         71 my $path = request->path;
166 12         155 my $query = params('query');
167              
168 12         2295 my %args = (
169             path => $path,
170             query => $query,
171             @args,
172             );
173              
174 12         53 debug "adding page to history: ", \%args;
175              
176 12         115 my $history = history();
177              
178             # add the page and save back to session with pages all unblessed
179 12         72 $history->add( %args );
180 12         344 session $name => unbless( $history->pages );
181             }
182              
183             sub history {
184 12     12   33 my $conf = plugin_setting;
185 12   33     378 my $name = $conf->{history_name} || $history_name;
186 12         25 my $history;
187              
188 12 100       49 if ( defined var($name) ) {
189 2         37 $history = var($name);
190             }
191             else {
192              
193 10         121 my $session_history = session($name);
194 10 100       31140 $session_history = {} unless ref($session_history) eq 'HASH';
195              
196 10 50       33 my %args = $conf->{PageSet} ? %{ $conf->{PageSet} } : ();
  10         45  
197 10         25 $args{pages} = $session_history;
198              
199 10         307 $history = Dancer::Plugin::PageHistory::PageSet->new(%args);
200 10         393 var $name => $history;
201             }
202              
203 12         144 return $history;
204             }
205              
206             register add_to_history => \&add_to_history;
207              
208             register history => \&history;
209              
210             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 problems with L
223             and L
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
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
252              
253             =item * L
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             1; # End of Dancer::Plugin::PageHistory