File Coverage

blib/lib/Mojolicious/Plugin/Restify/OtherActions.pm
Criterion Covered Total %
statement 19 19 100.0
branch 2 4 50.0
condition n/a
subroutine 3 3 100.0
pod 1 1 100.0
total 25 27 92.5


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Restify::OtherActions;
2              
3 1     1   25231 use Mojo::Base 'Mojolicious::Plugin::Restify';
  1         3  
  1         7  
4              
5             sub register() {
6 1     1 1 43 my $s = shift;
7 1         3 my ($app, $conf) = @_;
8 1         6 $s->SUPER::register(@_);
9 1         996 my $original_code = $app->routes->shortcuts->{collection};
10             $app->routes->add_shortcut(
11             # replace original shortcut
12             collection => sub {
13 1     1   785 my $coll = $original_code->(@_);
14 1         2385 my $r = shift;
15 1         2 my $path = shift;
16 1 50       5 my $options = ref $_[0] eq 'HASH' ? shift : {@_};
17 1         3 my $rname = $options->{route_name};
18 1         4 my $or = $r->find($rname);
19 1 50       185 if ($or->to_string =~ /:${rname}_id/) {
20             # M::P::Restify give same name to /accounts and /accounts/:accounts_id
21             # if I'm here my proposed patch
22             # https://github.com/kwakwaversal/mojolicious-plugin-restify/pull/19
23             # has not been still accepted, so I rename :accounts_id route
24 1         84 $or->name("${rname}_id");
25             # and find route again to match /accounts
26 1         48 $or = $r->find("$options->{route_name}")
27             }
28             $or->get("list/:query/*opt")->to(action => 'list',
29 1         143 opt => undef)->name($options->{route_name} . "_otheractions");
30 1         410 return $coll;
31              
32             }
33 1         10 );
34             }
35              
36             1;
37              
38             =pod
39              
40             =head1 NAME
41              
42             Mojolicious::Plugin::Restify::OtherActions - Mojolicious plug-in which extends Restify with more actions
43              
44             =for html

GitHub last commit Travis tests

45              
46             =head1 VERSION
47              
48             version 0.03
49              
50             =head1 SYNOPSIS
51              
52             plugin 'Restify::OtherActions';
53              
54             =head1 DESCRIPTION
55              
56             Extends L allowing to call other methods over REST collection
57              
58             =encoding UTF-8
59              
60             =head1 USAGE
61              
62             When you create your controller (see L documentation),
63             you can use, as an example, this list method
64              
65             sub list {
66             my $c = shift;
67             my $query = $c->stash('query');
68             return $c->$query if ($query);
69             ...your original list code ...
70             }
71              
72             to redirect your call to an alternative C<$query> method.
73              
74             As an example, if your endpoint is C then C
75             is redirect to C<< $c->my_method >> and remaining url is available in C<< $c->stash->('opt') >>.
76              
77             In addition to standard routes added by L, a new route is added
78              
79             # Pattern Methods Name Class::Method Name
80             # ------- ------- ---- ------------------
81             # ....
82             # +/list/:query/*opt GET accounts_otheractions Accounts::list
83              
84             =head1 Notes about Mojolicious::Plugin::Restify
85              
86             This module extends L but solves also a little bug in route naming.
87              
88             In L /accounts and /accounts/:accounts_id have the same name (accounts).
89              
90             This module replace the second route appending "_id" so that in original module where there is
91              
92             # Pattern Methods Name Class::Method Name
93             # ------- ------- ---- ------------------
94             # ...
95             # +/:accounts_id * "accounts"
96              
97             here there is
98              
99             # +/:accounts_id * "accounts_id".
100              
101             There is a pull request in github repository for this little problem
102              
103             L
104              
105             =head1 BUGS/CONTRIBUTING
106              
107             Please report any bugs through the web interface at L
108             If you want to contribute changes or otherwise involve yourself in development, feel free to fork the Git repository from
109             L.
110              
111             =head1 SUPPORT
112              
113             You can find this documentation with the perldoc command too.
114              
115             perldoc Mojolicious::Plugin::Restify::OtherActions
116              
117             =head1 AUTHOR
118              
119             Emiliano Bruni
120              
121             =head1 COPYRIGHT AND LICENSE
122              
123             This software is copyright (c) 2021 by Emiliano Bruni.
124              
125             This is free software; you can redistribute it and/or modify it under
126             the same terms as the Perl 5 programming language system itself.
127              
128             =cut
129              
130             __END__