« lib/CatalystX/Resource.pm
↑ Summary
blib/lib/CatalystX/Resource/TraitFor/Controller/Resource/Create.pm »
| 1 | package CatalystX::Resource; | ||
| 2 | $CatalystX::Resource::VERSION = '0.009_002'; | ||
| 3 | use Moose::Role; | ||
| 4 | use CatalystX::InjectComponent; | ||
| 5 | use namespace::autoclean; | ||
| 6 | |||
| 7 | # ABSTRACT: Provide CRUD functionality to your Controllers | ||
| 8 | |||
| 9 | |||
| 10 | after 'setup_components' => sub { | ||
| 11 | my $class = shift; | ||
| 12 | |||
| 13 | my $config = $class->config->{'CatalystX::Resource'}; | ||
| 14 | my $controllers = $config->{controllers}; | ||
| 15 | |||
| 16 | for my $controller (@$controllers) { | ||
| 17 | my $controller_name = 'Controller::' . $controller; | ||
| 18 | $class->config->{$controller_name}{error_path} = $config->{error_path} | ||
| 19 | if exists $config->{error_path}; | ||
| 20 | CatalystX::InjectComponent->inject( | ||
| 21 | into => $class, | ||
| 22 | component => 'CatalystX::Resource::Controller::Resource', | ||
| 23 | as => $controller_name, | ||
| 24 | ); | ||
| 25 | } | ||
| 26 | }; | ||
| 27 | |||
| 28 | 1; | ||
| 29 | |||
| 30 | __END__ | ||
| 31 | |||
| 32 | =pod | ||
| 33 | |||
| 34 | =encoding UTF-8 | ||
| 35 | |||
| 36 | =head1 NAME | ||
| 37 | |||
| 38 | CatalystX::Resource - Provide CRUD functionality to your Controllers | ||
| 39 | |||
| 40 | =head1 VERSION | ||
| 41 | |||
| 42 | version 0.009_002 | ||
| 43 | |||
| 44 | =head1 SYNOPSIS | ||
| 45 | |||
| 46 | use Catalyst qw/ | ||
| 47 | +CatalystX::Resource | ||
| 48 | /; | ||
| 49 | |||
| 50 | __PACKAGE__->config( | ||
| 51 | 'Controller::Resource::Artist' => { | ||
| 52 | resultset_key => 'artists', | ||
| 53 | resource_key => 'artist', | ||
| 54 | form_class => 'TestApp::Form::Resource::Artist', | ||
| 55 | model => 'DB::Resource::Artist', | ||
| 56 | error_path => '/error', | ||
| 57 | actions => { | ||
| 58 | base => { | ||
| 59 | PathPart => 'artists', | ||
| 60 | }, | ||
| 61 | }, | ||
| 62 | }, | ||
| 63 | 'CatalystX::Resource' => { | ||
| 64 | controllers => [ qw/ Artist / ], | ||
| 65 | }, | ||
| 66 | ); | ||
| 67 | |||
| 68 | =head1 DESCRIPTION | ||
| 69 | |||
| 70 | CatalystX::Resource enhances your App with CRUD functionality. | ||
| 71 | |||
| 72 | After creating files for HTML::FormHandler, DBIx::Class | ||
| 73 | and Template Toolkit templates you get create/edit/delete/show/list | ||
| 74 | actions for free. | ||
| 75 | |||
| 76 | Resources can be nested. | ||
| 77 | (e.g.: Artist has_many Albums) | ||
| 78 | |||
| 79 | =head2 traits | ||
| 80 | |||
| 81 | default traits: | ||
| 82 | List ... GET /<resource>/list | ||
| 83 | Show ... GET /<resource>/*/show | ||
| 84 | Delete ... POST /<resource>/*/delete | ||
| 85 | Form | ||
| 86 | Create ... GET|POST /<resource>/create | ||
| 87 | Edit ... GET|POST /<resource>/*/edit | ||
| 88 | |||
| 89 | optional traits: | ||
| 90 | MergeUploadParams | ||
| 91 | Sortable | ||
| 92 | POST /<resource>/*/move_next | ||
| 93 | POST /<resource>/*/move_previous | ||
| 94 | POST /<resource>/*/move_to | ||
| 95 | |||
| 96 | You can remove actions if you don't need them. | ||
| 97 | |||
| 98 | Example, you don't need the edit action: | ||
| 99 | 'Controller::Resource::Artist' => { | ||
| 100 | ..., | ||
| 101 | traits => ['-Edit'], | ||
| 102 | }, | ||
| 103 | |||
| 104 | Use the Sortable trait to make your resources sortable: | ||
| 105 | 'Controller::Resource::Artist' => { | ||
| 106 | ..., | ||
| 107 | traits => ['Sortable'], | ||
| 108 | }, | ||
| 109 | |||
| 110 | =head1 CONFIG | ||
| 111 | |||
| 112 | =head2 controllers | ||
| 113 | |||
| 114 | array ref of controller names which will be injected into your app | ||
| 115 | |||
| 116 | =head2 error_path | ||
| 117 | |||
| 118 | CatalystX::Resource detaches to $self->error_path if a resource cannot be found. | ||
| 119 | Make sure you implement this action in your App. (default: '/default') | ||
| 120 | |||
| 121 | =head1 CAVEAT | ||
| 122 | |||
| 123 | =head2 Using Moose Method Modifiers on your Resource Controller | ||
| 124 | |||
| 125 | If you want to apply Method Modifiers to a resource controller you have to | ||
| 126 | subclass from CatalystX::Resource::Controller::Resource and apply the roles in | ||
| 127 | a BEGIN block. | ||
| 128 | |||
| 129 | The following example loads the B<List> trait via B<with> in order to apply a | ||
| 130 | B<before> method modifier. | ||
| 131 | |||
| 132 | package MyApp::Controller::Foo; | ||
| 133 | use Moose; | ||
| 134 | use namespace::autoclean; | ||
| 135 | |||
| 136 | __PACKAGE__->config( | ||
| 137 | resultset_key => 'artists', | ||
| 138 | resource_key => 'artist', | ||
| 139 | form_class => 'TestApp::Form::Resource::Artist', | ||
| 140 | model => 'DB::Resource::Artist', | ||
| 141 | traits => [qw/ MergeUploadParams -Delete /], | ||
| 142 | error_path => '/error', | ||
| 143 | actions => { | ||
| 144 | base => { | ||
| 145 | PathPart => 'artists', | ||
| 146 | }, | ||
| 147 | }, | ||
| 148 | ); | ||
| 149 | |||
| 150 | BEGIN { | ||
| 151 | extends 'CatalystX::Resource::Controller::Resource'; | ||
| 152 | with 'CatalystX::Resource::TraitFor::Controller::Resource::List'; | ||
| 153 | with 'CatalystX::Resource::TraitFor::Controller::Resource::Show'; | ||
| 154 | with 'CatalystX::Resource::TraitFor::Controller::Resource::Form'; | ||
| 155 | with 'CatalystX::Resource::TraitFor::Controller::Resource::Create'; | ||
| 156 | with 'CatalystX::Resource::TraitFor::Controller::Resource::Edit'; | ||
| 157 | with 'CatalystX::Resource::TraitFor::Controller::Resource::Sortable'; | ||
| 158 | } | ||
| 159 | |||
| 160 | before 'list' => sub { ... } | ||
| 161 | |||
| 162 | 1; | ||
| 163 | |||
| 164 | Because of a bug in L<MooseX::MethodAttributes> | ||
| 165 | L<CatalystX::Resource::TraitFor::Controller::Resource::MergeUploadParams> is not | ||
| 166 | applied correctly if you include it via C<with> in the C<BEGIN> block of the | ||
| 167 | subclassed controller. | ||
| 168 | |||
| 169 | Including it via C<traits =E<gt> ['MergeUploadParams']> works around this. | ||
| 170 | |||
| 171 | MergeUploadParams is different from the other roles. The other roles add a subroutine | ||
| 172 | whereas MergeUploadParams uses a Moose Method Modifier. | ||
| 173 | |||
| 174 | =head1 SEE ALSO | ||
| 175 | |||
| 176 | Check out L<Catalyst::Controller::DBIC::API> if you want to provide your data | ||
| 177 | as a web service. | ||
| 178 | |||
| 179 | =head1 AUTHOR | ||
| 180 | |||
| 181 | David Schmidt <davewood@cpan.org> | ||
| 182 | |||
| 183 | =head1 COPYRIGHT AND LICENSE | ||
| 184 | |||
| 185 | This software is copyright (c) 2011 by David Schmidt. | ||
| 186 | |||
| 187 | This is free software; you can redistribute it and/or modify it under | ||
| 188 | the same terms as the Perl 5 programming language system itself. | ||
| 189 | |||
| 190 | =cut |