Module: Pageable::ActiveRecord::Relation
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/pageable/active_record/relation.rb
Instance Method Summary collapse
- #current_page ⇒ Object
- #first_page ⇒ Object
- #last_page ⇒ Object
- #next_page ⇒ Object
- #out_of_bounds? ⇒ Boolean
- #padding(value) ⇒ Object
- #per(value) ⇒ Object
- #previous_page ⇒ Object
- #total_count ⇒ Object
- #total_pages ⇒ Object
Instance Method Details
#current_page ⇒ Object
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_page ⇒ Object
42 43 44 |
# File 'lib/pageable/active_record/relation.rb', line 42 def first_page 1 end |
#last_page ⇒ Object
46 47 48 |
# File 'lib/pageable/active_record/relation.rb', line 46 def last_page total_pages end |
#next_page ⇒ Object
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
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_page ⇒ Object
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_count ⇒ Object
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_pages ⇒ Object
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 |