Module: BootstrapPager::ActionViewExtension

Included in:
BootstrapPagerHelper
Defined in:
lib/bootstrap_pager/helpers/action_view_extension.rb

Overview

Helpers

Instance Method Summary collapse

Instance Method Details

A simple “Twitter like” pagination link that creates a link to the next page.

Examples

Basic usage:

<%= link_to_next_page @items, 'Next Page' %>

Ajax:

<%= link_to_next_page @items, 'Next Page', :remote => true %>

By default, it renders nothing if there are no more results on the next page. You can customize this output by passing a block.

<%= link_to_next_page @users, 'Next Page' do %>
  <span>No More Pages</span>
<% end %>

67
68
69
70
71
72
73
74
75
# File 'lib/bootstrap_pager/helpers/action_view_extension.rb', line 67

def link_to_next_page(scope, name, options = {}, &block)
  params = options.delete(:params) || {}
  param_name = options.delete(:param_name) || BootstrapPager.config.param_name
  engine_namespace = options.delete(:engine_namespace)
  url = (engine_namespace || self).url_for params.merge(param_name => (scope.current_page + 1))
  link_to_unless scope.last_page?, name, url, options.reverse_merge(:rel => 'next') do
    block.call if block
  end
end

A simple “Twitter like” pagination link that creates a link to the previous page.

Examples

Basic usage:

<%= link_to_previous_page @items, 'Previous Page' %>

Ajax:

<%= link_to_previous_page @items, 'Previous Page', :remote => true %>

By default, it renders nothing if there are no more results on the previous page. You can customize this output by passing a block.

<%= link_to_previous_page @users, 'Previous Page' do %>
  <span>At the Beginning</span>
<% end %>

40
41
42
43
44
45
46
47
48
# File 'lib/bootstrap_pager/helpers/action_view_extension.rb', line 40

def link_to_previous_page(scope, name, options = {}, &block)
  params = options.delete(:params) || {}
  param_name = options.delete(:param_name) || BootstrapPager.config.param_name
  engine_namespace = options.delete(:engine_namespace)
  url = (engine_namespace || self).url_for params.merge(param_name => (scope.current_page - 1))
  link_to_unless scope.first_page?, name, url, options.reverse_merge(:rel => 'previous') do
    block.call if block
  end
end

#page_entries_info(collection, options = {}) ⇒ Object

Renders a helpful message with numbers of displayed vs. total entries. Ported from mislav/will_paginate

Examples

Basic usage:

<%= page_entries_info @posts %>
#-> Displaying posts 6 - 10 of 26 in total

By default, the message will use the humanized class name of objects in collection: for instance, “project types” for ProjectType models. The namespace will be cutted out and only the last name will be used. Override this with the :entry_name parameter:

<%= page_entries_info @posts, :entry_name => 'item' %>
#-> Displaying items 6 - 10 of 26 in total

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bootstrap_pager/helpers/action_view_extension.rb', line 93

def page_entries_info(collection, options = {})
  entry_name = if options[:entry_name]
    options[:entry_name]
  elsif collection.is_a?(::BootstrapPager::PaginatableArray)
    'entry'
  else
    if collection.respond_to? :model  # DataMapper
      collection.model.model_name.human.downcase
    else  # AR
      collection.model_name.human.downcase
    end
  end
  entry_name = entry_name.pluralize unless collection.total_count == 1

  if collection.total_pages < 2
    t('helpers.page_entries_info.one_page.display_entries', :entry_name => entry_name, :count => collection.total_count)
  else
    first = collection.offset_value + 1
    last = collection.last_page? ? collection.total_count : collection.offset_value + collection.limit_value
    t('helpers.page_entries_info.more_pages.display_entries', :entry_name => entry_name, :first => first, :last => last, :total => collection.total_count)
  end.html_safe
end

#paginate(scope, options = {}, &block) ⇒ Object

A helper that renders the pagination links.

<%= paginate @articles %>

Options

  • :window - The “inner window” size (4 by default).

  • :outer_window - The “outer window” size (0 by default).

  • :left - The “left outer window” size (0 by default).

  • :right - The “right outer window” size (0 by default).

  • :params - url_for parameters for the links (:controller, :action, etc.)

  • :param_name - parameter name for page number in the links (:page by default)

  • :remote - Ajax? (false by default)

  • :engine_namespace - namespace of router (current engine's namespace by default)

  • :ANY_OTHER_VALUES - Any other hash key & values would be directly passed into each tag as :locals value.


18
19
20
21
# File 'lib/bootstrap_pager/helpers/action_view_extension.rb', line 18

def paginate(scope, options = {}, &block)
  paginator = BootstrapPager::Helpers::Paginator.new self, options.reverse_merge(:current_page => scope.current_page, :total_pages => scope.total_pages, :per_page => scope.limit_value, :param_name => BootstrapPager.config.param_name, :remote => false)
  paginator.to_s
end

Renders rel=“next” and rel=“prev” links to be used in the head.

Examples

Basic usage:

In head:
<head>
  <title>My Website</title>
  <%= yield :head %>
</head>

Somewhere in body:
<% content_for :head do %>
  <%= rel_next_prev_link_tags @items %>
<% end %>

#-> <link rel="next" href="/items/page/3" /><link rel="prev" href="/items/page/1" />

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/bootstrap_pager/helpers/action_view_extension.rb', line 134

def rel_next_prev_link_tags(scope, options = {})
  params = options.delete(:params) || {}
  param_name = options.delete(:param_name) || BootstrapPager.config.param_name
  engine_namespace = options.delete(:engine_namespace)

  output = ""

  unless scope.last_page?
    output << '<link rel="next" href="' + (engine_namespace || self).url_for(params.merge(param_name => (scope.current_page + 1))) + '"/>'
  end
  unless scope.first_page?
    output << '<link rel="prev" href="' + (engine_namespace || self).url_for(params.merge(param_name => (scope.current_page - 1))) + '"/>'
  end

  output.html_safe
end