Module: Pageable::ActiveRecord::Relation

Extended by:
ActiveSupport::Concern
Defined in:
lib/pageable/active_record/relation.rb

Instance Method Summary collapse

Instance Method Details

#current_pageObject



30
31
32
# File 'lib/pageable/active_record/relation.rb', line 30

def current_page
  @current_page ||= (offset_value.to_f / fixed_limit_value).ceil + 1
end

#first_pageObject



42
43
44
# File 'lib/pageable/active_record/relation.rb', line 42

def first_page
  1
end

#last_pageObject



46
47
48
# File 'lib/pageable/active_record/relation.rb', line 46

def last_page
  total_pages
end

#next_pageObject



38
39
40
# File 'lib/pageable/active_record/relation.rb', line 38

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

#out_of_bounds?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/pageable/active_record/relation.rb', line 50

def out_of_bounds?
  @out_of_bounds ||= current_page > total_pages or current_page < first_page
end

#padding(value) ⇒ Object



11
12
13
14
15
# File 'lib/pageable/active_record/relation.rb', line 11

def padding(value)
  @padding = value
  r = (offset_value + value) < 0 ? self : offset(offset_value + value)
  decrease_limit? ? r.limit(limit_value + value) : r
end

#per(value) ⇒ Object



6
7
8
9
# File 'lib/pageable/active_record/relation.rb', line 6

def per(value)
  value = [value.to_i, 1].max
  limit(value).offset(value * (offset_value / limit_value))
end

#previous_pageObject



34
35
36
# File 'lib/pageable/active_record/relation.rb', line 34

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

#total_countObject



17
18
19
20
21
22
23
24
# File 'lib/pageable/active_record/relation.rb', line 17

def total_count
  @total_count ||= begin
    r = except(:offset, :limit, :order, :reorder)
    r = r.except(:includes) unless eager_loading?
    r = r.count
    r.respond_to?(:count) ? r.count : r
  end
end

#total_pagesObject



26
27
28
# File 'lib/pageable/active_record/relation.rb', line 26

def total_pages
  @total_pages ||= [(fixed_total_count.to_f / fixed_limit_value).ceil, 1].max
end