Class: Foliate::Pagination

Inherits:
Object
  • Object
show all
Defined in:
lib/foliate/pagination.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actionSymbol

Returns originating controller action.

Returns:

  • (Symbol)

    originating controller action



24
25
26
# File 'lib/foliate/pagination.rb', line 24

def action
  @action
end

#controllerSymbol

Returns originating controller.

Returns:

  • (Symbol)

    originating controller



20
21
22
# File 'lib/foliate/pagination.rb', line 20

def controller
  @controller
end

#current_pageInteger

Returns current page number.

Returns:

  • (Integer)

    current page number



8
9
10
# File 'lib/foliate/pagination.rb', line 8

def current_page
  @current_page
end

#per_pageInteger

Returns number of records per page.

Returns:

  • (Integer)

    number of records per page



12
13
14
# File 'lib/foliate/pagination.rb', line 12

def per_page
  @per_page
end

#query_paramsHash

Returns originating request query params.

Returns:

  • (Hash)

    originating request query params



28
29
30
# File 'lib/foliate/pagination.rb', line 28

def query_params
  @query_params
end

#total_recordsInteger

Returns total number of records.

Returns:

  • (Integer)

    total number of records



16
17
18
# File 'lib/foliate/pagination.rb', line 16

def total_records
  @total_records
end

Instance Method Details

#each_query_param {|name, value| ... } ⇒ nil #each_query_paramEnumerator

Iterates through each #query_params as name-value pairs. Nested Hashes and Arrays are iterated as well. The name of nested a value will reflect its nesting in the same way as when converting a nested Hash to a query string via Hash#to_query. Intended for use when generating form fields.

If no block is given, an Enumerator is returned.

Overloads:

  • #each_query_param {|name, value| ... } ⇒ nil

    Yield Parameters:

    • name (String)
    • value (String)

    Returns:

    • (nil)
  • #each_query_paramEnumerator

    Returns:

    • (Enumerator)


61
62
63
64
65
66
67
68
69
70
# File 'lib/foliate/pagination.rb', line 61

def each_query_param
  if block_given?
    ParamsHelper.to_keyed_pairs(query_params).each do |pair|
      yield pair[:name], pair[:value]
    end
    nil
  else
    to_enum(__method__)
  end
end

#next?Boolean

Indicates if there is a page expected after the current page.

Returns:

  • (Boolean)


97
98
99
# File 'lib/foliate/pagination.rb', line 97

def next?
  current_page < total_pages
end

#next_page_paramsHash?

Linking params for the next page. Returns nil if #next? is false. See also #params_for_page.

Examples:

usage in view

link_to_if @pagination.next?, "Next", @pagination.next_page_params

Returns:

  • (Hash, nil)


119
120
121
# File 'lib/foliate/pagination.rb', line 119

def next_page_params
  params_for_page(current_page + 1) if next?
end

#offsetInteger

Computes the record set offset based on #current_page and #per_page.

Returns:

  • (Integer)


42
43
44
# File 'lib/foliate/pagination.rb', line 42

def offset
  (current_page - 1) * per_page
end

#params_for_page(page_number) ⇒ Hash

Returns linking params for a specified page_number. The returned Hash is intended for use with link_to, url_for, etc.

Examples:

usage in view

link_to "First", @pagination.params_for_page(1)
link_to "Last", @pagination.params_for_page(@pagination.total_pages)

Parameters:

  • page_number (Integer)

Returns:

  • (Hash)


81
82
83
84
85
# File 'lib/foliate/pagination.rb', line 81

def params_for_page(page_number)
  { controller: controller, action: action, params: query_params.merge(
      Foliate.config.page_param => (page_number if page_number > 1)
  ) }
end

#prev?Boolean

Indicates if there is a page expected before the current page.

Returns:

  • (Boolean)


90
91
92
# File 'lib/foliate/pagination.rb', line 90

def prev?
  current_page > 1
end

#prev_page_paramsHash?

Linking params for the previous page. Returns nil if #prev? is false. See also #params_for_page.

Examples:

usage in view

link_to_if @pagination.prev?, "Previous", @pagination.prev_page_params

Returns:

  • (Hash, nil)


108
109
110
# File 'lib/foliate/pagination.rb', line 108

def prev_page_params
  params_for_page(current_page - 1) if prev?
end

#to_partial_pathString

Path to the view partial. This method exists to allow Pagination objects to be passed directly to render calls in the view.

Examples:

rendering view partial from view

render @pagination

Returns:

  • (String)


131
132
133
# File 'lib/foliate/pagination.rb', line 131

def to_partial_path
  "pagination/pagination"
end

#total_pagesInteger

Computes the total number of pages based on #total_records and #per_page.

Returns:

  • (Integer)


34
35
36
# File 'lib/foliate/pagination.rb', line 34

def total_pages
  (total_records / per_page.to_f).ceil
end