Module: RailsPaginationUltimate::ActiveRecordExtension::ActiveRecordModelExtension

Defined in:
lib/rails_pagination_ultimate/active_record_extension.rb

Instance Method Summary collapse

Instance Method Details

#current_pageObject



40
41
42
43
44
45
46
# File 'lib/rails_pagination_ultimate/active_record_extension.rb', line 40

def current_page
  limit_val = limit_value || RailsPaginationUltimate.config.default_per_page
  offset_val = offset_value || 0
  (offset_val / limit_val) + 1
rescue StandardError
  1
end

#first_page?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/rails_pagination_ultimate/active_record_extension.rb', line 67

def first_page?
  current_page == 1
end

#is_without_count?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rails_pagination_ultimate/active_record_extension.rb', line 31

def is_without_count?
  singleton_class.included_modules.include?(WithoutCountExtension)
end

#last_page?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/rails_pagination_ultimate/active_record_extension.rb', line 71

def last_page?
  if is_without_count?
    return !has_next_page?
  end
  current_page >= total_pages
end

#next_pageObject



78
79
80
# File 'lib/rails_pagination_ultimate/active_record_extension.rb', line 78

def next_page
  current_page + 1 unless last_page?
end

#per(num) ⇒ Object



35
36
37
38
# File 'lib/rails_pagination_ultimate/active_record_extension.rb', line 35

def per(num)
  num = num.to_i
  limit(num).offset(num * (current_page - 1))
end

#prev_pageObject



82
83
84
# File 'lib/rails_pagination_ultimate/active_record_extension.rb', line 82

def prev_page
  current_page - 1 unless first_page?
end

#total_countObject



57
58
59
60
61
62
63
64
65
# File 'lib/rails_pagination_ultimate/active_record_extension.rb', line 57

def total_count
  return 0 if is_without_count?
  
  @total_count ||= begin
    c = except(:offset, :limit, :order)
    c = c.count
    c.is_a?(Hash) ? c.count : c
  end
end

#total_pagesObject



48
49
50
51
52
53
54
55
# File 'lib/rails_pagination_ultimate/active_record_extension.rb', line 48

def total_pages
  return Float::INFINITY if is_without_count?
  
  limit_val = limit_value || RailsPaginationUltimate.config.default_per_page
  (total_count.to_f / limit_val).ceil
rescue StandardError
  0
end