File Coverage

lib/CatalystX/Resource.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package CatalystX::Resource;
2             $CatalystX::Resource::VERSION = '0.03';
3 9     9   18586938 use Moose::Role;
  9         89  
  9         88  
4 9     9   56292 use CatalystX::InjectComponent;
  9         40052  
  9         307  
5 9     9   74 use namespace::autoclean;
  9         32  
  9         94  
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.03
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             =head3 default
82              
83             =over
84              
85             =item List ... GET /<resource>/list
86              
87             =item Show ... GET /<resource>/*/show
88              
89             =item Delete ... POST /<resource>/*/delete
90              
91             =item Form
92              
93             =item Create ... GET|POST /<resource>/create
94              
95             =item Edit ... GET|POST /<resource>/*/edit
96              
97             =back
98              
99             =head3 optional
100              
101             =over
102              
103             =item MergeUploadParams
104              
105             =item Sortable
106              
107             POST /<resource>/*/move_next
108             POST /<resource>/*/move_previous
109             POST /<resource>/*/move_to
110              
111             =back
112              
113             You can remove actions if you don't need them.
114              
115             Example, you don't need the edit action:
116             'Controller::Resource::Artist' => {
117             ...,
118             traits => ['-Edit'],
119             },
120              
121             Use the Sortable trait to make your resources sortable:
122             'Controller::Resource::Artist' => {
123             ...,
124             traits => ['Sortable'],
125             },
126              
127             =head1 CONFIG
128              
129             =head2 controllers
130              
131             array ref of controller names which will be injected into your app
132              
133             =head2 error_path
134              
135             CatalystX::Resource detaches to $self->error_path if a resource cannot be found.
136             Make sure you implement this action in your App. (default: '/default')
137              
138             =head1 CAVEAT
139              
140             =head2 Using Moose Method Modifiers on your Resource Controller
141              
142             If you want to apply Method Modifiers to a resource controller you have to
143             subclass from CatalystX::Resource::Controller::Resource and apply the roles in
144             a BEGIN block.
145              
146             The following example loads the B<List> trait via B<with> in order to apply a
147             B<before> method modifier.
148              
149             package MyApp::Controller::Foo;
150             use Moose;
151             use namespace::autoclean;
152              
153             __PACKAGE__->config(
154             resultset_key => 'artists',
155             resource_key => 'artist',
156             form_class => 'TestApp::Form::Resource::Artist',
157             model => 'DB::Resource::Artist',
158             traits => [qw/ MergeUploadParams -Delete /],
159             error_path => '/error',
160             actions => {
161             base => {
162             PathPart => 'artists',
163             },
164             },
165             );
166              
167             BEGIN {
168             extends 'CatalystX::Resource::Controller::Resource';
169             with 'CatalystX::Resource::TraitFor::Controller::Resource::List';
170             with 'CatalystX::Resource::TraitFor::Controller::Resource::Show';
171             with 'CatalystX::Resource::TraitFor::Controller::Resource::Form';
172             with 'CatalystX::Resource::TraitFor::Controller::Resource::Create';
173             with 'CatalystX::Resource::TraitFor::Controller::Resource::Edit';
174             with 'CatalystX::Resource::TraitFor::Controller::Resource::Sortable';
175             }
176              
177             before 'list' => sub { ... }
178              
179             1;
180              
181             Because of a bug in L<MooseX::MethodAttributes>
182             L<CatalystX::Resource::TraitFor::Controller::Resource::MergeUploadParams> is not
183             applied correctly if you include it via C<with> in the C<BEGIN> block of the
184             subclassed controller.
185              
186             Including it via C<traits =E<gt> ['MergeUploadParams']> works around this.
187              
188             MergeUploadParams is different from the other roles. The other roles add a subroutine
189             whereas MergeUploadParams uses a Moose Method Modifier.
190              
191             =head1 SEE ALSO
192              
193             Check out L<Catalyst::Controller::DBIC::API> if you want to provide your data
194             as a web service.
195              
196             =head1 AUTHOR
197              
198             David Schmidt <davewood@cpan.org>
199              
200             =head1 COPYRIGHT AND LICENSE
201              
202             This software is copyright (c) 2011 by David Schmidt.
203              
204             This is free software; you can redistribute it and/or modify it under
205             the same terms as the Perl 5 programming language system itself.
206              
207             =cut