Module: PeijiSan::ViewHelper
- Defined in:
- lib/peiji_san/view_helper.rb
Overview
Optionally defines the peiji_san_options method in your helper to override the default options.
Include the PeijiSan::ViewHelper into one of your view helpers to acquire PeijiSan superpowers in your view.
class ApplicationHelper
include PeijiSan::ViewHelper
end
@collection = Member.active.page(2)
<% pages_to_link_to(@collection).each do |page %>
<%= page.is_a?(String) ? page : link_to_page(page, @collection) %>
<% end %>
Constant Summary collapse
- DEFAULT_PEIJI_SAN_OPTIONS =
The default options for link_to_page and pages_to_link_to.
{ # For link_to_page :page_parameter => :page, :anchor => nil, :current_class => :current, # For pages_to_link_to :max_visible => 11, :separator => '…' }
Instance Method Summary collapse
-
#link_to(*arguments) ⇒ Object
This Rails method is overridden to provide compatibility with other frameworks.
-
#link_to_page(page, paginated_set, options = {}, html_options = {}) ⇒ Object
Creates a link using
link_to
for a page in a pagination collection. -
#pages_to_link_to(paginated_set, options = {}) ⇒ Object
Returns an array of pages to link to.
-
#peiji_san_options ⇒ Object
Override this method in your helper to override default values:.
Instance Method Details
#link_to(*arguments) ⇒ Object
This Rails method is overridden to provide compatibility with other frameworks. By default it will just call super if super is provided. However, you will need your application to provide a standard url_for method in the context where this helper is used. For that you could use github.com/emk/sinatra-url-for/
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/peiji_san/view_helper.rb', line 73 def link_to(*arguments) return super if defined?(super) # What follows is a very simplistic implementation of link_to link_text, url, = arguments[0..2] [:href] = url attr_string = .keys.sort.map do |attribute| '%s="%s"' % [Rack::Utils.escape_html(attribute), Rack::Utils.escape_html([attribute])] end.join(' ') # Compose the tag return "<a %s>%s</a>" % [attr_string, Rack::Utils::escape_html(link_text)] end |
#link_to_page(page, paginated_set, options = {}, html_options = {}) ⇒ Object
Creates a link using link_to
for a page in a pagination collection. If the specified page is the current page then its class will be ‘current’.
Options:
[:page_parameter]
The name of the GET parameter used to indicate the page to display.
Defaults to <tt>:page</tt>.
[:current_class]
The CSS class name used when a page is the current page in a pagination
collection. Defaults to <tt>:current</tt>.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/peiji_san/view_helper.rb', line 48 def link_to_page(page, paginated_set, = {}, = {}) page_parameter = peiji_san_option(:page_parameter, ) # Sinatra/Rails differentiator pageable_params = respond_to?(:controller) ? controller.params : self.params = (page == 1 ? pageable_params.except(page_parameter) : pageable_params.merge(page_parameter => page)) anchor = peiji_san_option(:anchor, ) [:anchor] = anchor if anchor [:class] = peiji_san_option(:current_class, ) if paginated_set.current_page?(page) # Again a little fork here = if respond_to?(:controller) # Rails url_for() else # Sinatra root_path = env['PATH_INFO'].blank? ? "/" : env["PATH_INFO"] url_for(root_path, ) end link_to page, , end |
#pages_to_link_to(paginated_set, options = {}) ⇒ Object
Returns an array of pages to link to. This array includes the separator, so make sure to keep this in mind when iterating over the array and creating links.
For consistency’s sake, it is adviced to use an odd number for :max_visible
.
Options:
[:max_visible]
The maximum amount of elements in the array, this includes the
separator(s). Defaults to 11.
[:separator]
The separator string used to indicate a range between the first or last
page and the ones surrounding the current page.
collection = Model.all.page(40)
collection.page_count # => 80
pages_to_link_to(collection) # => [1, '…', 37, 38, 39, 40, 41, 42, 43, '…', 80]
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/peiji_san/view_helper.rb', line 106 def pages_to_link_to(paginated_set, = {}) current, last = paginated_set.current_page, paginated_set.page_count max = peiji_san_option(:max_visible, ) separator = peiji_san_option(:separator, ) if last <= max (1..last).to_a elsif current <= ((max / 2) + 1) (1..(max - 2)).to_a + [separator, last] elsif current >= (last - (max / 2)) [1, separator, *((last - (max - 3))..last)] else offset = (max - 4) / 2 [1, separator] + ((current - offset)..(current + offset)).to_a + [separator, last] end end |
#peiji_san_options ⇒ Object
Override this method in your helper to override default values:
def
{ :max_visible => 7 }
end
35 36 |
# File 'lib/peiji_san/view_helper.rb', line 35 def end |