1NAME
2 Catalyst::TraitFor::Controller::DBIC::DoesPaging - Helps you paginate,
3 search, sort, and more easily using DBIx::Class
4
5VERSION
6 version 1.001000
7
8SYNOPSIS
9 package MyApp::Controller::Foo;
10 use Moose;
11 BEGIN { extends 'Catalyst::Controller' }
12 with 'Catalyst::TraitFor::Controller::DBIC::DoesPaging';
13
14 sub people {
15 my ($self, $c) = @_;
16 my $people = $self->page_and_sort(
17 $self->search( $self->model('DB::People') )
18 );
19 # ...
20 }
21
22DESCRIPTION
23 This module helps you to map various DBIx::Class features to CGI
24 parameters. For the most part that means it will help you search, sort,
25 and paginate with a minimum of effort and thought.
26
27METHODS
28 All methods take the context and a ResultSet as their arguments. All
29 methods return a ResultSet.
30
31 page_and_sort
32 my $result = $self->page_and_sort($c, $c->model('DB::Foo'));
33
34 This is a helper method that will first "sort" your data and then
35 "paginate" it.
36
37 paginate
38 my $result = $self->paginate($c, $c->model('DB::Foo'));
39
40 Paginates the passed in resultset based on the following CGI parameters:
41
42 start - first row to display
43 limit - amount of rows per page
44
45 search
46 my $searched_rs = $self->search($c, $c->model('DB::Foo'));
47
48 If the $resultset has a "controller_search" method it will call that
49 method on the passed in resultset with all of the CGI parameters. I like
50 to have this method look something like the following:
51
52 # Base search dispatcher, defined in MyApp::Schema::ResultSet
53 sub _build_search {
54 my $self = shift;
55 my $dispatch_table = shift;
56 my $q = shift;
57
58 my %search = ();
59 my %meta = ();
60
61 foreach ( keys %{$q} ) {
62 if ( my $fn = $dispatch_table->{$_} and $q->{$_} ) {
63 my ( $tmp_search, $tmp_meta ) = $fn->( $q->{$_} );
64 %search = ( %search, %{$tmp_search||{}} );
65 %meta = ( %meta, %{$tmp_meta||{}} );
66 }
67 }
68
69 return $self->search(\%search, \%meta);
70 }
71
72 # search method in specific resultset
73 sub controller_search {
74 my $self = shift;
75 my $params = shift;
76 return $self->_build_search({
77 status => sub {
78 return { 'repair_order_status' => shift }, {};
79 },
80 part_id => sub {
81 return {
82 'lineitems.part_id' => { -like => q{%}.shift( @_ ).q{%} }
83 }, { join => 'lineitems' };
84 },
85 },$params);
86 }
87
88 If the "controller_search" method does not exist, this method will call
89 "simple_search" instead.
90
91 sort
92 my $result = $self->sort($c, $c->model('DB::Foo'));
93
94 Exactly the same as search, except calls "controller_sort" or
95 "simple_sort". Here is how I use it:
96
97 # Base sort dispatcher, defined in MyApp::Schema::ResultSet
98 sub _build_sort {
99 my $self = shift;
100 my $dispatch_table = shift;
101 my $default = shift;
102 my $q = shift;
103
104 my %search = ();
105 my %meta = ();
106
107 my $direction = $q->{dir};
108 my $sort = $q->{sort};
109
110 if ( my $fn = $dispatch_table->{$sort} ) {
111 my ( $tmp_search, $tmp_meta ) = $fn->( $direction );
112 %search = ( %search, %{$tmp_search||{}} );
113 %meta = ( %meta, %{$tmp_meta||{}} );
114 } elsif ( $sort && $direction ) {
115 my ( $tmp_search, $tmp_meta ) = $default->( $sort, $direction );
116 %search = ( %search, %{$tmp_search||{}} );
117 %meta = ( %meta, %{$tmp_meta||{}} );
118 }
119
120 return $self->search(\%search, \%meta);
121 }
122
123 # sort method in specific resultset
124 sub controller_sort {
125 my $self = shift;
126 my $params = shift;
127 return $self->_build_sort({
128 first_name => sub {
129 my $direction = shift;
130 return {}, {
131 order_by => { "-$direction" => [qw{last_name first_name}] },
132 };
133 },
134 }, sub {
135 my $param = shift;
136 my $direction = shift;
137 return {}, {
138 order_by => { "-$direction" => $param },
139 };
140 },$params);
141 }
142
143 simple_deletion
144 $self->simple_deletion($c, $c->model('DB::Foo'));
145
146 Deletes from the passed in resultset based on the following CGI
147 parameter:
148
149 to_delete - values of the ids of items to delete
150
151 This is the only method that does not return a ResultSet. Instead it
152 returns an arrayref of the id's that it deleted. If the ResultSet has
153 has a multipk this will expect each tuple of PK's to be separated by
154 commas.
155
156 Note that this method uses the "$rs->delete" method, as opposed to
157 "$rs->delete_all"
158
159 simple_search
160 my $searched_rs = $self->simple_search($c, $c->model('DB::Foo'));
161
162 Searches the resultset based on all fields in the request, except for
163 fields listed in "ignored_params". Searches with "$fieldname => { -like
164 => "%$value%" }". If there are multiple values for a CGI parameter it
165 will use all values via an "or".
166
167 simple_sort
168 my $sorted_rs = $self->simple_sort($c, $c->model('DB::Foo'));
169
170 Sorts the passed in resultset based on the following CGI parameters:
171
172 sort - field to sort by, defaults to primarky key
173 dir - direction to sort
174
175CONFIG VARIABLES
176 page_size
177 Default size of a page. Defaults to 25.
178
179 ignored_params
180 ArrayRef of params that will be ignored in simple_search, defaults
181 to:
182
183 [qw{limit start sort dir _dc rm xaction}]
184
185CREDITS
186 Thanks to Micro Technology Services, Inc. for funding the initial
187 development of this module.
188
189AUTHOR
190 Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
191
192COPYRIGHT AND LICENSE
193 This software is copyright (c) 2010 by Arthur Axel "fREW" Schmidt.
194
195 This is free software; you can redistribute it and/or modify it under
196 the same terms as the Perl 5 programming language system itself.
197
198