Class: AppQuery::Paginatable::PaginatedResult

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/app_query/paginatable.rb

Overview

Kaminari-compatible wrapper for paginated results.

Instance Method Summary collapse

Constructor Details

#initialize(records, page:, per_page:, total_count: nil, has_next: nil) ⇒ PaginatedResult

Returns a new instance of PaginatedResult.



41
42
43
44
45
46
47
# File 'lib/app_query/paginatable.rb', line 41

def initialize(records, page:, per_page:, total_count: nil, has_next: nil)
  @records = records
  @page = page
  @per_page = per_page
  @total_count = total_count
  @has_next = has_next
end

Instance Method Details

#current_pageObject



49
# File 'lib/app_query/paginatable.rb', line 49

def current_page = @page

#first_page?Boolean

Returns:

  • (Boolean)


55
# File 'lib/app_query/paginatable.rb', line 55

def first_page? = @page == 1

#last_page?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# File 'lib/app_query/paginatable.rb', line 74

def last_page?
  if @total_count
    @page >= total_pages
  else
    !@has_next
  end
end

#limit_valueObject



51
# File 'lib/app_query/paginatable.rb', line 51

def limit_value = @per_page

#next_pageObject



66
67
68
69
70
71
72
# File 'lib/app_query/paginatable.rb', line 66

def next_page
  if @total_count
    (@page < total_pages) ? @page + 1 : nil
  else
    @has_next ? @page + 1 : nil
  end
end

#out_of_range?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/app_query/paginatable.rb', line 82

def out_of_range?
  empty? && @page > 1
end

#prev_pageObject



53
# File 'lib/app_query/paginatable.rb', line 53

def prev_page = (@page > 1) ? @page - 1 : nil

#total_countObject



57
58
59
# File 'lib/app_query/paginatable.rb', line 57

def total_count
  @total_count || raise("total_count not available in without_count mode")
end

#total_pagesObject



61
62
63
64
# File 'lib/app_query/paginatable.rb', line 61

def total_pages
  return nil unless @total_count
  (@total_count.to_f / @per_page).ceil
end

#transform!Object



86
87
88
89
# File 'lib/app_query/paginatable.rb', line 86

def transform!
  @records = @records.map { |r| yield(r) }
  self
end