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.



54
55
56
57
58
59
60
# File 'lib/app_query/paginatable.rb', line 54

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



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

def current_page = @page

#first_page?Boolean

Returns:

  • (Boolean)


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

def first_page? = @page == 1

#last_page?Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
# File 'lib/app_query/paginatable.rb', line 87

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

#limit_valueObject



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

def limit_value = @per_page

#next_pageObject



79
80
81
82
83
84
85
# File 'lib/app_query/paginatable.rb', line 79

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)


95
96
97
# File 'lib/app_query/paginatable.rb', line 95

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

#prev_pageObject



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

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

#total_countObject



70
71
72
# File 'lib/app_query/paginatable.rb', line 70

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

#total_pagesObject



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

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

#transform!Object



99
100
101
102
# File 'lib/app_query/paginatable.rb', line 99

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