2007-01-28 02:16:55 -05:00
require 'action_view/helpers/javascript_helper'
2009-09-13 18:25:40 -04:00
require 'active_support/core_ext/array/access'
2009-06-08 22:35:30 -04:00
require 'active_support/core_ext/hash/keys'
2010-03-16 20:32:42 -04:00
require 'action_dispatch'
2005-08-14 04:43:07 -04:00
2004-11-23 20:04:44 -05:00
module ActionView
2010-06-16 14:24:53 -04:00
# = Action View URL Helpers
2006-10-22 19:54:41 -04:00
module Helpers #:nodoc:
2007-05-06 00:29:42 -04:00
# Provides a set of methods for making links and getting URLs that
2010-02-21 11:21:25 -05:00
# depend on the routing subsystem (see ActionDispatch::Routing).
2008-07-19 14:06:43 -04:00
# This allows you to use the same format for links in views
2007-05-06 00:29:42 -04:00
# and controllers.
2004-11-23 20:04:44 -05:00
module UrlHelper
2010-04-03 23:23:23 -04:00
# This helper may be included in any class that includes the
# URL helpers of a router (router.url_helpers). Some methods
2010-06-20 16:20:26 -04:00
# provided here will only work in the4 context of a request
2010-04-03 23:23:23 -04:00
# (link_to_unless_current, for instance), which must be provided
# as a method called #request on the context.
2010-03-16 17:55:42 -04:00
extend ActiveSupport :: Concern
include ActionDispatch :: Routing :: UrlFor
2010-04-03 05:30:06 -04:00
include TagHelper
2007-03-13 01:12:59 -04:00
2010-01-07 09:26:31 -05:00
# Need to map default url options to controller one.
2010-04-03 05:30:06 -04:00
# def default_url_options(*args) #:nodoc:
# controller.send(:default_url_options, *args)
# end
#
2010-03-16 17:55:42 -04:00
def url_options
2010-04-03 05:30:06 -04:00
return super unless controller . respond_to? ( :url_options )
2010-03-16 17:55:42 -04:00
controller . url_options
end
2007-03-13 01:12:59 -04:00
# Returns the URL for the set of +options+ provided. This takes the
2008-05-25 07:29:00 -04:00
# same options as +url_for+ in Action Controller (see the
2009-07-25 11:03:58 -04:00
# documentation for <tt>ActionController::Base#url_for</tt>). Note that by default
# <tt>:only_path</tt> is <tt>true</tt> so you'll get the relative "/controller/action"
# instead of the fully qualified URL like "http://example.com/controller/action".
2007-03-13 01:12:59 -04:00
#
2007-05-06 00:29:42 -04:00
# ==== Options
2008-05-09 05:38:02 -04:00
# * <tt>:anchor</tt> - Specifies the anchor name to be appended to the path.
# * <tt>:only_path</tt> - If true, returns the relative URL (omitting the protocol, host name, and port) (<tt>true</tt> by default unless <tt>:host</tt> is specified).
# * <tt>:trailing_slash</tt> - If true, adds a trailing slash, as in "/archive/2005/". Note that this
2007-05-06 00:29:42 -04:00
# is currently not recommended since it breaks caching.
2008-05-09 05:38:02 -04:00
# * <tt>:host</tt> - Overrides the default (current) host if provided.
# * <tt>:protocol</tt> - Overrides the default (current) protocol if provided.
# * <tt>:user</tt> - Inline HTTP authentication (only plucked out if <tt>:password</tt> is also present).
# * <tt>:password</tt> - Inline HTTP authentication (only plucked out if <tt>:user</tt> is also present).
2007-05-06 00:29:42 -04:00
#
2007-05-12 17:12:31 -04:00
# ==== Relying on named routes
#
# If you instead of a hash pass a record (like an Active Record or Active Resource) as the options parameter,
# you'll trigger the named route for that record. The lookup will happen on the name of the class. So passing
2009-07-25 11:03:58 -04:00
# a Workshop object will attempt to use the +workshop_path+ route. If you have a nested route, such as
# +admin_workshop_path+ you'll have to call that explicitly (it's impossible for +url_for+ to guess that route).
2007-05-12 17:12:31 -04:00
#
2007-05-06 00:29:42 -04:00
# ==== Examples
# <%= url_for(:action => 'index') %>
# # => /blog/
#
# <%= url_for(:action => 'find', :controller => 'books') %>
# # => /books/find
#
# <%= url_for(:action => 'login', :controller => 'members', :only_path => false, :protocol => 'https') %>
# # => https://www.railsapplication.com/members/login/
#
# <%= url_for(:action => 'play', :anchor => 'player') %>
# # => /messages/play/#player
#
2010-06-29 13:55:57 -04:00
# <%= url_for(:action => 'jump', :anchor => 'tax&ship') %>
2007-05-06 00:29:42 -04:00
# # => /testing/jump/#tax&ship
2007-05-12 17:12:31 -04:00
#
# <%= url_for(Workshop.new) %>
2010-02-21 05:09:21 -05:00
# # relies on Workshop answering a persisted? call (and in this case returning false)
2007-05-12 17:12:31 -04:00
# # => /workshops
#
# <%= url_for(@workshop) %>
# # calls @workshop.to_s
# # => /workshops/5
2008-07-19 14:06:43 -04:00
#
# <%= url_for("http://www.example.com") %>
# # => http://www.example.com
#
# <%= url_for(:back) %>
# # if request.env["HTTP_REFERER"] is set to "http://www.example.com"
# # => http://www.example.com
#
# <%= url_for(:back) %>
# # if request.env["HTTP_REFERER"] is not set or is blank
# # => javascript:history.back()
2007-05-12 17:12:31 -04:00
def url_for ( options = { } )
2008-06-23 08:51:38 -04:00
options || = { }
2008-07-19 14:06:43 -04:00
url = case options
when String
options
2007-05-12 17:12:31 -04:00
when Hash
2010-04-22 11:35:44 -04:00
options = { :only_path = > options [ :host ] . nil? } . update ( options . symbolize_keys )
2010-04-03 05:30:06 -04:00
super
2008-07-19 14:06:43 -04:00
when :back
2010-01-22 11:18:19 -05:00
controller . request . env [ " HTTP_REFERER " ] || 'javascript:history.back()'
2007-05-12 17:12:31 -04:00
else
2008-07-19 14:06:43 -04:00
polymorphic_path ( options )
2006-03-22 14:41:39 -05:00
end
2006-05-28 19:39:37 -04:00
2010-06-29 13:55:57 -04:00
url
2004-11-23 20:04:44 -05:00
end
2007-03-13 01:12:59 -04:00
# Creates a link tag of the given +name+ using a URL created by the set
# of +options+. See the valid options in the documentation for
2009-07-25 11:03:58 -04:00
# +url_for+. It's also possible to pass a string instead
2007-03-13 01:12:59 -04:00
# of an options hash to get a link tag that uses the value of the string as the
2008-04-04 23:52:58 -04:00
# href for the link, or use <tt>:back</tt> to link to the referrer - a JavaScript back
2009-07-25 11:03:58 -04:00
# link will be used in place of a referrer if none exists. If +nil+ is passed as
2007-10-07 23:30:29 -04:00
# a name, the link itself will become the name.
2005-09-03 20:33:45 -04:00
#
2008-06-17 15:01:37 -04:00
# ==== Signatures
#
2010-04-03 05:30:06 -04:00
# link_to(body, url, html_options = {})
# # url is a String; you can use URL helpers like
# # posts_path
#
# link_to(body, url_options = {}, html_options = {})
# # url_options, except :confirm or :method,
# # is passed to url_for
#
# link_to(options = {}, html_options = {}) do
# # name
# end
#
# link_to(url, html_options = {}) do
2008-06-17 15:01:37 -04:00
# # name
# end
#
2007-05-06 00:29:42 -04:00
# ==== Options
2010-04-03 05:30:06 -04:00
# * <tt>:confirm => 'question?'</tt> - This will allow the unobtrusive JavaScript
2010-01-31 17:30:21 -05:00
# driver to prompt with the question specified. If the user accepts, the link is
2006-10-22 19:54:41 -04:00
# processed normally, otherwise no action is taken.
2008-05-09 05:38:02 -04:00
# * <tt>:method => symbol of HTTP verb</tt> - This modifier will dynamically
2007-03-13 01:12:59 -04:00
# create an HTML form and immediately submit the form for processing using
2006-10-22 19:54:41 -04:00
# the HTTP verb specified. Useful for having links perform a POST operation
# in dangerous actions like deleting a record (which search bots can follow
2008-05-02 09:45:23 -04:00
# while spidering your site). Supported verbs are <tt>:post</tt>, <tt>:delete</tt> and <tt>:put</tt>.
2007-03-13 01:12:59 -04:00
# Note that if the user has JavaScript disabled, the request will fall back
2009-07-25 11:03:58 -04:00
# to using GET. If <tt>:href => '#'</tt> is used and the user has JavaScript
# disabled clicking the link will have no effect. If you are relying on the
# POST behavior, you should check for it in your controller's action by using
# the request object's methods for <tt>post?</tt>, <tt>delete?</tt> or <tt>put?</tt>.
2010-04-03 05:30:06 -04:00
# * <tt>:remote => true</tt> - This will allow the unobtrusive JavaScript
# driver to make an Ajax request to the URL in question instead of following
# the link. The drivers each provide mechanisms for listening for the
# completion of the Ajax request and performing JavaScript operations once
# they're complete
2005-09-03 20:33:45 -04:00
#
2007-05-06 00:29:42 -04:00
# ==== Examples
2008-05-16 18:01:32 -04:00
# Because it relies on +url_for+, +link_to+ supports both older-style controller/action/id arguments
# and newer RESTful routes. Current Rails style favors RESTful routes whenever possible, so base
# your application on resources and use
#
# link_to "Profile", profile_path(@profile)
# # => <a href="/profiles/1">Profile</a>
#
# or the even pithier
#
# link_to "Profile", @profile
# # => <a href="/profiles/1">Profile</a>
#
# in place of the older more verbose, non-resource-oriented
#
# link_to "Profile", :controller => "profiles", :action => "show", :id => @profile
# # => <a href="/profiles/show/1">Profile</a>
2008-07-19 14:06:43 -04:00
#
# Similarly,
2008-05-16 18:01:32 -04:00
#
# link_to "Profiles", profiles_path
# # => <a href="/profiles">Profiles</a>
#
# is better than
#
# link_to "Profiles", :controller => "profiles"
# # => <a href="/profiles">Profiles</a>
#
2008-06-17 15:01:37 -04:00
# You can use a block as well if your link target is hard to fit into the name parameter. ERb example:
#
2010-03-12 07:17:41 -05:00
# <%= link_to(@profile) do %>
2009-07-25 11:03:58 -04:00
# <strong><%= @profile.name %></strong> -- <span>Check it out!</span>
2008-06-17 15:01:37 -04:00
# <% end %>
2009-07-25 11:03:58 -04:00
# # => <a href="/profiles/1">
# <strong>David</strong> -- <span>Check it out!</span>
# </a>
2008-06-17 15:01:37 -04:00
#
2008-05-16 18:01:32 -04:00
# Classes and ids for CSS are easy to produce:
#
# link_to "Articles", articles_path, :id => "news", :class => "article"
# # => <a href="/articles" class="article" id="news">Articles</a>
#
# Be careful when using the older argument style, as an extra literal hash is needed:
#
# link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article"
# # => <a href="/articles" class="article" id="news">Articles</a>
#
# Leaving the hash off gives the wrong link:
#
# link_to "WRONG!", :controller => "articles", :id => "news", :class => "article"
# # => <a href="/articles/index/news?class=article">WRONG!</a>
#
# +link_to+ can also produce links with anchors or query strings:
#
# link_to "Comment wall", profile_path(@profile, :anchor => "wall")
# # => <a href="/profiles/1#wall">Comment wall</a>
#
# link_to "Ruby on Rails search", :controller => "searches", :query => "ruby on rails"
# # => <a href="/searches?query=ruby+on+rails">Ruby on Rails search</a>
#
# link_to "Nonsense search", searches_path(:foo => "bar", :baz => "quux")
# # => <a href="/searches?foo=bar&baz=quux">Nonsense search</a>
#
2010-01-31 17:30:21 -05:00
# The two options specific to +link_to+ (<tt>:confirm</tt> and <tt>:method</tt>) are used as follows:
2008-05-16 18:01:32 -04:00
#
2006-10-22 19:54:41 -04:00
# link_to "Visit Other Site", "http://www.rubyonrails.org/", :confirm => "Are you sure?"
2010-01-31 17:30:21 -05:00
# # => <a href="http://www.rubyonrails.org/" data-confirm="Are you sure?"">Visit Other Site</a>
#
# link_to("Destroy", "http://www.example.com", :method => :delete, :confirm => "Are you sure?")
# # => <a href='http://www.example.com' rel="nofollow" data-method="delete" data-confirm="Are you sure?">Destroy</a>
2008-06-17 15:01:37 -04:00
def link_to ( * args , & block )
if block_given?
options = args . first || { }
html_options = args . second
2010-03-16 02:26:48 -04:00
link_to ( capture ( & block ) , options , html_options )
2008-06-17 15:01:37 -04:00
else
2009-06-08 16:33:18 -04:00
name = args [ 0 ]
options = args [ 1 ] || { }
html_options = args [ 2 ]
2008-06-17 15:01:37 -04:00
2010-01-30 20:44:35 -05:00
html_options = convert_options_to_data_attributes ( options , html_options )
2010-04-03 05:30:06 -04:00
url = url_for ( options )
2008-06-17 15:01:37 -04:00
if html_options
html_options = html_options . stringify_keys
href = html_options [ 'href' ]
tag_options = tag_options ( html_options )
2007-10-07 23:30:29 -04:00
else
2008-06-17 15:01:37 -04:00
tag_options = nil
2007-10-07 23:30:29 -04:00
end
2008-07-19 14:06:43 -04:00
2010-06-29 13:55:57 -04:00
href_attr = " href= \" #{ escape_once ( url ) } \" " unless href
" <a #{ href_attr } #{ tag_options } > #{ html_escape ( name || url ) } </a> " . html_safe
2004-11-23 20:04:44 -05:00
end
end
2006-10-22 19:54:41 -04:00
# Generates a form containing a single button that submits to the URL created
# by the set of +options+. This is the safest method to ensure links that
# cause changes to your data are not triggered by search bots or accelerators.
# If the HTML button does not work with your layout, you can also consider
2009-07-25 11:03:58 -04:00
# using the +link_to+ method with the <tt>:method</tt> modifier as described in
# the +link_to+ documentation.
2005-06-16 02:17:51 -04:00
#
2010-04-08 15:17:46 -04:00
# The generated form element has a class name of <tt>button_to</tt>
2006-10-22 19:54:41 -04:00
# to allow styling of the form itself and its children. You can control
# the form submission and input element behavior using +html_options+.
# This method accepts the <tt>:method</tt> and <tt>:confirm</tt> modifiers
2009-07-25 11:03:58 -04:00
# described in the +link_to+ documentation. If no <tt>:method</tt> modifier
2007-03-13 01:12:59 -04:00
# is given, it will default to performing a POST operation. You can also
2006-10-22 19:54:41 -04:00
# disable the button by passing <tt>:disabled => true</tt> in +html_options+.
# If you are using RESTful routes, you can pass the <tt>:method</tt>
# to change the HTTP verb used to submit the form.
2005-06-16 02:17:51 -04:00
#
2007-05-06 00:29:42 -04:00
# ==== Options
2009-07-25 11:03:58 -04:00
# The +options+ hash accepts the same options as url_for.
2005-06-16 02:17:51 -04:00
#
2007-05-06 00:29:42 -04:00
# There are a few special +html_options+:
2008-05-09 05:38:02 -04:00
# * <tt>:method</tt> - Specifies the anchor name to be appended to the path.
# * <tt>:disabled</tt> - Specifies the anchor name to be appended to the path.
2010-04-03 05:30:06 -04:00
# * <tt>:confirm</tt> - This will use the unobtrusive JavaScript driver to
2007-05-06 00:29:42 -04:00
# prompt with the question specified. If the user accepts, the link is
# processed normally, otherwise no action is taken.
2010-04-03 05:30:06 -04:00
# * <tt>:remote</tt> - If set to true, will allow the Unobtrusive JavaScript drivers to control the
2010-01-31 17:30:21 -05:00
# submit behaviour. By default this behaviour is an ajax submit.
2008-07-19 14:06:43 -04:00
#
2007-05-06 00:29:42 -04:00
# ==== Examples
# <%= button_to "New", :action => "new" %>
2010-04-08 15:17:46 -04:00
# # => "<form method="post" action="/controller/new" class="button_to">
2007-05-06 00:29:42 -04:00
# # <div><input value="New" type="submit" /></div>
# # </form>"
2005-06-16 02:17:51 -04:00
#
2010-01-31 17:30:21 -05:00
#
# <%= button_to "Delete Image", { :action => "delete", :id => @image.id },
# :confirm => "Are you sure?", :method => :delete %>
2010-04-08 15:17:46 -04:00
# # => "<form method="post" action="/images/delete/1" class="button_to">
2007-05-06 00:29:42 -04:00
# # <div>
# # <input type="hidden" name="_method" value="delete" />
2010-01-31 17:30:21 -05:00
# # <input data-confirm='Are you sure?' value="Delete" type="submit" />
2007-05-06 00:29:42 -04:00
# # </div>
# # </form>"
2010-01-31 17:30:21 -05:00
#
#
2010-04-03 05:30:06 -04:00
# <%= button_to('Destroy', 'http://www.example.com', :confirm => 'Are you sure?',
2010-01-31 17:30:21 -05:00
# :method => "delete", :remote => true, :disable_with => 'loading...') %>
2010-04-08 15:17:46 -04:00
# # => "<form class='button_to' method='post' action='http://www.example.com' data-remote='true'>
2010-01-31 17:30:21 -05:00
# # <div>
# # <input name='_method' value='delete' type='hidden' />
# # <input value='Destroy' type='submit' disable_with='loading...' data-confirm='Are you sure?' />
# # </div>
# # </form>"
# #
2006-10-22 19:54:41 -04:00
def button_to ( name , options = { } , html_options = { } )
html_options = html_options . stringify_keys
2005-06-16 02:17:51 -04:00
convert_boolean_attributes! ( html_options , %w( disabled ) )
2006-09-03 11:59:18 -04:00
method_tag = ''
if ( method = html_options . delete ( 'method' ) ) && %w{ put delete } . include? ( method . to_s )
method_tag = tag ( 'input' , :type = > 'hidden' , :name = > '_method' , :value = > method . to_s )
end
form_method = method . to_s == 'get' ? 'get' : 'post'
2008-07-19 14:06:43 -04:00
2010-01-31 15:40:40 -05:00
remote = html_options . delete ( 'remote' )
2007-09-25 12:50:35 -04:00
request_token_tag = ''
2007-09-28 11:55:45 -04:00
if form_method == 'post' && protect_against_forgery?
2007-09-25 12:50:35 -04:00
request_token_tag = tag ( :input , :type = > " hidden " , :name = > request_forgery_protection_token . to_s , :value = > form_authenticity_token )
end
2008-07-19 14:06:43 -04:00
2006-10-22 19:54:41 -04:00
url = options . is_a? ( String ) ? options : self . url_for ( options )
2006-03-18 17:36:52 -05:00
name || = url
2010-01-29 21:00:55 -05:00
2010-01-30 20:44:35 -05:00
html_options = convert_options_to_data_attributes ( options , html_options )
2006-09-03 11:59:18 -04:00
2005-06-16 02:17:51 -04:00
html_options . merge! ( " type " = > " submit " , " value " = > name )
2004-12-09 18:18:25 -05:00
2010-04-08 15:17:46 -04:00
( " <form method= \" #{ form_method } \" action= \" #{ escape_once url } \" #{ " data-remote= \" true \" " if remote } class= \" button_to \" ><div> " +
For performance reasons, you can no longer call html_safe! on Strings. Instead, all Strings are always not html_safe?. Instead, you can get a SafeBuffer from a String by calling #html_safe, which will SafeBuffer.new(self).
* Additionally, instead of doing concat("</form>".html_safe), you can do
safe_concat("</form>"), which will skip both the flag set, and the flag
check.
* For the first pass, I converted virtually all #html_safe!s to #html_safe,
and the tests pass. A further optimization would be to try to use
#safe_concat as much as possible, reducing the performance impact if
we know up front that a String is safe.
2010-01-31 22:17:42 -05:00
method_tag + tag ( " input " , html_options ) + request_token_tag + " </div></form> " ) . html_safe
2004-12-09 18:18:25 -05:00
end
2005-02-19 06:33:32 -05:00
2006-10-22 19:54:41 -04:00
# Creates a link tag of the given +name+ using a URL created by the set of
2007-05-06 00:29:42 -04:00
# +options+ unless the current request URI is the same as the links, in
2006-10-22 19:54:41 -04:00
# which case only the name is returned (or the given block is yielded, if
2009-07-25 11:03:58 -04:00
# one exists). You can give +link_to_unless_current+ a block which will
2007-05-06 00:29:42 -04:00
# specialize the default behavior (e.g., show a "Start Here" link rather
# than the link's text).
#
# ==== Examples
# Let's say you have a navigation menu...
2006-10-22 19:54:41 -04:00
#
# <ul id="navbar">
# <li><%= link_to_unless_current("Home", { :action => "index" }) %></li>
# <li><%= link_to_unless_current("About Us", { :action => "about" }) %></li>
# </ul>
#
2007-05-06 00:29:42 -04:00
# If in the "about" action, it will render...
2006-10-22 19:54:41 -04:00
#
# <ul id="navbar">
# <li><a href="/controller/index">Home</a></li>
# <li>About Us</li>
# </ul>
2007-05-06 00:29:42 -04:00
#
2008-03-15 16:08:05 -04:00
# ...but if in the "index" action, it will render:
2007-05-06 00:29:42 -04:00
#
# <ul id="navbar">
2008-03-15 16:08:05 -04:00
# <li>Home</li>
2007-05-06 00:29:42 -04:00
# <li><a href="/controller/about">About Us</a></li>
# </ul>
#
2009-07-25 11:03:58 -04:00
# The implicit block given to +link_to_unless_current+ is evaluated if the current
2008-07-19 14:06:43 -04:00
# action is the action given. So, if we had a comments page and wanted to render a
2007-05-06 00:29:42 -04:00
# "Go Back" link instead of a link to the comments page, we could do something like this...
2008-07-19 14:06:43 -04:00
#
# <%=
2007-05-06 00:29:42 -04:00
# link_to_unless_current("Comment", { :controller => 'comments', :action => 'new}) do
2008-07-19 14:06:43 -04:00
# link_to("Go back", { :controller => 'posts', :action => 'index' })
# end
2007-05-06 00:29:42 -04:00
# %>
2007-05-12 17:12:31 -04:00
def link_to_unless_current ( name , options = { } , html_options = { } , & block )
link_to_unless current_page? ( options ) , name , options , html_options , & block
2005-03-06 09:06:33 -05:00
end
2006-10-22 19:54:41 -04:00
# Creates a link tag of the given +name+ using a URL created by the set of
2007-03-13 01:12:59 -04:00
# +options+ unless +condition+ is true, in which case only the name is
2007-05-06 00:29:42 -04:00
# returned. To specialize the default behavior (i.e., show a login link rather
# than just the plaintext link text), you can pass a block that
2009-07-25 11:03:58 -04:00
# accepts the name or the full argument list for +link_to_unless+.
2006-10-22 19:54:41 -04:00
#
2007-05-06 00:29:42 -04:00
# ==== Examples
2006-10-22 19:54:41 -04:00
# <%= link_to_unless(@current_user.nil?, "Reply", { :action => "reply" }) %>
2007-05-06 00:29:42 -04:00
# # If the user is logged in...
# # => <a href="/controller/reply/">Reply</a>
2006-10-22 19:54:41 -04:00
#
2008-07-19 14:06:43 -04:00
# <%=
2007-05-06 00:29:42 -04:00
# link_to_unless(@current_user.nil?, "Reply", { :action => "reply" }) do |name|
# link_to(name, { :controller => "accounts", :action => "signup" })
2008-07-19 14:06:43 -04:00
# end
2007-05-06 00:29:42 -04:00
# %>
# # If the user is logged in...
# # => <a href="/controller/reply/">Reply</a>
# # If not...
# # => <a href="/accounts/signup">Reply</a>
2007-05-12 17:12:31 -04:00
def link_to_unless ( condition , name , options = { } , html_options = { } , & block )
2005-03-06 09:06:33 -05:00
if condition
if block_given?
2007-05-12 17:12:31 -04:00
block . arity < = 1 ? yield ( name ) : yield ( name , options , html_options )
2005-03-06 09:06:33 -05:00
else
2005-07-09 13:08:23 -04:00
name
2005-03-06 09:06:33 -05:00
end
2004-11-23 20:04:44 -05:00
else
2007-05-12 17:12:31 -04:00
link_to ( name , options , html_options )
2007-03-13 01:12:59 -04:00
end
2005-03-06 09:06:33 -05:00
end
2007-03-13 01:12:59 -04:00
2006-10-22 19:54:41 -04:00
# Creates a link tag of the given +name+ using a URL created by the set of
2007-03-13 01:12:59 -04:00
# +options+ if +condition+ is true, in which case only the name is
2006-10-22 19:54:41 -04:00
# returned. To specialize the default behavior, you can pass a block that
2009-07-25 11:03:58 -04:00
# accepts the name or the full argument list for +link_to_unless+ (see the examples
# in +link_to_unless+).
2007-05-06 00:29:42 -04:00
#
# ==== Examples
# <%= link_to_if(@current_user.nil?, "Login", { :controller => "sessions", :action => "new" }) %>
# # If the user isn't logged in...
# # => <a href="/sessions/new/">Login</a>
#
2008-07-19 14:06:43 -04:00
# <%=
2007-05-06 00:29:42 -04:00
# link_to_if(@current_user.nil?, "Login", { :controller => "sessions", :action => "new" }) do
# link_to(@current_user.login, { :controller => "accounts", :action => "show", :id => @current_user })
2008-07-19 14:06:43 -04:00
# end
2007-05-06 00:29:42 -04:00
# %>
# # If the user isn't logged in...
# # => <a href="/sessions/new/">Login</a>
# # If they are logged in...
# # => <a href="/accounts/show/3">my_username</a>
2007-05-12 17:12:31 -04:00
def link_to_if ( condition , name , options = { } , html_options = { } , & block )
link_to_unless ! condition , name , options , html_options , & block
2004-11-23 20:04:44 -05:00
end
2006-10-22 19:54:41 -04:00
# Creates a mailto link tag to the specified +email_address+, which is
# also used as the name of the link unless +name+ is specified. Additional
2007-05-06 00:29:42 -04:00
# HTML attributes for the link can be passed in +html_options+.
2006-10-22 19:54:41 -04:00
#
2009-07-25 11:03:58 -04:00
# +mail_to+ has several methods for hindering email harvesters and customizing
2006-10-22 19:54:41 -04:00
# the email itself by passing special keys to +html_options+.
#
2007-05-06 00:29:42 -04:00
# ==== Options
2009-07-25 11:03:58 -04:00
# * <tt>:encode</tt> - This key will accept the strings "javascript" or "hex".
# Passing "javascript" will dynamically create and encode the mailto link then
2006-10-22 19:54:41 -04:00
# eval it into the DOM of the page. This method will not show the link on
# the page if the user has JavaScript disabled. Passing "hex" will hex
2009-07-25 11:03:58 -04:00
# encode the +email_address+ before outputting the mailto link.
# * <tt>:replace_at</tt> - When the link +name+ isn't provided, the
2006-10-22 19:54:41 -04:00
# +email_address+ is used for the link label. You can use this option to
# obfuscate the +email_address+ by substituting the @ sign with the string
# given as the value.
2009-07-25 11:03:58 -04:00
# * <tt>:replace_dot</tt> - When the link +name+ isn't provided, the
2006-10-22 19:54:41 -04:00
# +email_address+ is used for the link label. You can use this option to
# obfuscate the +email_address+ by substituting the . in the email with the
# string given as the value.
2009-07-25 11:03:58 -04:00
# * <tt>:subject</tt> - Preset the subject line of the email.
2006-10-22 19:54:41 -04:00
# * <tt>:body</tt> - Preset the body of the email.
2009-07-25 11:03:58 -04:00
# * <tt>:cc</tt> - Carbon Copy addition recipients on the email.
# * <tt>:bcc</tt> - Blind Carbon Copy additional recipients on the email.
2005-01-24 08:48:24 -05:00
#
2007-05-06 00:29:42 -04:00
# ==== Examples
2008-07-19 14:06:43 -04:00
# mail_to "me@domain.com"
2007-05-06 00:29:42 -04:00
# # => <a href="mailto:me@domain.com">me@domain.com</a>
2005-02-23 07:31:05 -05:00
#
2008-07-19 14:06:43 -04:00
# mail_to "me@domain.com", "My email", :encode => "javascript"
2008-07-31 18:44:11 -04:00
# # => <script type="text/javascript">eval(decodeURIComponent('%64%6f%63...%27%29%3b'))</script>
2005-04-02 03:16:57 -05:00
#
2008-07-19 14:06:43 -04:00
# mail_to "me@domain.com", "My email", :encode => "hex"
2007-05-06 00:29:42 -04:00
# # => <a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">My email</a>
#
2008-07-19 14:06:43 -04:00
# mail_to "me@domain.com", nil, :replace_at => "_at_", :replace_dot => "_dot_", :class => "email"
2007-05-06 00:29:42 -04:00
# # => <a href="mailto:me@domain.com" class="email">me_at_domain_dot_com</a>
2006-10-22 19:54:41 -04:00
#
2007-03-13 01:12:59 -04:00
# mail_to "me@domain.com", "My email", :cc => "ccaddress@domain.com",
2008-07-19 14:06:43 -04:00
# :subject => "This is an example email"
2007-05-06 00:29:42 -04:00
# # => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">My email</a>
2004-11-23 20:04:44 -05:00
def mail_to ( email_address , name = nil , html_options = { } )
2005-03-06 06:50:41 -05:00
html_options = html_options . stringify_keys
2007-01-28 09:16:25 -05:00
encode = html_options . delete ( " encode " ) . to_s
2005-04-02 03:16:57 -05:00
cc , bcc , subject , body = html_options . delete ( " cc " ) , html_options . delete ( " bcc " ) , html_options . delete ( " subject " ) , html_options . delete ( " body " )
2005-01-24 08:48:24 -05:00
string = ''
2005-04-02 03:16:57 -05:00
extras = ''
2010-01-05 18:33:17 -05:00
extras << " cc= #{ Rack :: Utils . escape ( cc ) . gsub ( " + " , " %20 " ) } & " unless cc . nil?
extras << " bcc= #{ Rack :: Utils . escape ( bcc ) . gsub ( " + " , " %20 " ) } & " unless bcc . nil?
extras << " body= #{ Rack :: Utils . escape ( body ) . gsub ( " + " , " %20 " ) } & " unless body . nil?
extras << " subject= #{ Rack :: Utils . escape ( subject ) . gsub ( " + " , " %20 " ) } & " unless subject . nil?
2005-04-02 03:16:57 -05:00
extras = " ? " << extras . gsub! ( / &?$ / , " " ) unless extras . empty?
2010-02-13 16:55:58 -05:00
email_address_obfuscated = html_escape ( email_address )
2005-07-02 02:46:41 -04:00
email_address_obfuscated . gsub! ( / @ / , html_options . delete ( " replace_at " ) ) if html_options . has_key? ( " replace_at " )
email_address_obfuscated . gsub! ( / \ . / , html_options . delete ( " replace_dot " ) ) if html_options . has_key? ( " replace_dot " )
2006-10-22 19:54:41 -04:00
if encode == " javascript "
2010-02-13 16:55:58 -05:00
" document.write(' #{ content_tag ( " a " , name || email_address_obfuscated . html_safe , html_options . merge ( { " href " = > " mailto: " + email_address + extras } ) ) } '); " . each_byte do | c |
2007-12-21 06:51:01 -05:00
string << sprintf ( " %%%x " , c )
2005-01-24 08:48:24 -05:00
end
2010-04-16 18:24:57 -04:00
" <script type= \" #{ Mime :: JS } \" >eval(decodeURIComponent(' #{ string } '))</script> " . html_safe
2006-10-22 19:54:41 -04:00
elsif encode == " hex "
2007-01-28 09:16:25 -05:00
email_address_encoded = ''
email_address_obfuscated . each_byte do | c |
email_address_encoded << sprintf ( " & # %d; " , c )
end
protocol = 'mailto:'
protocol . each_byte { | c | string << sprintf ( " & # %d; " , c ) }
2007-12-21 06:51:01 -05:00
email_address . each_byte do | c |
char = c . chr
string << ( char =~ / \ w / ? sprintf ( " %%%x " , c ) : char )
2005-01-24 08:48:24 -05:00
end
2010-02-13 16:53:26 -05:00
content_tag " a " , name || email_address_encoded . html_safe , html_options . merge ( { " href " = > " #{ string } #{ extras } " } )
2005-01-24 08:48:24 -05:00
else
2010-02-13 16:55:58 -05:00
content_tag " a " , name || email_address_obfuscated . html_safe , html_options . merge ( { " href " = > " mailto: #{ email_address } #{ extras } " } )
2005-01-24 08:48:24 -05:00
end
2004-11-23 20:04:44 -05:00
end
2007-05-06 00:29:42 -04:00
# True if the current request URI was generated by the given +options+.
#
# ==== Examples
2008-10-26 12:46:17 -04:00
# Let's say we're in the <tt>/shop/checkout?order=desc</tt> action.
2007-05-06 00:29:42 -04:00
#
# current_page?(:action => 'process')
# # => false
#
# current_page?(:controller => 'shop', :action => 'checkout')
# # => true
#
2009-02-04 00:55:08 -05:00
# current_page?(:controller => 'shop', :action => 'checkout', :order => 'asc')
# # => false
#
# current_page?(:action => 'checkout')
# # => true
#
# current_page?(:controller => 'library', :action => 'checkout')
# # => false
#
# Let's say we're in the <tt>/shop/checkout?order=desc&page=1</tt> action.
#
# current_page?(:action => 'process')
# # => false
#
# current_page?(:controller => 'shop', :action => 'checkout')
# # => true
#
# current_page?(:controller => 'shop', :action => 'checkout', :order => 'desc', :page=>'1')
# # => true
#
# current_page?(:controller => 'shop', :action => 'checkout', :order => 'desc', :page=>'2')
# # => false
#
# current_page?(:controller => 'shop', :action => 'checkout', :order => 'desc')
2008-10-26 12:46:17 -04:00
# # => false
#
2007-05-06 00:29:42 -04:00
# current_page?(:action => 'checkout')
# # => true
#
# current_page?(:controller => 'library', :action => 'checkout')
# # => false
2005-02-17 10:34:32 -05:00
def current_page? ( options )
2010-04-03 05:30:06 -04:00
unless request
raise " You cannot use helpers that need to determine the current " \
" page unless your view context provides a Request object " \
" in a # request method "
end
2010-06-29 13:55:57 -04:00
url_string = url_for ( options )
2010-04-03 05:30:06 -04:00
2010-01-22 11:18:19 -05:00
# We ignore any extra parameters in the request_uri if the
2008-10-26 12:46:17 -04:00
# submitted url doesn't have any either. This lets the function
2010-01-22 11:18:19 -05:00
# work with things like ?order=asc
2008-10-26 12:46:17 -04:00
if url_string . index ( " ? " )
2010-03-02 21:57:02 -05:00
request_uri = request . fullpath
2008-10-26 12:46:17 -04:00
else
2010-03-02 21:57:02 -05:00
request_uri = request . path
2008-10-26 12:46:17 -04:00
end
2010-03-02 21:57:02 -05:00
2007-01-12 02:02:38 -05:00
if url_string =~ / ^ \ w+: \/ \/ /
2008-10-26 12:46:17 -04:00
url_string == " #{ request . protocol } #{ request . host_with_port } #{ request_uri } "
2007-01-12 02:02:38 -05:00
else
2008-10-26 12:46:17 -04:00
url_string == request_uri
2007-01-12 02:02:38 -05:00
end
2005-02-17 10:34:32 -05:00
end
2004-11-23 20:04:44 -05:00
private
2010-01-30 20:44:35 -05:00
def convert_options_to_data_attributes ( options , html_options )
html_options = { } if html_options . nil?
html_options = html_options . stringify_keys
2010-04-14 20:44:28 -04:00
if ( options . is_a? ( Hash ) && options . key? ( 'remote' ) && options . delete ( 'remote' ) ) || ( html_options . is_a? ( Hash ) && html_options . key? ( 'remote' ) && html_options . delete ( 'remote' ) )
2010-01-30 20:44:35 -05:00
html_options [ 'data-remote' ] = 'true'
end
2010-01-30 15:39:41 -05:00
confirm = html_options . delete ( " confirm " )
if html_options . key? ( " popup " )
ActiveSupport :: Deprecation . warn ( " :popup has been deprecated " , caller )
end
2010-01-30 20:44:35 -05:00
method , href = html_options . delete ( " method " ) , html_options [ 'href' ]
2010-01-31 17:30:21 -05:00
add_confirm_to_attributes! ( html_options , confirm ) if confirm
add_method_to_attributes! ( html_options , method ) if method
2010-01-30 20:44:35 -05:00
html_options
2010-01-30 15:39:41 -05:00
end
2010-01-30 20:44:35 -05:00
def add_confirm_to_attributes! ( html_options , confirm )
html_options [ " data-confirm " ] = confirm if confirm
2010-01-30 15:39:41 -05:00
end
2010-01-30 20:44:35 -05:00
def add_method_to_attributes! ( html_options , method )
2010-01-30 21:26:30 -05:00
html_options [ " rel " ] = " nofollow " if method && method . to_s . downcase != " get "
2010-01-30 20:44:35 -05:00
html_options [ " data-method " ] = method if method
end
2010-01-30 15:39:41 -05:00
2010-01-30 20:44:35 -05:00
def options_for_javascript ( options )
if options . empty?
'{}'
else
" { #{ options . keys . map { | k | " #{ k } : #{ options [ k ] } " } . sort . join ( ', ' ) } } "
2010-01-30 15:39:41 -05:00
end
2010-01-30 20:44:35 -05:00
end
2010-01-30 15:39:41 -05:00
2010-01-30 20:44:35 -05:00
def array_or_string_for_javascript ( option )
if option . kind_of? ( Array )
" [' #{ option . join ( '\',\'' ) } '] "
elsif ! option . nil?
" ' #{ option } ' "
2010-01-30 15:39:41 -05:00
end
end
2009-07-25 11:03:58 -04:00
# Processes the +html_options+ hash, converting the boolean
2005-06-16 02:17:51 -04:00
# attributes from true/false form into the form required by
# HTML/XHTML. (An attribute is considered to be boolean if
2009-07-25 11:03:58 -04:00
# its name is listed in the given +bool_attrs+ array.)
2005-06-16 02:17:51 -04:00
#
2009-07-25 11:03:58 -04:00
# More specifically, for each boolean attribute in +html_options+
2005-06-16 02:17:51 -04:00
# given as:
#
2009-07-25 11:03:58 -04:00
# "attr" => bool_value
2005-06-16 02:17:51 -04:00
#
2009-07-25 11:03:58 -04:00
# if the associated +bool_value+ evaluates to true, it is
2005-06-16 02:17:51 -04:00
# replaced with the attribute's name; otherwise the attribute is
2009-07-25 11:03:58 -04:00
# removed from the +html_options+ hash. (See the XHTML 1.0 spec,
2005-06-16 02:17:51 -04:00
# section 4.5 "Attribute Minimization" for more:
# http://www.w3.org/TR/xhtml1/#h-4.5)
#
2009-07-25 11:03:58 -04:00
# Returns the updated +html_options+ hash, which is also modified
2005-06-16 02:17:51 -04:00
# in place.
#
# Example:
#
# convert_boolean_attributes!( html_options,
# %w( checked disabled readonly ) )
def convert_boolean_attributes! ( html_options , bool_attrs )
bool_attrs . each { | x | html_options [ x ] = x if html_options . delete ( x ) }
html_options
end
2004-11-23 20:04:44 -05:00
end
end
2005-06-16 02:17:51 -04:00
end