Class: PinFlags::Page

Inherits:
Object
  • Object
show all
Defined in:
app/models/pin_flags/page.rb

Constant Summary collapse

DEFAULT_PAGE_SIZE =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#indexObject (readonly)

Returns the value of attribute index.



4
5
6
# File 'app/models/pin_flags/page.rb', line 4

def index
  @index
end

#page_sizeObject (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

#recordsObject (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

Returns:

  • (Boolean)


24
25
26
# File 'app/models/pin_flags/page.rb', line 24

def empty?
  total_count == 0
end

#first?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'app/models/pin_flags/page.rb', line 16

def first?
  index == 1
end

#last?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'app/models/pin_flags/page.rb', line 20

def last?
  index == pages_count || empty? || records.empty?
end

#next_indexObject



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_countObject



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_indexObject



28
29
30
# File 'app/models/pin_flags/page.rb', line 28

def previous_index
  [ index - 1, 1 ].max
end

#total_countObject



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