Skip to content

Commit

Permalink
provide under_controller when under is given
Browse files Browse the repository at this point in the history
  • Loading branch information
omani committed Nov 9, 2014
1 parent 67c21f3 commit 1ea0e47
Showing 1 changed file with 42 additions and 13 deletions.
55 changes: 42 additions & 13 deletions lib/Mojolicious/Plugin/REST.pm
@@ -1,7 +1,7 @@
package Mojolicious::Plugin::REST; package Mojolicious::Plugin::REST;


# ABSTRACT: Mojolicious Plugin for RESTful operations # ABSTRACT: Mojolicious Plugin for RESTful operations
# VERSION our $VERSION = '0.006'; # VERSION
use Mojo::Base 'Mojolicious::Plugin'; use Mojo::Base 'Mojolicious::Plugin';
use Mojo::Exception; use Mojo::Exception;
use Lingua::EN::Inflect 1.895 qw/PL/; use Lingua::EN::Inflect 1.895 qw/PL/;
Expand Down Expand Up @@ -83,8 +83,13 @@ sub register {


# under setting # under setting
my $under_name = $params->{under}; my $under_name = $params->{under};
# fix controller name for under if under defined (hasan)
my $under_controller = $params->{under_controller};

my ( $under_name_lower, $under_name_plural, $under_id ); my ( $under_name_lower, $under_name_plural, $under_id );
if ( defined($under_name) and $under_name ne '' ) { if ( defined($under_name) and $under_name ne '' ) {
# throw exeption if under was defined but no controller name for that under was given (hasan)
Mojo::Exception->throw('Under name was given but no controller name for it.') unless defined $params->{under_controller};
$under_name_lower = lc $under_name; $under_name_lower = lc $under_name;
$under_name_plural = PL( $under_name_lower, 10 ); $under_name_plural = PL( $under_name_lower, 10 );
$under_id = ":" . $under_name_lower . "Id"; $under_id = ":" . $under_name_lower . "Id";
Expand All @@ -110,7 +115,8 @@ sub register {
my $action = $http2crud->{collection}->{$collection_method} . $action_suffix; my $action = $http2crud->{collection}->{$collection_method} . $action_suffix;


if ( defined($under_name) ) { if ( defined($under_name) ) {
my $bridge_controller = ucfirst($under_name_lower); #my $bridge_controller = ucfirst($under_name_lower);
my $bridge_controller = $under_controller;
my $bridge my $bridge
= $routes->bridge($url)->to( controller => $bridge_controller, action => $method_chained ) = $routes->bridge($url)->to( controller => $bridge_controller, action => $method_chained )
->name("${bridge_controller}::${method_chained}()") ->name("${bridge_controller}::${method_chained}()")
Expand Down Expand Up @@ -154,7 +160,8 @@ sub register {
my $action = $http2crud->{resource}->{$resource_method} . $action_suffix; my $action = $http2crud->{resource}->{$resource_method} . $action_suffix;


if ( defined($under_name) ) { if ( defined($under_name) ) {
my $bridge_controller = ucfirst($under_name_lower); #my $bridge_controller = ucfirst($under_name_lower);
my $bridge_controller = $under_controller;
my $bridge my $bridge
= $routes->bridge($url)->to( controller => $bridge_controller, action => $method_chained ) = $routes->bridge($url)->to( controller => $bridge_controller, action => $method_chained )
->name("${bridge_controller}::${method_chained}()") ->name("${bridge_controller}::${method_chained}()")
Expand Down Expand Up @@ -182,6 +189,14 @@ __END__
=pod =pod
=head1 NAME
Mojolicious::Plugin::REST - Mojolicious Plugin for RESTful operations
=head1 VERSION
version 0.006
=head1 SYNOPSIS =head1 SYNOPSIS
# In Mojolicious Application # In Mojolicious Application
Expand Down Expand Up @@ -276,17 +291,17 @@ Following options can be used to control route creation:
=item methods =item methods
This option can be used to control which methods are created for declared rest_route. Each character in the value of this option, This option can be used to control which methods are created for declared rest_route. Each character in the value of this option,
determines if corresponding route will be created or ommited. For Example: determined if corresponding route will be created or ommited. For Example:
$routes->rest_routes( name => 'Account', methods => 'crudl' ); $routes->rest_routes( name => 'Account', methods => 'crudl' );
This will install all the rest routes, value C<crudl> signifies: This will install all the rest routes, value 'crudl' signifies:
c - create c - create
r - read r - read
u - update u - update
d - delete d - delete
l - list l - list.
Only methods whose first character is mentioned in the value for this option will be created. For Example: Only methods whose first character is mentioned in the value for this option will be created. For Example:
Expand All @@ -298,9 +313,7 @@ This will install only create, read and delete routes as below:
# /api/v1/accounts/:accountId .... DELETE "Account::delete_account()" ^/api/v1/accounts/([^\/\.]+)(?:\.([^/]+)$)? # /api/v1/accounts/:accountId .... DELETE "Account::delete_account()" ^/api/v1/accounts/([^\/\.]+)(?:\.([^/]+)$)?
# /api/v1/accounts/:accountId .... GET "Account::read_account()" ^/api/v1/accounts/([^\/\.]+)(?:\.([^/]+)$)? # /api/v1/accounts/:accountId .... GET "Account::read_account()" ^/api/v1/accounts/([^\/\.]+)(?:\.([^/]+)$)?
option value 'crd' signifies,
Option value C<crd> signifies:
c - create, c - create,
r - read, r - read,
d - delete d - delete
Expand All @@ -327,11 +340,11 @@ If customized, this options needs a full namespace of the controller class.
=item under =item under
This option can be used for associations. If present, url's for named resource will be created under given under resource. The actions created, This option can be used for associations. If present, url's for named resource will be created under given under resource. The actions created,
will be bridged under C<method_chained> method of given under resouce. For Example: will be bridged under 'method_chained' method of given under resouce. For Example:
$routes->rest_routes( name => 'Feature', under => 'Account' ); $routes->rest_routes( name => 'Feature', under => 'Account' );
This will create following routes, where routes for feature are bridged under C<Account::chained()> # will create following routes, where routes for feature are bridged under Account::chained()
# /api/v1/accounts/:accountId/features B... * "Account::chained()" ^/api/v1/accounts/([^\/\.]+)/features # /api/v1/accounts/:accountId/features B... * "Account::chained()" ^/api/v1/accounts/([^\/\.]+)/features
# +/ .... GET "Feature::list_account_feature()" ^(?:\.([^/]+)$)? # +/ .... GET "Feature::list_account_feature()" ^(?:\.([^/]+)$)?
Expand All @@ -347,7 +360,6 @@ This will create following routes, where routes for feature are bridged under C<
Note that, The actual bridge code needs to return a true value or the dispatch chain will be broken. Please refer Note that, The actual bridge code needs to return a true value or the dispatch chain will be broken. Please refer
L<Mojolicious Bridges Documentation|https://metacpan.org/pod/Mojolicious::Guides::Routing#Bridges> for more information on bridges in Mojolicious. L<Mojolicious Bridges Documentation|https://metacpan.org/pod/Mojolicious::Guides::Routing#Bridges> for more information on bridges in Mojolicious.
=item types =item types
This option can be used to specify types of resources available in application. This option can be used to specify types of resources available in application.
Expand All @@ -370,7 +382,7 @@ If present, this value will be added as prefix to all routes created.
If present, this value will be added as prefix to all routes created but after prefix. If present, this value will be added as prefix to all routes created but after prefix.
=item http2crud =item htt2crud
If present, given HTTP to CRUD mapping will be used to determine method names. Default mapping: If present, given HTTP to CRUD mapping will be used to determine method names. Default mapping:
Expand All @@ -388,3 +400,20 @@ If present, given HTTP to CRUD mapping will be used to determine method names. D
} }
=back =back
=head1 AUTHOR
Abhishek Shende <abhishekisnot@gmail.com>
=head1 CONTRIBUTOR
Vincent HETRU <vincent.hetru@13pass.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Abhishek Shende.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut

0 comments on commit 1ea0e47

Please sign in to comment.