[Bps-public-commit] rt-extension-rest2 02/07: Abstract setup_ordering to simplify limit_collection and also avoid duplication

sunnavy sunnavy at bestpractical.com
Wed Jul 14 10:05:49 EDT 2021


This is an automated email from the git hooks/post-receive script.

sunnavy pushed a commit to branch saved-searches
in repository rt-extension-rest2.

commit 649dc129ac4a14f42aaf311ae0c754c26f8f1464
Author: sunnavy <sunnavy at bestpractical.com>
AuthorDate: Tue May 25 22:26:53 2021 +0800

    Abstract setup_ordering to simplify limit_collection and also avoid duplication
---
 lib/RT/Extension/REST2/Resource/Collection.pm          | 17 +++++++++++++++++
 .../Extension/REST2/Resource/Collection/QueryByJSON.pm | 11 -----------
 lib/RT/Extension/REST2/Resource/Tickets.pm             | 18 +++---------------
 3 files changed, 20 insertions(+), 26 deletions(-)

diff --git a/lib/RT/Extension/REST2/Resource/Collection.pm b/lib/RT/Extension/REST2/Resource/Collection.pm
index ee3245a..c27df40 100644
--- a/lib/RT/Extension/REST2/Resource/Collection.pm
+++ b/lib/RT/Extension/REST2/Resource/Collection.pm
@@ -12,6 +12,7 @@ use Web::Machine::FSM::States qw( is_status_code );
 use Module::Runtime qw( require_module );
 use RT::Extension::REST2::Util qw( serialize_record expand_uid format_datetime );
 use POSIX qw( ceil );
+use Encode;
 
 has 'collection_class' => (
     is  => 'ro',
@@ -45,6 +46,21 @@ sub setup_paging {
     $self->collection->GotoPage($page - 1);
 }
 
+sub setup_ordering {
+    my $self = shift;
+    my @orderby_cols;
+    my @orders = $self->request->param('order');
+    foreach my $orderby ($self->request->param('orderby')) {
+        $orderby = decode_utf8($orderby);
+        my $order = shift @orders || 'ASC';
+        $order = uc(decode_utf8($order));
+        $order = 'ASC' unless $order eq 'DESC';
+        push @orderby_cols, {FIELD => $orderby, ORDER => $order};
+    }
+    $self->collection->OrderByCols(@orderby_cols)
+        if @orderby_cols;
+}
+
 sub limit_collection {
     my $self        = shift;
     my $collection  = $self->collection;
@@ -57,6 +73,7 @@ sub limit_collection {
 sub search {
     my $self = shift;
     $self->setup_paging;
+    $self->setup_ordering;
     return $self->limit_collection;
 }
 
diff --git a/lib/RT/Extension/REST2/Resource/Collection/QueryByJSON.pm b/lib/RT/Extension/REST2/Resource/Collection/QueryByJSON.pm
index 9a7756c..b4e606c 100644
--- a/lib/RT/Extension/REST2/Resource/Collection/QueryByJSON.pm
+++ b/lib/RT/Extension/REST2/Resource/Collection/QueryByJSON.pm
@@ -66,17 +66,6 @@ sub limit_collection {
         );
     }
 
-    my @orderby_cols;
-    my @orders = $self->request->param('order');
-    foreach my $orderby ($self->request->param('orderby')) {
-        my $order = shift @orders || 'ASC';
-        $order = uc($order);
-        $order = 'ASC' unless $order eq 'DESC';
-        push @orderby_cols, {FIELD => $orderby, ORDER => $order};
-    }
-    $self->collection->OrderByCols(@orderby_cols)
-        if @orderby_cols;
-
     return 1;
 }
 
diff --git a/lib/RT/Extension/REST2/Resource/Tickets.pm b/lib/RT/Extension/REST2/Resource/Tickets.pm
index f060d49..1398d28 100644
--- a/lib/RT/Extension/REST2/Resource/Tickets.pm
+++ b/lib/RT/Extension/REST2/Resource/Tickets.pm
@@ -46,25 +46,13 @@ sub allowed_methods {
     [ 'GET', 'HEAD', 'POST' ]
 }
 
-sub limit_collection {
+override 'limit_collection' => sub {
     my $self = shift;
     my ($ok, $msg) = $self->collection->FromSQL( $self->query );
     return error_as_json( $self->response, 0, $msg ) unless $ok;
-
-    my @orderby_cols;
-    my @orders = $self->request->param('order');
-    foreach my $orderby ($self->request->param('orderby')) {
-        $orderby = decode_utf8($orderby);
-        my $order = shift @orders || 'ASC';
-        $order = uc(decode_utf8($order));
-        $order = 'ASC' unless $order eq 'DESC';
-        push @orderby_cols, {FIELD => $orderby, ORDER => $order};
-    }
-    $self->collection->OrderByCols(@orderby_cols)
-        if @orderby_cols;
-
+    super();
     return 1;
-}
+};
 
 sub expand_field {
     my $self         = shift;

-- 
To stop receiving notification emails like this one, please contact
sysadmin at bestpractical.com.


More information about the Bps-public-commit mailing list