Class: CursorPager::Page

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cursor_pager/page.rb

Overview

The main class that coordinates the whole pagination.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation, first: nil, last: nil, after: nil, before: nil) ⇒ Page

Returns a new instance of Page.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cursor_pager/page.rb', line 14

def initialize(relation, first: nil, last: nil, after: nil, before: nil)
  @configuration = CursorPager.configuration
  @relation = relation
  @first_value = first
  @last_value = last
  @after = after
  @before = before
  @order_values = OrderValues.from_relation(relation)

  add_default_order
  verify_order_directions!
end

Instance Attribute Details

#afterObject (readonly)

Returns the value of attribute after.



8
9
10
# File 'lib/cursor_pager/page.rb', line 8

def after
  @after
end

#beforeObject (readonly)

Returns the value of attribute before.



8
9
10
# File 'lib/cursor_pager/page.rb', line 8

def before
  @before
end

#first_valueObject (readonly)

Returns the value of attribute first_value.



8
9
10
# File 'lib/cursor_pager/page.rb', line 8

def first_value
  @first_value
end

#last_valueObject (readonly)

Returns the value of attribute last_value.



8
9
10
# File 'lib/cursor_pager/page.rb', line 8

def last_value
  @last_value
end

#order_valuesObject (readonly)

Returns the value of attribute order_values.



8
9
10
# File 'lib/cursor_pager/page.rb', line 8

def order_values
  @order_values
end

#relationObject (readonly)

Returns the value of attribute relation.



8
9
10
# File 'lib/cursor_pager/page.rb', line 8

def relation
  @relation
end

Instance Method Details

#cursor_for(item) ⇒ Object



70
71
72
73
74
# File 'lib/cursor_pager/page.rb', line 70

def cursor_for(item)
  return if item.nil?

  encoder.encode(item.id.to_s)
end

#firstObject

A capped ‘first` value. The underlying instance variable `first_value` doesn’t have limits on it. If neither ‘first` nor `last` is given, but `default_page_size` or `maximum_page_size` are configured, they will be used for first.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cursor_pager/page.rb', line 31

def first
  @first ||= begin
               capped = limit_pagination_argument(first_value)

               if capped.nil? && last.nil?
                 capped = default_page_size || maximum_page_size
               end

               capped
             end
end

#first_cursorObject



76
77
78
# File 'lib/cursor_pager/page.rb', line 76

def first_cursor
  cursor_for(records.first)
end

#lastObject

A capped ‘last` value. The underlying instance variable `last_value` doesn’t have limits on it.



45
46
47
# File 'lib/cursor_pager/page.rb', line 45

def last
  @last ||= limit_pagination_argument(last_value)
end

#last_cursorObject



80
81
82
# File 'lib/cursor_pager/page.rb', line 80

def last_cursor
  cursor_for(records.last)
end

#next_page?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
# File 'lib/cursor_pager/page.rb', line 59

def next_page?
  @next_page ||= if before_limit_value.present?
                   true
                 elsif first
                   sliced_relation.unscope(:select).select(1)
                     .limit(first + 1).count == first + 1
                 else
                   false
                 end
end

#previous_page?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
# File 'lib/cursor_pager/page.rb', line 49

def previous_page?
  @previous_page ||= if after_limit_value.present?
                       true
                     elsif last
                       limited_relation.offset_value.to_i.positive?
                     else
                       false
                     end
end

#recordsObject



84
85
86
# File 'lib/cursor_pager/page.rb', line 84

def records
  @records ||= limited_relation.to_a
end