Module: ActiveRecord::Collections::Pagination

Included in:
ActiveRecord::Collection
Defined in:
lib/active_record/collections/pagination.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/active_record/collections/pagination.rb', line 4

def self.included(base)
  base.send :extend, ClassMethods
end

Instance Method Details

#current_pageObject



44
45
46
# File 'lib/active_record/collections/pagination.rb', line 44

def current_page
  (relation.offset_value.to_i / (relation.limit_value || 1)) + 1
end

#default_per_pageObject



15
16
17
# File 'lib/active_record/collections/pagination.rb', line 15

def default_per_page
  self.class.default_per_page
end

#first_page?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/active_record/collections/pagination.rb', line 68

def first_page?
  current_page == 1
end

#last_page?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/active_record/collections/pagination.rb', line 72

def last_page?
  current_page == total_pages
end

#next_pageObject



52
53
54
# File 'lib/active_record/collections/pagination.rb', line 52

def next_page
  current_page + 1 unless last_page?
end

#next_page?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/active_record/collections/pagination.rb', line 60

def next_page?
  !last_page?
end

#out_of_range?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/active_record/collections/pagination.rb', line 76

def out_of_range?
  current_page > total_pages
end

#page(pg = 1) ⇒ Object



19
20
21
# File 'lib/active_record/collections/pagination.rb', line 19

def page(pg=1)
  dup.page!(pg)
end

#page!(pg = 1) ⇒ Object



23
24
25
# File 'lib/active_record/collections/pagination.rb', line 23

def page!(pg=1)
  paginate!(pg, default_per_page)
end

#paginate!(pg, pp) ⇒ Object



35
36
37
38
# File 'lib/active_record/collections/pagination.rb', line 35

def paginate!(pg, pp)
  limit!(pp.to_i)
  offset!((pg.to_i - 1) * pp.to_i)
end

#per(pp = nil) ⇒ Object



27
28
29
# File 'lib/active_record/collections/pagination.rb', line 27

def per(pp=nil)
  dup.per!(pp)
end

#per!(pp = nil) ⇒ Object



31
32
33
# File 'lib/active_record/collections/pagination.rb', line 31

def per!(pp=nil)
  paginate!(current_page, (pp || default_per_page))
end

#per_pageObject



48
49
50
# File 'lib/active_record/collections/pagination.rb', line 48

def per_page
  limit_value || total_count
end

#prev_pageObject



56
57
58
# File 'lib/active_record/collections/pagination.rb', line 56

def prev_page
  current_page - 1 unless first_page?
end

#prev_page?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/active_record/collections/pagination.rb', line 64

def prev_page?
  !first_page?
end

#total_pagesObject



40
41
42
# File 'lib/active_record/collections/pagination.rb', line 40

def total_pages
  (total_count.to_f / (relation.limit_value || total_count).to_f).ceil
end