[Catalyst-commits] r9478 - in Catalyst-Controller-DBIC-API/1.001/branches/pagination: lib/Catalyst/Controller/DBIC lib/Catalyst/Controller/DBIC/API t/rpc

lukes at dev.catalyst.perl.org lukes at dev.catalyst.perl.org
Mon Mar 9 22:21:17 GMT 2009


Author: lukes
Date: 2009-03-09 22:21:17 +0000 (Mon, 09 Mar 2009)
New Revision: 9478

Modified:
   Catalyst-Controller-DBIC-API/1.001/branches/pagination/lib/Catalyst/Controller/DBIC/API.pm
   Catalyst-Controller-DBIC-API/1.001/branches/pagination/lib/Catalyst/Controller/DBIC/API/Base.pm
   Catalyst-Controller-DBIC-API/1.001/branches/pagination/t/rpc/list.t
Log:
pagination adapted for consistency and working with test

Modified: Catalyst-Controller-DBIC-API/1.001/branches/pagination/lib/Catalyst/Controller/DBIC/API/Base.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/branches/pagination/lib/Catalyst/Controller/DBIC/API/Base.pm	2009-03-09 19:10:30 UTC (rev 9477)
+++ Catalyst-Controller-DBIC-API/1.001/branches/pagination/lib/Catalyst/Controller/DBIC/API/Base.pm	2009-03-09 22:21:17 UTC (rev 9478)
@@ -69,20 +69,12 @@
   $args->{group_by} = $req_params->{list_grouped_by} || ((scalar(@{$self->list_grouped_by})) ? $self->list_grouped_by : undef);
   $args->{order_by} = $req_params->{list_ordered_by} || ((scalar(@{$self->list_ordered_by})) ? $self->list_ordered_by : undef);
   $args->{rows} = $req_params->{list_count} || $self->list_count;
+  $args->{page} = $req_params->{list_page};
+  if ($args->{page} && !$args->{rows}) {
+    $self->push_error($c, { message => "list_page can only be used with list_count" });
+  }
   $args->{select} = $req_params->{list_returns} || ((scalar(@{$self->list_returns})) ? $self->list_returns : undef);
   $args->{join} = $join;
-  
-  # handle pagination
-  my $rows = $c->req->params->{rows};
-  my $page = defined $c->req->params->{page}
-    && $c->req->params->{start} =~ /^\d+$/xms
-    ? int($c->req->params->{start} / $rows) +1
-    : 1;
-  if (defined $rows && $rows =~ /^\d+$/xms && defined $page && $page =~ /^\d+$/xms) {
-    $c->stash->{rows_per_page} = $rows;
-    $args->{page} = $page;
-    $args->{rows} = $rows;
-  }
 
   return [$params, $args];
 }
@@ -284,11 +276,11 @@
 	# check for errors
 	my $default_status;
 
-    # Check for errors caught elsewhere
-    if ( $c->res->status and $c->res->status != 200 ) {
+  # Check for errors caught elsewhere
+	if ( $c->res->status and $c->res->status != 200 ) {
 		$default_status = $c->res->status;
 		$c->stash->{response}->{success} = 'false';
-    }
+  }
 	elsif ($self->get_errors($c)) {
 		$c->stash->{response}->{messages} = $self->get_errors($c);
 		$c->stash->{response}->{success} = 'false';
@@ -298,6 +290,7 @@
 		$default_status = 200;
 	}
 	
+	delete $c->stash->{response}->{list} unless ($default_status == 200);
 	$c->res->status( $default_status || 200 );
 	$c->forward('serialize');
 }

Modified: Catalyst-Controller-DBIC-API/1.001/branches/pagination/lib/Catalyst/Controller/DBIC/API.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/branches/pagination/lib/Catalyst/Controller/DBIC/API.pm	2009-03-09 19:10:30 UTC (rev 9477)
+++ Catalyst-Controller-DBIC-API/1.001/branches/pagination/lib/Catalyst/Controller/DBIC/API.pm	2009-03-09 22:21:17 UTC (rev 9478)
@@ -101,6 +101,10 @@
 
 Arguments to pass to L<DBIx::Class::ResultSet/rows> when performing search for L</list>.
 
+=head2 list_page
+
+Arguments to pass to L<DBIx::Class::ResultSet/rows> when performing search for L</list>.
+
 =head2 object_stash_key
 
 Object level methods such as delete and update stash the object in the stash. Specify the stash key you would like to use here. Defaults to 'object'.
@@ -164,7 +168,7 @@
 
 =head2 list
 
-List level action chained from L</setup>. By default populates $c->stash->{response}->{list} with a list of hashrefs representing each object in the class resultset. If the L</list_returns> config param is defined then the hashes will contain only those columns, otherwise all columns in the object will be returned. Similarly L</list_count>, L</list_grouped_by> and L</list_ordered_by> affect the maximum number of rows returned as well as the ordering and grouping. Note that if list_returns, list_count, list_ordered_by or list_grouped_by request parameters are present then these will override the values set on the class.
+List level action chained from L</setup>. By default populates $c->stash->{response}->{list} with a list of hashrefs representing each object in the class resultset. If the L</list_returns> config param is defined then the hashes will contain only those columns, otherwise all columns in the object will be returned. Similarly L</list_count>, L</list_page>, L</list_grouped_by> and L</list_ordered_by> affect the maximum number of rows returned as well as the ordering and grouping. Note that if list_returns, list_count, list_ordered_by or list_grouped_by request parameters are present then these will override the values set on the class.
 
 If not all objects in the resultset are required then it's possible to pass conditions to the method as request parameters. You can use a JSON string as the 'search' parameter for maximum flexibility or use L</CGI::Expand> syntax. In the second case the request parameters are expanded into a structure and then $c->req->params->{search} is used as the search condition.
 
@@ -178,9 +182,9 @@
 
  $rs->search({ name => 'fred', 'cd.artist' => 'luke' }, { join => ['cd'] })
 
-If pagination is needed it's possible to pass the 'rows' and 'page' request parameters:
+Note that if pagination is needed, this can be achieved using a combination of the L</list_count> and L</list_page> parameters. For example:
 
-  ?page=2&rows=20
+  ?list_page=2&list_count=20
 
 Would result in this search:
  

Modified: Catalyst-Controller-DBIC-API/1.001/branches/pagination/t/rpc/list.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/branches/pagination/t/rpc/list.t	2009-03-09 19:10:30 UTC (rev 9477)
+++ Catalyst-Controller-DBIC-API/1.001/branches/pagination/t/rpc/list.t	2009-03-09 22:21:17 UTC (rev 9478)
@@ -126,31 +126,55 @@
 
 {
   my $uri = URI->new( $track_list_url );
-  $uri->query_form({ 'list_ordered_by' => 'cd', 'list_count' => 1 });
+  $uri->query_form({ 'list_ordered_by' => 'cd', 'list_count' => 2 });
   my $req = GET( $uri, 'Accept' => 'text/x-json' );
   $mech->request($req);
-  cmp_ok( $mech->status, '==', 200, 'search related request okay' );
+  cmp_ok( $mech->status, '==', 200, 'list count request okay' );
 
-  my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search({}, { group_by => 'position', order_by => 'position ASC', select => 'position', rows => 1 })->all;
+  my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search({}, { group_by => 'position', order_by => 'position ASC', select => 'position', rows => 2 })->all;
   my $response = JSON::Syck::Load( $mech->content);
 #  use Data::Dumper; warn Dumper($response, \@expected_response);
-  is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned when everything overridden in query' );
+  is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned' );
 }
 
 {
-    my $uri = URI->new( $cd_list_url );
-    $uri->query_form({ 'search.artist.name' => 'Caterwauler McCrae' });
-    my $req = GET( $uri, 'Accept' => 'text/x-json' );
-    $mech->request($req);
-    if (cmp_ok( $mech->status, '==', 200, 'search on rel with same name column request okay' )) {
-        my @expected_response = map { { $_->get_columns } } $schema->resultset('CD')->search({'me.artist' => 1})->all;
-        my $response = JSON::Syck::Load( $mech->content);
-        #use Data::Dumper; warn Dumper($response, \@expected_response);
-        is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for search on rel with same name column' );
-    }
+  my $uri = URI->new( $track_list_url );
+  $uri->query_form({ 'list_ordered_by' => 'cd', 'list_count' => 2, 'list_page' => 2 });
+  my $req = GET( $uri, 'Accept' => 'text/x-json' );
+  $mech->request($req);
+  cmp_ok( $mech->status, '==', 200, 'list count with page request okay' );
+
+  my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search({}, { group_by => 'position', order_by => 'position ASC', select => 'position', rows => 2, page => 2 })->all;
+  my $response = JSON::Syck::Load( $mech->content);
+#  use Data::Dumper; warn Dumper($response, \@expected_response);
+  is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned' );
 }
 
 {
+  my $uri = URI->new( $track_list_url );
+  $uri->query_form({ 'list_ordered_by' => 'cd', 'list_page' => 2 });
+  my $req = GET( $uri, 'Accept' => 'text/x-json' );
+  $mech->request($req);
+  cmp_ok( $mech->status, '==', 400, 'list page without count returns error' );
+  my $response = JSON::Syck::Load( $mech->content);
+#  use Data::Dumper; warn Dumper($response);
+  is_deeply( { messages => ['list_page can only be used with list_count'], success => 'false' }, $response, 'correct data returned' );
+}
+
+{
+	my $uri = URI->new( $cd_list_url );
+	$uri->query_form({ 'search.artist.name' => 'Caterwauler McCrae' });
+	my $req = GET( $uri, 'Accept' => 'text/x-json' );
+	$mech->request($req);
+	if (cmp_ok( $mech->status, '==', 200, 'search on rel with same name column request okay' )) {
+		my @expected_response = map { { $_->get_columns } } $schema->resultset('CD')->search({'me.artist' => 1})->all;
+		my $response = JSON::Syck::Load( $mech->content);
+		#use Data::Dumper; warn Dumper($response, \@expected_response);
+		is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for search on rel with same name column' );
+	}
+}
+
+{
   my $uri = URI->new( $cd_list_url );
   $uri->query_form({ 'search.artist' => 1 });
   my $req = GET( $uri, 'Accept' => 'text/x-json' );




More information about the Catalyst-commits mailing list