Skip to content

Commit

Permalink
Refactor TypesenseSearchService to use page/per
Browse files Browse the repository at this point in the history
  • Loading branch information
NuckChorris committed Sep 21, 2023
1 parent f022d38 commit 846b094
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/services/anime_search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 22 additions & 6 deletions app/services/typesense_search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit 846b094

Please sign in to comment.