From 2ac594269d9a6d9a02944ef490bb8ad8aad1b86d Mon Sep 17 00:00:00 2001 From: barnold Date: Mon, 19 Sep 2022 11:50:01 +0100 Subject: [PATCH] Make rows-per-page a user preference. --- lib/MyApp.pm | 3 ++- lib/MyApp/Controller/Auth.pm | 8 ++++++++ lib/MyModel/Author.pm | 2 +- templates/_page-navigation.html.ep | 7 +++++-- templates/auth/account.html.ep | 9 ++++++++- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/MyApp.pm b/lib/MyApp.pm index 79394fe..df939fb 100644 --- a/lib/MyApp.pm +++ b/lib/MyApp.pm @@ -59,7 +59,8 @@ sub startup ($self) { return undef; } ); - $auth->get('/account')->to('auth#account'); + $auth->get('/account')->to('auth#account')->name('account'); + $auth->post('/preferences')->to('auth#preferences')->name('preferences'); } 1; diff --git a/lib/MyApp/Controller/Auth.pm b/lib/MyApp/Controller/Auth.pm index 772cea5..7ee2288 100644 --- a/lib/MyApp/Controller/Auth.pm +++ b/lib/MyApp/Controller/Auth.pm @@ -26,4 +26,12 @@ sub logout ($self) { $self->redirect_to('home'); } +sub preferences ($self) { + $self->session(rows_per_page => $self->param('rows_per_page')); + $self->flash( + confirmation => sprintf("Updated rows per page to %s.", $self->rpp) + ); + $self->redirect_to('account'); +} + 1; diff --git a/lib/MyModel/Author.pm b/lib/MyModel/Author.pm index 289679c..82295a8 100644 --- a/lib/MyModel/Author.pm +++ b/lib/MyModel/Author.pm @@ -13,7 +13,7 @@ sub get_page ($self, $page_number = 1, $name_like = undef) { { prefetch => [ "books" ], order_by => [ "me.name " ], page => $page_number, - rows => 10, + rows => $self->rows_per_page, }, ); return $rs; diff --git a/templates/_page-navigation.html.ep b/templates/_page-navigation.html.ep index 4fd17a2..8cca60c 100644 --- a/templates/_page-navigation.html.ep +++ b/templates/_page-navigation.html.ep @@ -1,8 +1,10 @@

-Page <%= commify($pager->current_page) %> +Showing <%= $pager->entries_on_this_page %> items +on page <%= commify($pager->current_page) %> of <%= commify($pager->last_page) %> -    +<% if ($pager->total_entries > rpp) { %> +
<%= link_to url_with(page_number => 1) => begin %>◄ First<% end %> % if (my $prev = $pager->previous_page) { @@ -18,3 +20,4 @@ Next ► % } <%= link_to url_with(page_number => $pager->last_page) => begin %>Last ►<% end %> +<% } %> diff --git a/templates/auth/account.html.ep b/templates/auth/account.html.ep index 86f303e..d5af532 100644 --- a/templates/auth/account.html.ep +++ b/templates/auth/account.html.ep @@ -2,4 +2,11 @@ % title 'Account';

<%= logname %>'s account on MyApp

-Account details here. +%= form_for preferences => begin +%= label_for rows_per_page => 'Rows per page' +<%= number_field rows_per_page => $c->rpp, + min => 5, max => 100, maxlength => 4, size => 4 %> +%= submit_button "Update" +% end +

+%= flash('confirmation')