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
- #current_page ⇒ Object
- #default_per_page ⇒ Object
- #first_page? ⇒ Boolean
- #last_page? ⇒ Boolean
- #next_page ⇒ Object
- #next_page? ⇒ Boolean
- #out_of_range? ⇒ Boolean
- #page(pg = 1) ⇒ Object
- #page!(pg = 1) ⇒ Object
- #paginate!(pg, pp) ⇒ Object
- #per(pp = nil) ⇒ Object
- #per!(pp = nil) ⇒ Object
- #per_page ⇒ Object
- #prev_page ⇒ Object
- #prev_page? ⇒ Boolean
- #total_pages ⇒ Object
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_page ⇒ Object
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_page ⇒ Object
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
68 69 70 |
# File 'lib/active_record/collections/pagination.rb', line 68 def first_page? current_page == 1 end |
#last_page? ⇒ Boolean
72 73 74 |
# File 'lib/active_record/collections/pagination.rb', line 72 def last_page? current_page == total_pages end |
#next_page ⇒ Object
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
60 61 62 |
# File 'lib/active_record/collections/pagination.rb', line 60 def next_page? !last_page? end |
#out_of_range? ⇒ 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_page ⇒ Object
48 49 50 |
# File 'lib/active_record/collections/pagination.rb', line 48 def per_page limit_value || total_count end |
#prev_page ⇒ Object
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
64 65 66 |
# File 'lib/active_record/collections/pagination.rb', line 64 def prev_page? !first_page? end |
#total_pages ⇒ Object
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 |