The TextHelper module provides a set of methods for filtering, formatting
and transforming strings, which can reduce the amount of inline Ruby code
in your views. These helper methods extend ActionView making them callable
within your template files.
| Public Methods |
| auto_link |
Turns all URLs and e-mail addresses into clickable links. The link
parameter will limit what should be linked. You can add HTML attributes to
the links using href_options. Options for link are
:all (default), :email_addresses, and :urls. If
a block is given, each URL and e-mail address is yielded and the result is
used as the link text.
|
| concat |
The preferred method of outputting text in your views is to use the <%=
"text" %> eRuby syntax. The regular puts and
print methods do not operate as expected in an eRuby code block.
If you absolutely must output text within a non-output code block (i.e.,
<% %>), you can use the concat method.
|
| cycle |
Creates a Cycle object whose to_s method cycles through elements
of an array every time it is called. This can be used for example, to
alternate classes for table rows. You can use named cycles to allow nesting
in loops. Passing a Hash as the last parameter with a :name key
will create a named cycle. You can manually reset a cycle by calling
reset_cycle and passing the name of the cycle.
|
| excerpt |
Extracts an excerpt from text that matches the first instance of
phrase. The radius expands the excerpt on each side of
the first occurrence of phrase by the number of characters defined
in radius (which defaults to 100). If the excerpt radius overflows
the beginning or end of the text, then the excerpt_string
will be prepended/appended accordingly. If the phrase isn’t
found, nil is returned.
|
| highlight |
Highlights one or more phrases everywhere in text by
inserting it into a highlighter string. The highlighter can be
specialized by passing highlighter as a single-quoted string with
\1 where the phrase is to be inserted (defaults to
’<strong>\1</strong>’)
|
| markdown |
Returns the text with all the Markdown codes turned into HTML tags.
This method is only available if BlueCloth is
available.
|
| pluralize |
Attempts to pluralize the singular word unless count is
1. If plural is supplied, it will use that when count is > 1,
if the ActiveSupport Inflector is loaded, it will use the Inflector to
determine the plural form, otherwise it will just add an ’s’ to
the singular word.
|
| reset_cycle |
Resets a cycle so that it starts from the first element the next time it is
called. Pass in name to reset a named cycle.
|
| simple_format |
Returns text transformed into HTML using simple formatting rules.
Two or more consecutive newlines(\n\n) are considered as a
paragraph and wrapped in <p></p> tags. One newline
(\n) is considered as a linebreak and a <br /> tag
is appended. This method does not remove the newlines from the
text.
|
| textilize |
Returns the text with all the Textile codes turned into
HTML tags.
|
| textilize_without_paragraph |
Returns the text with all the Textile codes turned into HTML tags, but
without the bounding <p> tag that RedCloth adds.
|
| truncate |
If text is longer than length, text will be
truncated to the length of length (defaults to 30) and the last
characters will be replaced with the truncate_string (defaults to
"…").
|
| word_wrap |
Wraps the text into lines no longer than line_width
width. This method breaks on the first whitespace character that does not
exceed line_width (which is 80 by default).
|
| Private Methods |
| auto_link_email_addresses |
Turns all email addresses into clickable links. If a block is given, each
email is yielded and the result is used as the link text.
|
| auto_link_urls |
Turns all urls into clickable links. If a block is given, each url is
yielded and the result is used as the link text.
|
| get_cycle |
The cycle helpers need to store the cycles in a place that is guaranteed to
be reset every time a page is rendered, so it uses an instance variable of
ActionView::Base.
|
| set_cycle |
|
<code/>and<pre/>for code samples.