From 846b0943c69a6d17d14146d88fff1dc7ca2f5b1e Mon Sep 17 00:00:00 2001 From: Emma Lejeck Date: Wed, 20 Sep 2023 18:54:15 -0700 Subject: [PATCH] Refactor TypesenseSearchService to use page/per --- app/services/anime_search_service.rb | 4 ++-- app/services/typesense_search_service.rb | 28 +++++++++++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/app/services/anime_search_service.rb b/app/services/anime_search_service.rb index 3b463984e..72a9d078c 100644 --- a/app/services/anime_search_service.rb +++ b/app/services/anime_search_service.rb @@ -43,8 +43,8 @@ def query ) query = apply_filters_to(query) query = apply_order_to(query) - query = apply_limit_to(query) - query = apply_offset_to(query) + query = apply_page_to(query) + query = apply_per_to(query) query end end diff --git a/app/services/typesense_search_service.rb b/app/services/typesense_search_service.rb index 0422443bf..2f003e8fe 100644 --- a/app/services/typesense_search_service.rb +++ b/app/services/typesense_search_service.rb @@ -57,16 +57,16 @@ def apply_order_to(scope) scope.sort(orders) end - def apply_limit_to(scope) - return scope unless _limit + def apply_per_to(scope) + return scope unless _per - scope.per(_limit) + scope.per(_per) end - def apply_offset_to(scope) - return scope unless _offset + def apply_page_to(scope) + return scope unless _page - scope.page((_offset / _limit).floor) + scope.page(_page) end def parse_range(value) @@ -99,4 +99,20 @@ def orders def filters @filters ||= _filters.merge(_queries) end + + def _per + if @_per + @_per + elsif @_limit + @_per = @_limit + end + end + + def _page + if @_page + @_page + elsif @_offset && @_limit + @_page = (@_offset / @_limit).floor + end + end end