Class: PinFlags::Page
- Inherits:
-
Object
- Object
- PinFlags::Page
- Defined in:
- app/models/pin_flags/page.rb
Constant Summary collapse
- DEFAULT_PAGE_SIZE =
10
Instance Attribute Summary collapse
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#page_size ⇒ Object
readonly
Returns the value of attribute page_size.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Instance Method Summary collapse
- #empty? ⇒ Boolean
- #first? ⇒ Boolean
-
#initialize(relation, page: 1, page_size: DEFAULT_PAGE_SIZE) ⇒ Page
constructor
A new instance of Page.
- #last? ⇒ Boolean
- #next_index ⇒ Object
- #pages_count ⇒ Object
- #previous_index ⇒ Object
- #total_count ⇒ Object
Constructor Details
#initialize(relation, page: 1, page_size: DEFAULT_PAGE_SIZE) ⇒ Page
Returns a new instance of Page.
6 7 8 9 10 |
# File 'app/models/pin_flags/page.rb', line 6 def initialize(relation, page: 1, page_size: DEFAULT_PAGE_SIZE) @relation = relation @page_size = page_size @index = [ page, 1 ].max end |
Instance Attribute Details
#index ⇒ Object (readonly)
Returns the value of attribute index.
4 5 6 |
# File 'app/models/pin_flags/page.rb', line 4 def index @index end |
#page_size ⇒ Object (readonly)
Returns the value of attribute page_size.
4 5 6 |
# File 'app/models/pin_flags/page.rb', line 4 def page_size @page_size end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
4 5 6 |
# File 'app/models/pin_flags/page.rb', line 4 def records @records end |
Instance Method Details
#empty? ⇒ Boolean
24 25 26 |
# File 'app/models/pin_flags/page.rb', line 24 def empty? total_count == 0 end |
#first? ⇒ Boolean
16 17 18 |
# File 'app/models/pin_flags/page.rb', line 16 def first? index == 1 end |
#last? ⇒ Boolean
20 21 22 |
# File 'app/models/pin_flags/page.rb', line 20 def last? index == pages_count || empty? || records.empty? end |
#next_index ⇒ Object
32 33 34 |
# File 'app/models/pin_flags/page.rb', line 32 def next_index pages_count&.positive? ? [ index + 1, pages_count ].min : index + 1 end |
#pages_count ⇒ Object
36 37 38 |
# File 'app/models/pin_flags/page.rb', line 36 def pages_count (total_count.to_f / page_size).ceil unless total_count.infinite? end |
#previous_index ⇒ Object
28 29 30 |
# File 'app/models/pin_flags/page.rb', line 28 def previous_index [ index - 1, 1 ].max end |
#total_count ⇒ Object
40 41 42 |
# File 'app/models/pin_flags/page.rb', line 40 def total_count @total_count ||= @relation.size # Uses loaded records if available, otherwise falls back to count end |