From 5cccc5a89004f9c514e9d611b5e2a02e9aa28ece Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Thu, 5 Feb 2004 17:19:03 +0000 Subject: [PATCH] And now we have paging support. (And some better docs) git-svn-id: http://svn.maypole.perl.org/Maypole/trunk@46 48953598-375a-da11-a14b-00016c27c3ee --- lib/Apache/MVC.pm | 3 ++- lib/Apache/MVC/Model/Base.pm | 23 +++++++++++++++++------ lib/Apache/MVC/Model/CDBI.pm | 32 ++++++++++++++++++++++---------- lib/Apache/MVC/Workflow.pod | 2 +- lib/BeerDB.pm | 2 ++ templates/factory/list | 2 ++ templates/factory/pager | 18 ++++++++++++++++++ 7 files changed, 64 insertions(+), 18 deletions(-) create mode 100644 templates/factory/pager diff --git a/lib/Apache/MVC.pm b/lib/Apache/MVC.pm index 4b1a600..61e0764 100644 --- a/lib/Apache/MVC.pm +++ b/lib/Apache/MVC.pm @@ -8,7 +8,7 @@ use strict; use warnings; our $VERSION = "0.1"; __PACKAGE__->mk_classdata($_) for qw( _config init_done view_object ); -__PACKAGE__->mk_accessors ( qw( config ar params objects model_class +__PACKAGE__->mk_accessors ( qw( config ar params query objects model_class args action template )); __PACKAGE__->config({}); __PACKAGE__->init_done(0); @@ -109,6 +109,7 @@ sub parse_location { $self->{args} = \@pi; $self->{params} = { $self->{ar}->content }; + $self->{query} = { $self->{ar}->args }; } sub is_applicable { diff --git a/lib/Apache/MVC/Model/Base.pm b/lib/Apache/MVC/Model/Base.pm index b7337be..908a2fc 100644 --- a/lib/Apache/MVC/Model/Base.pm +++ b/lib/Apache/MVC/Model/Base.pm @@ -6,11 +6,6 @@ sub FETCH_CODE_ATTRIBUTES { $remember{$_[1]} } sub view :Exported { } sub edit :Exported { } -sub list :Exported { - my ($self, $r) = @_; - $r->objects([ $self->retrieve_all ]); -} - sub process { my ($class, $r) = @_; $r->template( my $method = $r->action ); @@ -64,7 +59,23 @@ This is the name of the table. =head2 Commands -See the exported commands in C. +=over + +=item list + +The C method should fill C<< $r-> objects >> with all of the +objects in the class. You may want to page this using C or +similar. + +=back + +=cut + +sub list :Exported { die "This is an abstract method" }; + +=pod + +Also, see the exported commands in C. =head1 Other overrides diff --git a/lib/Apache/MVC/Model/CDBI.pm b/lib/Apache/MVC/Model/CDBI.pm index d842d5f..854118e 100644 --- a/lib/Apache/MVC/Model/CDBI.pm +++ b/lib/Apache/MVC/Model/CDBI.pm @@ -4,9 +4,23 @@ use Lingua::EN::Inflect::Number qw(to_PL); use Class::DBI::AsForm; use Class::DBI::FromCGI; use Class::DBI::AbstractSearch; +use Class::DBI::Pager; use CGI::Untaint; use strict; +=head1 NAME + +Apache::MVC::Model::CDBI - Model class based on Class::DBI + +=head1 DESCRIPTION + +This is a master model class which uses C to do all the hard +work of fetching rows and representing them as objects. It is a good +model to copy if you're replacing it with other database abstraction +modules. + +=cut + sub related { my ($self, $r) = @_; # Has-many methods; XXX this is a hack @@ -67,15 +81,13 @@ sub search :Exported { $r->{template_args}{search} = 1; } +sub list :Exported { + my ($self, $r) = @_; + if ( my $rows = $r->config->{rows_per_page}) { + $self = $self->pager($rows, $r->query->{page}); + $r->{template_args}{pager} = $self; + } + $r->objects([ $self->retrieve_all ]); +} 1; -=head1 NAME - -Apache::MVC::Model::CDBI - Model class based on Class::DBI - -=head1 DESCRIPTION - -This is a master model class which uses C to do all the hard -work of fetching rows and representing them as objects; instead, it -concentrates on the actions that can be performed in the URL: -C, C and C. diff --git a/lib/Apache/MVC/Workflow.pod b/lib/Apache/MVC/Workflow.pod index 7a0e9f3..15d87c0 100644 --- a/lib/Apache/MVC/Workflow.pod +++ b/lib/Apache/MVC/Workflow.pod @@ -64,7 +64,7 @@ several slots of the request object. First, C and C should be populated with the name of the table and the action parts of the URL. Any other arguments should be placed in a listref in the C slot, and GET and POST parameters should be arranged into a hash -and placed in the C slot. +and placed in the C and C slots, respectively. Some people may not like the idea of passing everything around in the URL; this is the method to override for you. Of course, you'll also need diff --git a/lib/BeerDB.pm b/lib/BeerDB.pm index ec9677a..1f673f2 100644 --- a/lib/BeerDB.pm +++ b/lib/BeerDB.pm @@ -13,6 +13,8 @@ BeerDB->set_database("dbi:mysql:beerdb"); #BeerDB->config->{uri_base} = "http://localhost/beerdb/"; BeerDB->config->{uri_base} = "http://neo.trinity-house.org.uk/beerdb/"; +BeerDB->config->{rows_per_page} = 10; + # Handpumps should not show up. BeerDB->config->{display_tables} = [qw[beer brewery pub style]]; BeerDB::Brewery->untaint_columns( printable => [qw/name notes url/] ); diff --git a/templates/factory/list b/templates/factory/list index d52d3e8..2a15964 100644 --- a/templates/factory/list +++ b/templates/factory/list @@ -30,5 +30,7 @@ FOR item = objects; ""; END %]
+ +[% INCLUDE pager; %] [% INCLUDE addnew; %] diff --git a/templates/factory/pager b/templates/factory/pager new file mode 100644 index 0000000..9053098 --- /dev/null +++ b/templates/factory/pager @@ -0,0 +1,18 @@ +[% +IF pager AND pager.first_page != pager.last_page; +%] + +

Pages: +[% + FOREACH num = [pager.first_page .. pager.last_page]; + IF num == pager.current_page; + "["; num; "] "; + ELSE; + ''; + "["; num; "]"; + ' '; + END; + END; +%] +

+[% END %] -- 2.39.2