Module: HtmlSlicer::ActionViewExtension

Defined in:
lib/html_slicer/helpers/action_view_extension.rb

Overview

This part of code is almost completely ported from Kaminari gem by Akira Matsuda. Look at github.com/amatsuda/kaminari/tree/master/lib/kaminari/helpers

The part of code, processing the :param_name was rewritten by me. Now you can define :param_name as a symbol or string, or as an +array of any object that responses .to_s method and returns string. Passing array is the way to define nested :param_name.

Examples:

:param_name => :page
# means you define params[:page] as a slice key.

:param_name => [:article, :page]
# means you define params[:article][:page] as a slice key.

Instance Method Summary collapse

Instance Method Details

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

Examples

Basic usage:

<%= link_to_next_slice @article.paged, 'Next page' %>

Ajax:

<%= link_to_next_slice @article.paged, 'Next page', :remote => true %>

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

<%= link_to_next_slice @article.paged, 'Next page' do %>
  <span>No More slices</span>
<% end %>


55
56
57
58
59
60
61
# File 'lib/html_slicer/helpers/action_view_extension.rb', line 55

def link_to_next_slice(object, name, options = {}, &block)
  params = options[:params] ? self.params.merge(options.delete :params) : self.params
  param_name = options.delete(:param_name) || object.options.param_name
  link_to_unless object.last_slice?, name, params.merge_hashup(*param_name, object.current_slice + 1), options.reverse_merge(:rel => 'next') do
    block.call if block
  end
end

#slice(object, options = {}, &block) ⇒ Object

A helper that renders the pagination links.

<%= slicer @article.paged %>

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 slice number in the links. Accepts symbol, string, array.

  • :remote - Ajax? (false by default)

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



33
34
35
36
# File 'lib/html_slicer/helpers/action_view_extension.rb', line 33

def slice(object, options = {}, &block)
  slicer = HtmlSlicer::Helpers::Slicer.new self, object.options.reverse_merge(options).reverse_merge(:current_slice => object.current_slice, :slice_number => object.slice_number, :remote => false)
  slicer.to_s
end

#slice_entries_info(object, options = {}) ⇒ Object

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

Examples

Basic usage:

<%= slice_entries_info @article.paged %>
#-> Displaying paged 6 of 26

By default, the message will use the stringified method_name (:as option) implemented as slicer method. Override this with the :entry_name parameter:

<%= slice_entries_info @article.paged, :entry_name => 'page' %>
#-> Displaying page 6 of 26


77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/html_slicer/helpers/action_view_extension.rb', line 77

def slice_entries_info(object, options = {})
  entry_name = options[:entry_name] || object.options.as
  output = ""
  if object.slice_number < 2
    output = case object.slice_number
    when 0 then "No #{entry_name} found"
    when 1 then "Displaying <b>1</b> #{entry_name}"
    else; "Displaying <b>all #{object.slice_number}</b> #{entry_name.to_s.pluralize}"
    end
  else
    output = %{Displaying #{entry_name} <b>#{object.current_slice}</b> of <b>#{object.slice_number}</b>}
  end
  output.html_safe
end