Module: Istox::Filter

Defined in:
lib/istox/helpers/filter.rb

Class Method Summary collapse

Class Method Details

.order(query:, filters:) ⇒ Object



3
4
5
6
7
8
# File 'lib/istox/helpers/filter.rb', line 3

def self.order(query:, filters:)
  filters&.sort&.each do |x|
    query = query.order("#{x.field.downcase} #{x.order.upcase}")
  end
  query
end

.order_and_paginate(query:, filters:) ⇒ Object



32
33
34
35
# File 'lib/istox/helpers/filter.rb', line 32

def self.order_and_paginate(query:, filters:)
  query = order(query: query, filters: filters)
  paginate(query: query, filters: filters)
end

.paginate(query:, filters:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/istox/helpers/filter.rb', line 10

def self.paginate(query:, filters:)
  current_page = filters.page
  offset = (current_page - 1) * filters.limit
  total = query.count
  data = query.limit(filters.limit).offset(offset)

  page_count = (total + filters.limit - 1) / filters.limit

  {
    data: data,
    pagination: {
      page: current_page,
      limit: filters.limit,
      previousPage: current_page - 1,
      currentPage: current_page,
      nextPage: current_page < page_count ? current_page + 1 : 0,
      pageCount: page_count,
      totalCount: total
    }
  }
end