File Coverage

lib/CatalystX/Resource/TraitFor/Controller/Resource/Sortable.pm
Criterion Covered Total %
statement 34 34 100.0
branch 2 2 100.0
condition 3 6 50.0
subroutine 8 8 100.0
pod 3 3 100.0
total 50 53 94.3


line stmt bran cond sub pod time code
1             package CatalystX::Resource::TraitFor::Controller::Resource::Sortable;
2             $CatalystX::Resource::TraitFor::Controller::Resource::Sortable::VERSION = '0.03';
3 9     9   13984 use MooseX::MethodAttributes::Role;
  9         22  
  9         93  
4 9     9   85491 use namespace::autoclean;
  9         26  
  9         74  
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 4     4 1 1784 my ( $self, $c ) = @_;
19 4         18 my $resource = $c->stash->{ $self->resource_key };
20 4         48 $resource->move_next;
21 4         49332 $c->flash( msg => $self->_msg( $c, 'move_next' ) );
22 4   50     19871 $c->res->redirect($c->req->referer || '/');
23 9     9   1905 }
  9         27  
  9         84  
24              
25              
26             sub move_previous : Method('POST') Chained('base_with_id') PathPart('move_previous') Args(0) {
27 2     2 1 910 my ( $self, $c ) = @_;
28 2         9 my $resource = $c->stash->{ $self->resource_key };
29 2         39 $resource->move_previous;
30 2         11027 $c->flash( msg => $self->_msg( $c, 'move_previous' ) );
31 2   50     9120 $c->res->redirect($c->req->referer || '/');
32 9     9   5098 }
  9         25  
  9         60  
33              
34              
35             sub move_to : Method('POST') Chained('base_with_id') PathPart('move_to') Args(0) {
36 2     2 1 1042 my ( $self, $c ) = @_;
37              
38 2         9 my $pos = $c->req->param('pos');
39 2 100       307 if (!defined $pos) {
40 1         10 $c->stash( error_msg => $self->_msg( $c, 'move_to_undef' ) );
41 1         181 $c->detach( $self->error_path );
42             }
43              
44 1         6 my $resource = $c->stash->{ $self->resource_key };
45 1         9 $resource->move_to( $pos );
46 1         5539 $c->flash( msg => $self->_msg( $c, 'move_to' ) );
47 1   50     4570 $c->res->redirect($c->req->referer || '/');
48 9     9   3990 }
  9         23  
  9         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.03
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