| 1 | package Catalyst::Plugin::SimpleMessage; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | { | ||||||||||||||||||||||||||||
| 4 | 3 | $Catalyst::Plugin::SimpleMessage::VERSION = '0.0.2'; | |||||||||||||||||||||||||||
| 5 | } | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use strictures 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Catalyst::Plugin::SimpleMessage - Handle passing multiple types of messages between screens of a web application using session or stash. | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | In MyApp.pm: | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | use Catalyst qr/ | ||||||||||||||||||||||||||||
| 19 | SimpleMessage | ||||||||||||||||||||||||||||
| 20 | /; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | In controller where you want to save a message for display on the next page (here, once the "delete" action taken is complete, we are redirecting to a "list" page to show the status [we don't want to leave the delete action in the browser URL]): | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | $c->response->redirect($c->uri_for($self->action_for('list'), | ||||||||||||||||||||||||||||
| 25 | { $c->sm_get_token_param() => $c->sm_session({ message => 'Deleted widget', type => 'success' }) })); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Or, to display an error message: | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | $c->response->redirect($c->uri_for($self->action_for('list'), | ||||||||||||||||||||||||||||
| 30 | { $c->sm_get_token_param() => $c->sm_session({ message => 'Error deleting widget', type => 'danger' }) })); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | If you do not need a redirect, you can pass the message using stash: | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | $c->sm_stash({ message => 'Your email was not confirmed yet', type => 'warning' }); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | And, to display the output (here using L<Template Toolkit|Template> and L<Twitter Bootstrap|http://getbootstrap.com/>): | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | ... | ||||||||||||||||||||||||||||
| 39 | [% FOREACH item IN c.sm_get() %] | ||||||||||||||||||||||||||||
| 40 | <span class="alert alert-[% item.type %]">[% item.message %]</span> | ||||||||||||||||||||||||||||
| 41 | [% END %] | ||||||||||||||||||||||||||||
| 42 | ... | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | There are a number of ways people commonly use to pass messages between screens in a web application. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =over 4 | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =item * | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | Using $c->flash: The "flash" feature does provide a mechanism where the application can redirect to an appropriate URL, but it can also lead to a race condition where the wrong status message is displayed in the wrong browser window or tab (and can therefore be confusing to the users of your application). | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =item * | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | Query parameters in the URL: This suffers from issues related to long/ugly URLs and leaves the message displayed even after a browser refresh. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =back | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | This plugin attempts to address these issues through the following mechanisms: | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =over 4 | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =item * | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | Stores messages in the C<$c-E<gt>session> so that the application is free to redirect to the appropriate URL after an action is taken. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =item * | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | Associates a random 8-digit "token" with each message, so it's completely unambiguous what message should be shown in each window/tab. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =item * | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | Only requires that the token (not the full message) be included in the redirect URL. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =item * | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Automatically removes the message after the first time it is displayed. That way, if users hit refresh in their browsers they only see the messages the first time. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =back | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head1 METHODS | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head2 sm_get_messages | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Returns an array ref with messages sent through C<$c-E<gt>sm_session> and C<$c-E<gt>sm_stash>. You can send messages through C<$c-E<gt>sm_session> and C<$c-E<gt>sm_stash> and both will be available, messages sent throught stash are available only for the current request. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head2 sm_get | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Alias for sm_get_messages | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 14 | ▶ | sub sm_get_messages { | ||||||||||||||||||||||||||
|
Subroutine
sm_get_messages: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 14 | my ($self) = @_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 14 | my $conf = $self->__sm_config(); | |||||||||||||||||||||||||||
| 102 | 14 | ▶ | my $token = $self->request->param($conf->{token_param}) || undef; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->request->param($conf->{'token_param'}) || undef
MC/DC100%1/1
$self->request->param($conf->{'token_param'}) || undef
$self->request->param($conf->{'token_param'})
Truth table100%2/2
A
A= $self->request->param($conf->{'token_param'})
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # if stash key must be an array | ||||||||||||||||||||||||||||
| 105 | 14 | ▶ | $self->stash->{$conf->{stash_prefix}} = [] if(ref($self->stash->{$conf->{stash_prefix}}) ne 'ARRAY'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 14 | ▶ | if($token && (ref($self->session->{$conf->{session_prefix}}->{$token}) eq 'ARRAY')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$token and ref $self->session->{$conf->{"session_prefix"}}{$token} eq "ARRAY"
MC/DC100%2/2
$token and ref $self->session->{$conf->{"session_prefix"}}{$token} eq "ARRAY"
$token
ref $self->session->{$conf->{"session_prefix"}}{$token} eq "ARRAY"
Truth table100%3/3
A and B
A= $tokenB= ref $self->session->{$conf->{"session_prefix"}}{$token} eq "ARRAY"
| |||||||||||||||||||||||||||||
| 108 | 4 | my $searray = delete $self->session->{$conf->{session_prefix}}->{$token}; | |||||||||||||||||||||||||||
| 109 | 4 | push(@{ $self->stash->{$conf->{stash_prefix}} }, @$searray); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 14 | return $self->stash->{$conf->{stash_prefix}}; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 3 | *sm_get = \&sm_get_messages; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head2 sm_get_token_param | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Return the token_param name. Default is "__sm" | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =cut | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 4 | ▶ | sub sm_get_token_param { | ||||||||||||||||||||||||||
|
Subroutine
sm_get_token_param: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 125 | 4 | return $self->__sm_config()->{token_param}; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head2 sm_set_messages_session | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | Send messages through the session and return a token. You can send multiple messages: | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | my $token = $c->sm_session({ message => 'Product added with success', type => 'success' }, { message => 'You must define a price before selling', type => 'warning' }); | ||||||||||||||||||||||||||||
| 134 | $c->response->redirect($c->uri_for($self->action_for('list'), { $c->sm_get_token_param() => $token })); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head2 sm_session | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Alias for sm_set_messages_session | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =cut | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 4 | ▶ | sub sm_set_messages_session { | ||||||||||||||||||||||||||
|
Subroutine
sm_set_messages_session: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 4 | my ($self, @messages) = @_; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 4 | my $conf = $self->__sm_config(); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 4 | my $token = int(rand(90_000_000))+10_000_000; | |||||||||||||||||||||||||||
| 148 | 4 | ▶ | $self->session->{$conf->{session_prefix}} = {} if(ref($self->session->{$conf->{session_prefix}}) ne 'HASH'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 4 | $self->session->{$conf->{session_prefix}}->{$token} = \@messages; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 4 | return $token; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 3 | *sm_session = \&sm_set_messages_session; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head2 sm_set_messages_stash | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Send messages through the stash. You can send multiple messages: | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | my $token = $c->sm_stash({ message => 'You account is active', type => 'info' }, { message => 'You do not have credit', type => 'danger' }); | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Messages sent through stash will be available only for the current request. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head2 sm_stash | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Alias for sm_set_messages_stash | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 4 | ▶ | sub sm_set_messages_stash { | ||||||||||||||||||||||||||
|
Subroutine
sm_set_messages_stash: called CC=2 SCAR=7.9
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 4 | my ($self, @messages) = @_; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 4 | my $conf = $self->__sm_config(); | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 4 | ▶ | if(ref($self->stash->{$conf->{stash_prefix}}) ne 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 4 | $self->stash->{$conf->{stash_prefix}} = \@messages; | |||||||||||||||||||||||||||
| 177 | } else { | ||||||||||||||||||||||||||||
| 178 | 0 | push(@{ $self->stash->{$conf->{stash_prefix}} }, @messages); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | 3 | *sm_stash = \&sm_set_messages_stash; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head1 CONFIGURABLE OPTIONS | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head2 session_prefix | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | The location inside $c->session where messages will be stored. Defaults to "C<__sm>". | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =head2 stash_prefix | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | The location inside $c->stash where messages will be stored. Defaults to "C<__sm>". | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head2 token_param | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | The name of the URL param that holds the token on the page where you want to retrieve/display the status message. Defaults to "C<__sm>". | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head2 Configuration Example | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | Here is a quick example showing how Catalyst::Plugin::SimpleMessage can be configured | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | # Configure Catalyst::Plugin::SimpleMessage | ||||||||||||||||||||||||||||
| 210 | __PACKAGE__->config( | ||||||||||||||||||||||||||||
| 211 | 'Plugin::SimpleMessage' => { | ||||||||||||||||||||||||||||
| 212 | session_prefix => '__sm', | ||||||||||||||||||||||||||||
| 213 | stash_prefix => '__sm', | ||||||||||||||||||||||||||||
| 214 | token_param => '__sm' | ||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | ); | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =head1 INTERNALS | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | Note: You normally shouldn't need any of the information in this section to use L<Catalyst::Plugin::SimpleMessage>. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =head2 __sm_config | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | Handles default values for config and lets them be overriden from the MyApp configuration. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =cut | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 26 | ▶ | sub __sm_config { | ||||||||||||||||||||||||||
|
Subroutine
__sm_config: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 231 | 26 | my ($self) = @_; | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | my %config = ( | ||||||||||||||||||||||||||||
| 234 | session_prefix => '__sm', | ||||||||||||||||||||||||||||
| 235 | stash_prefix => '__sm', | ||||||||||||||||||||||||||||
| 236 | token_param => '__sm', | ||||||||||||||||||||||||||||
| 237 | 26 | ▶ | %{ $self->config->{'Plugin::SimpleMessage'} || {} } | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->config->{'Plugin::SimpleMessage'} || {}
MC/DC100%1/1
$self->config->{'Plugin::SimpleMessage'} || {}
$self->config->{'Plugin::SimpleMessage'}
Truth table100%2/2
A
A= $self->config->{'Plugin::SimpleMessage'}
| |||||||||||||||||||||||||||||
| 238 | ); | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 26 | return \%config; | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | }; | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =head1 CODE | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | Github | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | https://github.com/geovannyjs/Catalyst-Plugin-SimpleMessage | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | Geovanny Junio, geovannyjs@gmail.com | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | This module was based on L<Catalyst::Plugin::StatusMessage>. Many thanks to Kennedy Clark (HKCLARK). | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | =cut | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 3 | 1; | |||||||||||||||||||||||||||