Class: Bridgetown::Paginate::Paginator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
bridgetown-paginate/lib/bridgetown-paginate/paginator.rb

Overview

Handles the preparation of all the documents based on the current page index

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_per_page, first_index_page_url, paginated_page_url, documents, cur_page_nr, num_pages, default_indexpage, default_ext) ⇒ Paginator

Initialize a new Paginator.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 18

def initialize( # rubocop:todo Metrics/AbcSize
  config_per_page,
  first_index_page_url,
  paginated_page_url,
  documents,
  cur_page_nr,
  num_pages,
  default_indexpage,
  default_ext
)
  @page = cur_page_nr
  @per_page = config_per_page.to_i
  @total_pages = num_pages

  if @page > @total_pages
    raise "page number can't be greater than total pages: " \
          "#{@page} > #{@total_pages}"
  end

  init = (@page - 1) * @per_page
  offset = [init + @per_page - 1, documents.size].min

  # Ensure that the current page has correct extensions if needed
  this_page_url = Utils.ensure_full_path(
    @page == 1 ? first_index_page_url : paginated_page_url,
    default_indexpage || "",
    default_ext || ""
  )

  # To support customizable pagination pages we attempt to explicitly
  # append the page name to the url incase the user is using extensionless permalinks.
  if default_indexpage&.length&.positive?
    # Adjust first page url
    first_index_page_url = Utils.ensure_full_path(
      first_index_page_url, default_indexpage, default_ext
    )
    # Adjust the paginated pages as well
    paginated_page_url = Utils.ensure_full_path(
      paginated_page_url, default_indexpage, default_ext
    )
  end

  @total_documents = documents.size
  @documents = documents[init..offset]
  @page_path = Utils.format_page_number(this_page_url, cur_page_nr, @total_pages)

  @previous_page = @page == 1 ? nil : @page - 1
  @previous_page_path = unless @page == 1
                          if @page == 2
                            Utils.format_page_number(
                              first_index_page_url, 1, @total_pages
                            )
                          else
                            Utils.format_page_number(
                              paginated_page_url,
                              @previous_page,
                              @total_pages
                            )
                          end
                        end
  @next_page = @page == @total_pages ? nil : @page + 1
  @next_page_path = if @page != @total_pages
                      Utils.format_page_number(
                        paginated_page_url, @next_page, @total_pages
                      )
                    end

  @first_page = 1
  @first_page_path = Utils.format_page_number(first_index_page_url, 1, @total_pages)
  @last_page = @total_pages
  @last_page_path = Utils.format_page_number(paginated_page_url, @total_pages, @total_pages)
end

Instance Attribute Details

#documentsObject (readonly)

Returns the value of attribute documents.



11
12
13
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 11

def documents
  @documents
end

#first_pageObject (readonly)

Returns the value of attribute first_page.



11
12
13
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 11

def first_page
  @first_page
end

#first_page_pathObject (readonly)

Returns the value of attribute first_page_path.



11
12
13
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 11

def first_page_path
  @first_page_path
end

#last_pageObject (readonly)

Returns the value of attribute last_page.



11
12
13
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 11

def last_page
  @last_page
end

#last_page_pathObject (readonly)

Returns the value of attribute last_page_path.



11
12
13
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 11

def last_page_path
  @last_page_path
end

#next_pageObject (readonly)

Returns the value of attribute next_page.



11
12
13
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 11

def next_page
  @next_page
end

#next_page_pathObject (readonly)

Returns the value of attribute next_page_path.



11
12
13
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 11

def next_page_path
  @next_page_path
end

#pageObject (readonly)

Returns the value of attribute page.



11
12
13
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 11

def page
  @page
end

#page_pathObject (readonly)

Returns the value of attribute page_path.



11
12
13
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 11

def page_path
  @page_path
end

#page_trailObject

Returns the value of attribute page_trail.



14
15
16
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 14

def page_trail
  @page_trail
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



11
12
13
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 11

def per_page
  @per_page
end

#previous_pageObject (readonly)

Returns the value of attribute previous_page.



11
12
13
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 11

def previous_page
  @previous_page
end

#previous_page_pathObject (readonly)

Returns the value of attribute previous_page_path.



11
12
13
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 11

def previous_page_path
  @previous_page_path
end

#total_documentsObject (readonly)

Returns the value of attribute total_documents.



11
12
13
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 11

def total_documents
  @total_documents
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



11
12
13
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 11

def total_pages
  @total_pages
end

Instance Method Details

#[]Object

Delegates to resources[]



135
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 135

def [](...) = resources.[](...)

#deconstructObject

Delegates to resources.deconstruct



132
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 132

def deconstruct = resources.deconstruct

#eachObject

Iterate over Resources by delegating to resources.each (supports Enumerable)



126
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 126

def each(...) = resources.each(...)

#lastObject

Delegates to resources.last



129
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 129

def last(...) = resources.last(...)

#resourcesObject

TODO: eventually deprecate documents and only have resources



92
93
94
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 92

def resources
  documents
end

#to_liquidObject

Convert this Paginator’s data to a Hash suitable for use by Liquid.

Returns the Hash representation of this Paginator.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 103

def to_liquid
  {
    "per_page"           => per_page,
    "documents"          => documents,
    "resources"          => documents,
    "total_documents"    => total_documents,
    "total_resources"    => total_resources,
    "total_pages"        => total_pages,
    "page"               => page,
    "page_path"          => page_path,
    "previous_page"      => previous_page,
    "previous_page_path" => previous_page_path,
    "next_page"          => next_page,
    "next_page_path"     => next_page_path,
    "first_page"         => first_page,
    "first_page_path"    => first_page_path,
    "last_page"          => last_page,
    "last_page_path"     => last_page_path,
    "page_trail"         => page_trail,
  }
end

#total_resourcesObject



96
97
98
# File 'bridgetown-paginate/lib/bridgetown-paginate/paginator.rb', line 96

def total_resources
  total_documents
end