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-10-18 11:58:57 -04:00
require 'active_support/core_ext/string/output_safety'
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
2010-07-01 18:29:20 -04:00
# URL helpers of a routes (routes.url_helpers). Some methods
2010-09-01 03:00:55 -04:00
# provided here will only work in the 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
2012-06-18 14:52:15 -04:00
# We need to override url_options, _routes_context
2012-03-02 09:01:20 -05:00
# and optimize_routes_generation? to consider the controller.
2010-08-04 10:20:56 -04:00
2012-03-02 09:01:20 -05:00
def url_options #:nodoc:
2010-04-03 05:30:06 -04:00
return super unless controller . respond_to? ( :url_options )
2010-08-04 10:20:56 -04:00
controller . url_options
2010-03-16 17:55:42 -04:00
end
2012-03-02 09:01:20 -05:00
def _routes_context #:nodoc:
controller
end
protected :_routes_context
def optimize_routes_generation? #:nodoc:
controller . respond_to? ( :optimize_routes_generation? ) ?
controller . optimize_routes_generation? : super
end
protected :optimize_routes_generation?
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
#
2011-05-15 15:34:36 -04:00
# Passing a record (like an Active Record) instead of a Hash as the options parameter will
2011-07-24 06:21:42 -04:00
# trigger the named route for that record. The lookup will happen on the name of the class. So passing a
2011-06-22 11:00:56 -04:00
# Workshop object will attempt to use the +workshop_path+ route. If you have a nested route, such as
2009-07-25 11:03:58 -04:00
# +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') %>
2011-04-21 10:32:02 -04:00
# # => https://www.example.com/members/login/
2007-05-06 00:29:42 -04:00
#
# <%= 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) %>
2011-04-05 07:22:10 -04:00
# # calls @workshop.to_param which by default returns the id
2007-05-12 17:12:31 -04:00
# # => /workshops/5
2008-07-19 14:06:43 -04:00
#
2011-04-05 07:22:10 -04:00
# # to_param can be re-defined in a model to provide different URL names:
# # => /workshops/1-workshop-name
#
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()
2012-08-09 11:10:39 -04:00
# ==== Implicit Controller Namespace
#
# Controllers passed in will retain their namespace unless an absolute controller is specified with a slash
#
# # When called inside of the "admin" namespace
# <%= url_for(:action => 'index', :controller => 'users') %>
# # => /admin/users
#
# # Specify absolute path with beginning slash
# <%= url_for(:action => 'index', :controller => '/users') %>
# # => /users
2012-01-12 17:41:03 -05:00
def url_for ( options = nil )
2010-09-26 08:04:51 -04:00
case options
2008-07-19 14:06:43 -04:00
when String
options
2012-01-12 17:41:03 -05:00
when nil , Hash
options || = { }
2012-05-14 16:51:54 -04:00
options = { :only_path = > options [ :host ] . nil? } . merge! ( 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
2004-11-23 20:04:44 -05:00
end
2011-06-22 11:00:56 -04:00
# Creates a link tag of the given +name+ using a URL created by the set of +options+.
2011-07-24 06:21:42 -04:00
# See the valid options in the documentation for +url_for+. It's also possible to
2011-06-22 11:00:56 -04:00
# pass a String instead of an options hash, which generates a link tag that uses the
# value of the String as the href for the link. Using a <tt>:back</tt> Symbol instead
# of an options hash will generate a link to the referrer (a JavaScript back link
# will be used in place of a referrer if none exists). If +nil+ is passed as the name
# the value of 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 = {})
2012-06-01 15:54:21 -04:00
# # url_options, except :method, is passed to url_for
2010-04-03 05:30:06 -04:00
#
# 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
2012-06-01 15:54:21 -04:00
# * <tt>:data</tt> - This option can be used to add custom data attributes.
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
2011-05-06 17:03:55 -04:00
# while spidering your site). Supported verbs are <tt>:post</tt>, <tt>:delete</tt>, <tt>:patch</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
2011-05-06 17:03:55 -04:00
# the request object's methods for <tt>post?</tt>, <tt>delete?</tt>, <tt>:patch</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
#
2012-07-21 14:20:26 -04:00
# ==== Data attributes
#
# * <tt>:confirm => 'question?'</tt> - This will allow the unobtrusive JavaScript
# driver to prompt with the question specified. If the user accepts, the link is
# processed normally, otherwise no action is taken.
2012-07-21 15:19:40 -04:00
# * <tt>:disable_with</tt> - Value of this parameter will be
# used as the value for a disabled version of the submit
# button when the form is submitted. This feature is provided
# by the unobtrusive JavaScript driver.
2012-07-21 14:20:26 -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
2011-05-23 19:22:33 -04:00
# and newer RESTful routes. Current Rails style favors RESTful routes whenever possible, so base
2008-05-16 18:01:32 -04:00
# 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>
#
2011-04-02 23:47:51 -04:00
# You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
2008-06-17 15:01:37 -04:00
#
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>
#
2012-06-01 15:54:21 -04:00
# The only option specific to +link_to+ (<tt>:method</tt>) is used as follows:
2008-05-16 18:01:32 -04:00
#
2012-06-01 15:54:21 -04:00
# link_to("Destroy", "http://www.example.com", :method => :delete)
# # => <a href='http://www.example.com' rel="nofollow" data-method="delete">Destroy</a>
#
# You can also use custom data attributes using the <tt>:data</tt> option:
2010-01-31 17:30:21 -05:00
#
2012-06-01 15:54:21 -04:00
# link_to "Visit Other Site", "http://www.rubyonrails.org/", :data => { :confirm => "Are you sure?" }
# # => <a href="http://www.rubyonrails.org/" data-confirm="Are you sure?"">Visit Other Site</a>
2012-05-30 21:53:14 -04:00
def link_to ( name = nil , options = nil , html_options = nil , & block )
2012-05-31 08:18:14 -04:00
html_options , options = options , name if block_given?
options || = { }
url = url_for ( options )
2008-06-17 15:01:37 -04:00
2012-05-31 08:18:14 -04:00
html_options = convert_options_to_data_attributes ( options , html_options )
html_options [ 'href' ] || = url
2008-07-19 14:06:43 -04:00
2012-05-31 08:18:14 -04:00
content_tag ( :a , name || url , html_options , & block )
2004-11-23 20:04:44 -05:00
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-11-19 13:25:21 -05:00
# By default, the generated form element has a class name of <tt>button_to</tt>
# to allow styling of the form itself and its children. This can be changed
# using the <tt>:form_class</tt> modifier within +html_options+. You can control
2006-10-22 19:54:41 -04:00
# the form submission and input element behavior using +html_options+.
2012-06-01 15:54:21 -04:00
# This method accepts the <tt>:method</tt> modifier described in the +link_to+ documentation.
# If no <tt>:method</tt> modifier is given, it will default to performing a POST operation.
# You can also disable the button by passing <tt>:disabled => true</tt> in +html_options+.
2006-10-22 19:54:41 -04:00
# 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
2011-08-20 18:28:49 -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+:
2010-09-05 22:45:51 -04:00
# * <tt>:method</tt> - Symbol of HTTP verb. Supported verbs are <tt>:post</tt>, <tt>:get</tt>,
2011-05-06 17:03:55 -04:00
# <tt>:delete</tt>, <tt>:patch</tt>, and <tt>:put</tt>. By default it will be <tt>:post</tt>.
2010-08-31 03:19:43 -04:00
# * <tt>:disabled</tt> - If set to true, it will generate a disabled button.
2012-06-01 15:54:21 -04:00
# * <tt>:data</tt> - This option can be used to add custom data attributes.
2010-04-03 05:30:06 -04:00
# * <tt>:remote</tt> - If set to true, will allow the Unobtrusive JavaScript drivers to control the
2011-07-24 06:40:40 -04:00
# submit behavior. By default this behavior is an ajax submit.
2011-09-28 13:40:15 -04:00
# * <tt>:form</tt> - This hash will be form attributes
2010-11-19 13:25:21 -05:00
# * <tt>:form_class</tt> - This controls the class of the form within which the submit button will
# be placed
2008-07-19 14:06:43 -04:00
#
2012-07-21 14:20:26 -04:00
# ==== Data attributes
#
# * <tt>:confirm</tt> - This will use the unobtrusive JavaScript driver to
# prompt with the question specified. If the user accepts, the link is
# processed normally, otherwise no action is taken.
2012-07-21 15:19:40 -04:00
# * <tt>:disable_with</tt> - Value of this parameter will be
# used as the value for a disabled version of the submit
# button when the form is submitted. This feature is provided
# by the unobtrusive JavaScript driver.
2012-07-21 14:20:26 -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
#
2012-05-30 16:15:15 -04:00
# <%= button_to [:make_happy, @user] do %>
# Make happy <strong><%= @user.name %></strong>
# <% end %>
# # => "<form method="post" action="/users/1/make_happy" class="button_to">
# # <div>
# # <button type="submit">
# # Make happy <strong><%= @user.name %></strong>
# # </button>
# # </div>
# # </form>"
2010-01-31 17:30:21 -05:00
#
2010-11-19 13:25:21 -05:00
# <%= button_to "New", :action => "new", :form_class => "new-thing" %>
# # => "<form method="post" action="/controller/new" class="new-thing">
# # <div><input value="New" type="submit" /></div>
# # </form>"
#
#
2011-09-28 13:40:15 -04:00
# <%= button_to "Create", :action => "create", :remote => true, :form => { "data-type" => "json" } %>
# # => "<form method="post" action="/images/create" class="button_to" data-remote="true" data-type="json">
2012-05-04 09:36:42 -04:00
# # <div>
# # <input value="Create" type="submit" />
# # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/>
# # </div>
2011-09-28 13:40:15 -04:00
# # </form>"
#
2012-01-12 17:41:03 -05:00
#
2010-01-31 17:30:21 -05:00
# <%= button_to "Delete Image", { :action => "delete", :id => @image.id },
2012-06-01 15:54:21 -04:00
# :method => :delete, :data => { :confirm => "Are you sure?" } %>
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" />
2012-05-04 14:28:15 -04:00
# # <input data-confirm='Are you sure?' value="Delete Image" type="submit" />
2012-05-04 09:36:42 -04:00
# # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/>
2007-05-06 00:29:42 -04:00
# # </div>
# # </form>"
2010-01-31 17:30:21 -05:00
#
#
2012-06-01 15:54:21 -04:00
# <%= button_to('Destroy', 'http://www.example.com',
2012-07-21 15:19:40 -04:00
# :method => "delete", :remote => true, :data => { :confirm' => 'Are you sure?', :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' />
2012-07-21 15:19:40 -04:00
# # <input value='Destroy' type='submit' data-disable-with='loading...' data-confirm='Are you sure?' />
2012-05-04 09:36:42 -04:00
# # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/>
2010-01-31 17:30:21 -05:00
# # </div>
# # </form>"
# #
2012-05-30 21:44:30 -04:00
def button_to ( name = nil , options = nil , html_options = nil , & block )
html_options , options = options , name if block_given?
options || = { }
html_options || = { }
2012-05-30 16:15:15 -04:00
2006-10-22 19:54:41 -04:00
html_options = html_options . stringify_keys
2012-01-21 16:44:03 -05:00
convert_boolean_attributes! ( html_options , %w( disabled ) )
2006-09-03 11:59:18 -04:00
2012-01-21 16:44:03 -05:00
url = options . is_a? ( String ) ? options : url_for ( options )
remote = html_options . delete ( 'remote' )
2006-09-03 11:59:18 -04:00
2012-01-21 16:44:03 -05:00
method = html_options . delete ( 'method' ) . to_s
2012-02-26 11:39:33 -05:00
method_tag = %w{ patch put delete } . include? ( method ) ? method_tag ( method ) : '' . html_safe
2012-01-21 16:44:03 -05:00
form_method = method == 'get' ? 'get' : 'post'
2011-09-28 13:40:15 -04:00
form_options = html_options . delete ( 'form' ) || { }
form_options [ :class ] || = html_options . delete ( 'form_class' ) || 'button_to'
2012-01-21 16:44:03 -05:00
form_options . merge! ( :method = > form_method , :action = > url )
form_options . merge! ( " data-remote " = > " true " ) if remote
2012-01-12 17:41:03 -05:00
2012-01-18 17:30:04 -05:00
request_token_tag = form_method == 'post' ? token_tag : ''
2008-07-19 14:06:43 -04:00
2010-01-30 20:44:35 -05:00
html_options = convert_options_to_data_attributes ( options , html_options )
2012-05-30 16:15:15 -04:00
html_options [ 'type' ] = 'submit'
button = if block_given?
content_tag ( 'button' , html_options , & block )
else
2012-05-30 21:44:30 -04:00
html_options [ 'value' ] = name || url
tag ( 'input' , html_options )
2012-05-30 16:15:15 -04:00
end
2012-01-12 17:41:03 -05:00
2012-05-30 16:15:15 -04:00
inner_tags = method_tag . safe_concat ( button ) . safe_concat ( request_token_tag )
2012-02-26 11:39:33 -05:00
content_tag ( 'form' , content_tag ( 'div' , inner_tags ) , form_options )
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
2011-05-23 19:22:33 -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
2011-05-23 19:22:33 -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
#
# <%=
2010-08-25 09:55:53 -04:00
# link_to_unless_current("Comment", { :controller => "comments", :action => "new" }) do
# link_to("Go back", { :controller => "posts", :action => "index" })
2008-07-19 14:06:43 -04:00
# 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?
2010-07-21 09:23:07 -04:00
block . arity < = 1 ? capture ( name , & block ) : capture ( name , options , html_options , & block )
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
2011-05-08 13:26:12 -04:00
# +options+ if +condition+ is true, otherwise 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.
2012-01-19 02:33:29 -05:00
# * <tt>:cc</tt> - Carbon Copy additional recipients on the email.
2009-07-25 11:03:58 -04:00
# * <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"
2012-04-05 07:32:37 -04:00
# # => <script>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 = { } )
2010-10-18 11:58:57 -04:00
email_address = ERB :: Util . html_escape ( email_address )
2010-06-29 18:12:48 -04:00
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
2010-10-04 22:46:38 -04:00
extras = %w{ cc bcc body subject } . map { | item |
option = html_options . delete ( item ) || next
2012-01-13 05:49:16 -05:00
" #{ item } = #{ Rack :: Utils . escape_path ( option ) } "
2010-10-04 22:46:38 -04:00
} . compact
2010-10-18 11:58:57 -04:00
extras = extras . empty? ? '' : '?' + ERB :: Util . html_escape ( extras . join ( '&' ) )
2010-06-29 18:12:48 -04:00
2011-06-08 01:51:48 -04:00
email_address_obfuscated = email_address . to_str
2010-10-04 22:46:38 -04:00
email_address_obfuscated . gsub! ( / @ / , html_options . delete ( " replace_at " ) ) if html_options . key? ( " replace_at " )
email_address_obfuscated . gsub! ( / \ . / , html_options . delete ( " replace_dot " ) ) if html_options . key? ( " replace_dot " )
2010-10-04 23:05:56 -04:00
case encode
when " javascript "
2010-12-06 22:27:55 -05:00
string = ''
html = content_tag ( " a " , name || email_address_obfuscated . html_safe , html_options . merge ( " href " = > " mailto: #{ email_address } #{ extras } " . html_safe ) )
2011-06-08 01:51:48 -04:00
html = escape_javascript ( html . to_str )
2010-12-06 22:27:55 -05:00
" document.write(' #{ html } '); " . each_byte do | c |
string << sprintf ( " %%%x " , c )
end
2012-04-05 07:32:37 -04:00
" <script>eval(decodeURIComponent(' #{ string } '))</script> " . html_safe
2010-10-04 23:05:56 -04:00
when " hex "
2010-10-04 22:52:17 -04:00
email_address_encoded = email_address_obfuscated . unpack ( 'C*' ) . map { | c |
sprintf ( " & # %d; " , c )
} . join
2007-01-28 09:16:25 -05:00
2010-10-04 23:05:56 -04:00
string = 'mailto:' . unpack ( 'C*' ) . map { | c |
sprintf ( " & # %d; " , c )
} . join + email_address . unpack ( 'C*' ) . map { | c |
2007-12-21 06:51:01 -05:00
char = c . chr
2010-10-04 23:00:55 -04:00
char =~ / \ w / ? sprintf ( " %%%x " , c ) : char
2010-10-04 23:05:56 -04:00
} . join
2010-06-29 18:12:48 -04:00
content_tag " a " , name || email_address_encoded . html_safe , html_options . merge ( " href " = > " #{ string } #{ extras } " . html_safe )
2005-01-24 08:48:24 -05:00
else
2010-06-29 18:12:48 -04:00
content_tag " a " , name || email_address_obfuscated . html_safe , html_options . merge ( " href " = > " mailto: #{ email_address } #{ extras } " . html_safe )
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
#
2011-04-15 21:34:49 -04:00
# current_page?(:controller => 'shop', :action => 'checkout', :order => 'desc', :page => '1')
2009-02-04 00:55:08 -05:00
# # => true
#
2011-04-15 21:34:49 -04:00
# current_page?(:controller => 'shop', :action => 'checkout', :order => 'desc', :page => '2')
2009-02-04 00:55:08 -05:00
# # => 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
2011-09-02 10:20:10 -04:00
#
# Let's say we're in the <tt>/products</tt> action with method POST in case of invalid product.
#
# current_page?(:controller => 'product', :action => 'index')
# # => 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
2011-09-02 10:20:10 -04:00
return false unless request . get?
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
2011-05-23 19:22:33 -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
2012-01-21 16:44:03 -05:00
request_uri = url_string . index ( " ? " ) ? request . fullpath : request . path
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 )
2011-09-04 11:44:08 -04:00
if html_options
2010-09-27 11:43:27 -04:00
html_options = html_options . stringify_keys
html_options [ 'data-remote' ] = 'true' if link_to_remote_options? ( options ) || link_to_remote_options? ( html_options )
2010-01-30 20:44:35 -05:00
2012-07-21 15:19:40 -04:00
disable_with = html_options . delete ( " disable_with " )
2012-07-21 14:20:26 -04:00
confirm = html_options . delete ( 'confirm' )
2010-09-27 11:43:27 -04:00
method = html_options . delete ( 'method' )
2010-01-30 20:44:35 -05:00
2012-07-21 14:20:26 -04:00
if confirm
ActiveSupport :: Deprecation . warn " :confirm option is deprecated and will be removed from Rails 4.1. Use ':data => { :confirm => \' Text \' }' instead "
html_options [ " data-confirm " ] = confirm
end
2012-01-12 17:41:03 -05:00
add_method_to_attributes! ( html_options , method ) if method
2010-01-30 20:44:35 -05:00
2012-07-21 15:19:40 -04:00
if disable_with
ActiveSupport :: Deprecation . warn " :disable_with option is deprecated and will be removed from Rails 4.1. Use ':data => { :disable_with => \' Text \' }' instead "
html_options [ " data-disable-with " ] = disable_with
end
2010-09-27 11:43:27 -04:00
html_options
2011-09-04 11:44:08 -04:00
else
link_to_remote_options? ( options ) ? { 'data-remote' = > 'true' } : { }
2010-09-27 11:43:27 -04:00
end
end
2010-01-30 20:44:35 -05:00
2010-09-27 11:43:27 -04:00
def link_to_remote_options? ( options )
2012-01-21 16:44:03 -05:00
options . is_a? ( Hash ) && options . delete ( 'remote' )
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 )
2011-06-22 23:35:54 -04:00
if method && method . to_s . downcase != " get " && html_options [ " rel " ] !~ / nofollow /
2012-01-21 16:44:03 -05:00
html_options [ " rel " ] = " #{ html_options [ " rel " ] } nofollow " . lstrip
2011-06-21 01:17:53 -04:00
end
2010-10-11 13:55:06 -04:00
html_options [ " data-method " ] = method
2010-01-30 20:44:35 -05:00
end
2010-01-30 15:39:41 -05:00
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
2011-05-23 19:22:33 -04:00
# 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
2011-05-23 19:22:33 -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
2012-01-18 17:30:04 -05:00
def token_tag ( token = nil )
2012-05-05 08:12:08 -04:00
if token != false && protect_against_forgery?
2012-01-18 17:30:04 -05:00
token || = form_authenticity_token
tag ( :input , :type = > " hidden " , :name = > request_forgery_protection_token . to_s , :value = > token )
2012-05-05 08:12:08 -04:00
else
''
2012-01-18 17:30:04 -05:00
end
end
2012-01-18 18:53:32 -05:00
def method_tag ( method )
tag ( 'input' , :type = > 'hidden' , :name = > '_method' , :value = > method . to_s )
end
2004-11-23 20:04:44 -05:00
end
end
2005-06-16 02:17:51 -04:00
end