« lib/CatalystX/Resource/TraitFor/Controller/Resource/Form.pm
↑ Summary
lib/CatalystX/Resource/TraitFor/Controller/Resource/MergeUploadParams.pm »
| 1 | package CatalystX::Resource::TraitFor::Controller::Resource::Sortable; | ||
| 2 | $CatalystX::Resource::TraitFor::Controller::Resource::Sortable::VERSION = '0.009_002'; | ||
| 3 | use MooseX::MethodAttributes::Role; | ||
| 4 | use namespace::autoclean; | ||
| 5 | |||
| 6 | # ABSTRACT: makes your resource sortable | ||
| 7 | |||
| 8 | requires qw/ | ||
| 9 | _msg | ||
| 10 | resource_key | ||
| 11 | _redirect | ||
| 12 | /; | ||
| 13 | |||
| 14 | # ABSTRACT: make your Resource sortable | ||
| 15 | |||
| 16 | |||
| 17 | sub move_next : Method('POST') Chained('base_with_id') PathPart('move_next') Args(0) { | ||
| 18 | my ( $self, $c ) = @_; | ||
| 19 | my $resource = $c->stash->{ $self->resource_key }; | ||
| 20 | $resource->move_next; | ||
| 21 | $c->flash( msg => $self->_msg( $c, 'move_next' ) ); | ||
| 22 | $c->res->redirect($c->req->referer || '/'); | ||
| 23 | } | ||
| 24 | |||
| 25 | |||
| 26 | sub move_previous : Method('POST') Chained('base_with_id') PathPart('move_previous') Args(0) { | ||
| 27 | my ( $self, $c ) = @_; | ||
| 28 | my $resource = $c->stash->{ $self->resource_key }; | ||
| 29 | $resource->move_previous; | ||
| 30 | $c->flash( msg => $self->_msg( $c, 'move_previous' ) ); | ||
| 31 | $c->res->redirect($c->req->referer || '/'); | ||
| 32 | } | ||
| 33 | |||
| 34 | |||
| 35 | sub move_to : Method('POST') Chained('base_with_id') PathPart('move_to') Args(0) { | ||
| 36 | my ( $self, $c ) = @_; | ||
| 37 | |||
| 38 | my $pos = $c->req->param('pos'); | ||
| 39 | if (!defined $pos) { | ||
| 40 | $c->stash( error_msg => $self->_msg( $c, 'move_to_undef' ) ); | ||
| 41 | $c->detach( $self->error_path ); | ||
| 42 | } | ||
| 43 | |||
| 44 | my $resource = $c->stash->{ $self->resource_key }; | ||
| 45 | $resource->move_to( $pos ); | ||
| 46 | $c->flash( msg => $self->_msg( $c, 'move_to' ) ); | ||
| 47 | $c->res->redirect($c->req->referer || '/'); | ||
| 48 | } | ||
| 49 | |||
| 50 | 1; | ||
| 51 | |||
| 52 | __END__ | ||
| 53 | |||
| 54 | =pod | ||
| 55 | |||
| 56 | =encoding UTF-8 | ||
| 57 | |||
| 58 | =head1 NAME | ||
| 59 | |||
| 60 | CatalystX::Resource::TraitFor::Controller::Resource::Sortable - makes your resource sortable | ||
| 61 | |||
| 62 | =head1 VERSION | ||
| 63 | |||
| 64 | version 0.009_002 | ||
| 65 | |||
| 66 | =head1 SYNOPSIS | ||
| 67 | |||
| 68 | # TestApp.pm | ||
| 69 | 'Controller::Resource::Artist' => { | ||
| 70 | resultset_key => 'artists', | ||
| 71 | resource_key => 'artist', | ||
| 72 | form_class => 'TestApp::Form::Resource::Artist', | ||
| 73 | model => 'DB::Resource::Artist', | ||
| 74 | redirect_mode => 'list', | ||
| 75 | traits => ['Sortable'], | ||
| 76 | actions => { | ||
| 77 | base => { | ||
| 78 | PathPart => 'artists', | ||
| 79 | }, | ||
| 80 | }, | ||
| 81 | }, | ||
| 82 | |||
| 83 | # TestApp/Schema/Result/Resource/Artist.pm | ||
| 84 | __PACKAGE__->load_components(qw/ Ordered Core /); | ||
| 85 | __PACKAGE__->table('artist'); | ||
| 86 | __PACKAGE__->add_columns( | ||
| 87 | ..., | ||
| 88 | 'position', | ||
| 89 | { | ||
| 90 | data_type => 'integer', | ||
| 91 | is_numeric => 1, | ||
| 92 | is_nullable => 0, | ||
| 93 | }, | ||
| 94 | ); | ||
| 95 | |||
| 96 | __PACKAGE__->resultset_attributes({ order_by => 'position' }); | ||
| 97 | __PACKAGE__->position_column('position'); | ||
| 98 | |||
| 99 | =head1 DESCRIPTION | ||
| 100 | |||
| 101 | adds these paths to your Controller which call move_previous/move_next | ||
| 102 | on your resource item as provided by L<DBIx::Class::Ordered> | ||
| 103 | |||
| 104 | Make sure the schema for your sortable resource has a 'position' column. | ||
| 105 | |||
| 106 | /resource/*/move_previous | ||
| 107 | /resource/*/move_next | ||
| 108 | |||
| 109 | For nested resources you need to set a grouping_column | ||
| 110 | Example: Artist has_many Albums has_many Songs | ||
| 111 | |||
| 112 | # TestApp/Schema/Result/Resource/Song.pm | ||
| 113 | __PACKAGE__->grouping_column('album_id'); | ||
| 114 | |||
| 115 | After a move operation you will always be redirected to the referer | ||
| 116 | If no referer header is foudn you'll be redirected to '/' | ||
| 117 | |||
| 118 | =head1 ACTIONS | ||
| 119 | |||
| 120 | =head2 move_next | ||
| 121 | |||
| 122 | will switch the resource with the next one | ||
| 123 | |||
| 124 | =head2 move_previous | ||
| 125 | |||
| 126 | will switch the resource with the previous one | ||
| 127 | |||
| 128 | =head2 move_to | ||
| 129 | |||
| 130 | move resource to denoted position | ||
| 131 | |||
| 132 | =head1 AUTHOR | ||
| 133 | |||
| 134 | David Schmidt <davewood@cpan.org> | ||
| 135 | |||
| 136 | =head1 COPYRIGHT AND LICENSE | ||
| 137 | |||
| 138 | This software is copyright (c) 2011 by David Schmidt. | ||
| 139 | |||
| 140 | This is free software; you can redistribute it and/or modify it under | ||
| 141 | the same terms as the Perl 5 programming language system itself. | ||
| 142 | |||
| 143 | =cut |