Class: Kadmin::Pager
- Inherits:
-
Object
- Object
- Kadmin::Pager
- Includes:
- Presentable
- Defined in:
- app/components/kadmin/pager.rb,
app/components/kadmin/pager/presenter.rb
Overview
Simple Pager structure, used to paginate collections
Defined Under Namespace
Classes: Presenter
Instance Attribute Summary collapse
-
#current_page ⇒ Integer
readonly
The current page.
-
#offset ⇒ Integer
readonly
Current index offset (determines current page).
-
#pages ⇒ Integer
readonly
Total number of pages.
-
#size ⇒ Integer
readonly
Number of items per page.
-
#total ⇒ Integer
Total number of items in the collection.
Instance Method Summary collapse
-
#contains?(page) ⇒ Boolean
True if
pageexists (i.e. would have any data). -
#current_page?(page) ⇒ Boolean
True if
pageis the current page. -
#initialize(size:, offset:) ⇒ Pager
constructor
A new instance of Pager.
-
#next_page?(page = nil) ⇒ Boolean
True if there is a next page.
-
#offset_at(page = nil) ⇒ Integer
Start offset for the given page.
-
#page_size(page = nil) ⇒ Integer
The number of items that are on this page.
-
#paginate(collection) ⇒ ActiveRecord::Relation
Paginated collection.
-
#previous_page?(page = nil) ⇒ Boolean
True if there is a previous page.
Methods included from Presentable
Constructor Details
#initialize(size:, offset:) ⇒ Pager
Returns a new instance of Pager.
24 25 26 27 28 29 30 31 32 33 |
# File 'app/components/kadmin/pager.rb', line 24 def initialize(size:, offset:) @size = size @offset = offset raise(Kadmin::Error, 'Page size must be greater than 0!') unless @size.positive? raise(Kadmin::Error, 'Offset must be at least 0!') unless @offset >= 0 @current_page = (@offset / @size.to_f).floor self.total = @offset # assume offset is maximum initially end |
Instance Attribute Details
#current_page ⇒ Integer (readonly)
Returns the current page.
17 18 19 |
# File 'app/components/kadmin/pager.rb', line 17 def current_page @current_page end |
#offset ⇒ Integer (readonly)
Returns current index offset (determines current page).
11 12 13 |
# File 'app/components/kadmin/pager.rb', line 11 def offset @offset end |
#pages ⇒ Integer (readonly)
Returns total number of pages.
14 15 16 |
# File 'app/components/kadmin/pager.rb', line 14 def pages @pages end |
#size ⇒ Integer (readonly)
Returns number of items per page.
8 9 10 |
# File 'app/components/kadmin/pager.rb', line 8 def size @size end |
#total ⇒ Integer
Returns total number of items in the collection.
20 21 22 |
# File 'app/components/kadmin/pager.rb', line 20 def total @total end |
Instance Method Details
#contains?(page) ⇒ Boolean
Returns true if page exists (i.e. would have any data).
63 64 65 |
# File 'app/components/kadmin/pager.rb', line 63 def contains?(page) page.in?(0...@pages) end |
#current_page?(page) ⇒ Boolean
Returns true if page is the current page.
57 58 59 |
# File 'app/components/kadmin/pager.rb', line 57 def current_page?(page) return page == @current_page end |
#next_page?(page = nil) ⇒ Boolean
Returns true if there is a next page.
87 88 89 90 |
# File 'app/components/kadmin/pager.rb', line 87 def next_page?(page = nil) page ||= @current_page return offset_at(page.to_i + 1) < @total end |
#offset_at(page = nil) ⇒ Integer
Returns start offset for the given page.
50 51 52 53 |
# File 'app/components/kadmin/pager.rb', line 50 def offset_at(page = nil) page ||= @current_page return @size * page.to_i end |
#page_size(page = nil) ⇒ Integer
Returns the number of items that are on this page.
75 76 77 78 79 80 81 82 83 |
# File 'app/components/kadmin/pager.rb', line 75 def page_size(page = nil) page ||= @current_page return 0 unless contains?(page) page_start = offset_at(page) page_end = [offset_at(page.to_i + 1), @total].min return page_end - page_start end |
#paginate(collection) ⇒ ActiveRecord::Relation
Returns paginated collection.
37 38 39 40 41 42 43 44 45 46 |
# File 'app/components/kadmin/pager.rb', line 37 def paginate(collection) return nil if collection.nil? self.total = collection.count collection = collection.offset(@offset) collection = collection.limit(@size) return collection end |
#previous_page?(page = nil) ⇒ Boolean
Returns true if there is a previous page.
94 95 96 97 98 |
# File 'app/components/kadmin/pager.rb', line 94 def previous_page?(page = nil) page ||= @current_page return page.to_i.positive? end |