Class: MongoModel::Paginator

Inherits:
Array
  • Object
show all
Defined in:
lib/mongomodel/support/paginator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, page, per_page) ⇒ Paginator

Returns a new instance of Paginator.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mongomodel/support/paginator.rb', line 5

def initialize(scope, page, per_page)
  @current_page = page.to_i
  @per_page = per_page.to_i

  super(scope.offset(offset).limit(per_page))

  # Try to autodetect total entries
  if total_entries.nil? && size < per_page && (current_page == 1 or size > 0)
    @total_entries = offset + size
  else
    @total_entries = scope.count
  end
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



3
4
5
# File 'lib/mongomodel/support/paginator.rb', line 3

def current_page
  @current_page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



3
4
5
# File 'lib/mongomodel/support/paginator.rb', line 3

def per_page
  @per_page
end

#total_entriesObject (readonly)

Returns the value of attribute total_entries.



3
4
5
# File 'lib/mongomodel/support/paginator.rb', line 3

def total_entries
  @total_entries
end

Instance Method Details

#next_pageObject



27
28
29
# File 'lib/mongomodel/support/paginator.rb', line 27

def next_page
  current_page < total_pages ? (current_page + 1) : nil
end

#offsetObject



35
36
37
# File 'lib/mongomodel/support/paginator.rb', line 35

def offset
  (current_page - 1) * per_page
end

#out_of_bounds?Boolean

Returns:



31
32
33
# File 'lib/mongomodel/support/paginator.rb', line 31

def out_of_bounds?
  current_page > total_pages
end

#previous_pageObject



23
24
25
# File 'lib/mongomodel/support/paginator.rb', line 23

def previous_page
  current_page > 1 ? (current_page - 1) : nil
end

#total_pagesObject



19
20
21
# File 'lib/mongomodel/support/paginator.rb', line 19

def total_pages
  total_entries.zero? ? 1 : (total_entries / per_page.to_f).ceil
end