Class: Backframe::Response::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/backframe/response/collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(collection, page, per_page) ⇒ Collection



9
10
11
12
13
# File 'lib/backframe/response/collection.rb', line 9

def initialize(collection, page, per_page)
  @collection = collection
  @page = page
  @per_page = per_page
end

Instance Method Details

#current_pageObject



23
24
25
# File 'lib/backframe/response/collection.rb', line 23

def current_page
  @page ? [total_pages, @page.to_i].min : 1
end

#limitObject



27
28
29
# File 'lib/backframe/response/collection.rb', line 27

def limit
  @page ? @per_page : total_records
end

#offsetObject



31
32
33
# File 'lib/backframe/response/collection.rb', line 31

def offset
  @page ? limit * (@page - 1) : 0
end

#recordsObject



35
36
37
# File 'lib/backframe/response/collection.rb', line 35

def records
  @page ? @collection.limit(limit).offset(offset) : @collection.all
end

#total_pagesObject



19
20
21
# File 'lib/backframe/response/collection.rb', line 19

def total_pages
  @page ? (total_records / @per_page.to_f).ceil : 1
end

#total_recordsObject



15
16
17
# File 'lib/backframe/response/collection.rb', line 15

def total_records
  @collection.count
end