2007-01-28 02:16:55 -05:00
require 'action_view/helpers/javascript_helper'
2009-06-08 22:35:30 -04:00
require 'active_support/core_ext/hash/keys'
2005-08-14 04:43:07 -04:00
2004-11-23 20:04:44 -05:00
module ActionView
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
2008-07-19 14:06:43 -04:00
# depend on the routing subsystem (see ActionController::Routing).
# 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
2005-08-14 04:43:07 -04:00
include JavaScriptHelper
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
#
2009-07-25 11:03:58 -04:00
# When called from a view, +url_for+ returns an HTML escaped url. If you
2008-05-02 09:45:23 -04:00
# need an unescaped url, pass <tt>:escape => false</tt> in the +options+.
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).
# * <tt>:escape</tt> - Determines whether the returned URL will be HTML escaped or not (<tt>true</tt> by default).
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
#
# <%= url_for(:action => 'checkout', :anchor => 'tax&ship') %>
# # => /testing/jump/#tax&ship
#
# <%= url_for(:action => 'checkout', :anchor => 'tax&ship', :escape => false) %>
# # => /testing/jump/#tax&ship
2007-05-12 17:12:31 -04:00
#
# <%= url_for(Workshop.new) %>
# # relies on Workshop answering a new_record? call (and in this case returning true)
# # => /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
escape = true
options
2007-05-12 17:12:31 -04:00
when Hash
2008-06-23 08:51:38 -04:00
options = { :only_path = > options [ :host ] . nil? } . update ( options . symbolize_keys )
2007-05-12 17:12:31 -04:00
escape = options . key? ( :escape ) ? options . delete ( :escape ) : true
2008-07-19 14:06:43 -04:00
@controller . send ( :url_for , options )
when :back
escape = false
@controller . request . env [ " HTTP_REFERER " ] || 'javascript:history.back()'
2007-05-12 17:12:31 -04:00
else
escape = false
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
2007-06-05 00:29:19 -04:00
escape ? escape_once ( url ) : 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
#
# link_to(name, options = {}, html_options = nil)
# link_to(options = {}, html_options = nil) do
# # name
# end
#
2007-05-06 00:29:42 -04:00
# ==== Options
2008-05-09 05:38:02 -04:00
# * <tt>:confirm => 'question?'</tt> - This will add a JavaScript confirm
2007-03-13 01:12:59 -04:00
# 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>:popup => true || array of window options</tt> - This will force the
2007-03-13 01:12:59 -04:00
# link to open in a popup window. By passing true, a default browser window
# will be opened with the URL. You can also specify an array of options
2009-07-25 11:03:58 -04:00
# that are passed to the <tt>window.open</tt> JavaScript call.
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>.
2007-05-06 00:29:42 -04:00
# * The +html_options+ will accept a hash of html attributes for the link tag.
2005-09-03 20:33:45 -04:00
#
2006-10-22 19:54:41 -04:00
# You can mix and match the +html_options+ with the exception of
2009-07-25 11:03:58 -04:00
# <tt>:popup</tt> and <tt>:method</tt> which will raise an
# <tt>ActionView::ActionViewError</tt> exception.
2005-09-07 08:56:33 -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:
#
# <% 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>
#
2008-07-16 08:00:36 -04:00
# The three options specific to +link_to+ (<tt>:confirm</tt>, <tt>:popup</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?"
2007-05-06 00:29:42 -04:00
# # => <a href="http://www.rubyonrails.org/" onclick="return confirm('Are you sure?');">Visit Other Site</a>
#
2005-09-03 20:33:45 -04:00
# link_to "Help", { :action => "help" }, :popup => true
2007-05-06 00:29:42 -04:00
# # => <a href="/testing/help/" onclick="window.open(this.href);return false;">Help</a>
#
2008-05-16 18:01:32 -04:00
# link_to "View Image", @image, :popup => ['new_window_name', 'height=300,width=600']
# # => <a href="/images/9" onclick="window.open(this.href,'new_window_name','height=300,width=600');return false;">View Image</a>
2007-05-06 00:29:42 -04:00
#
2008-05-16 18:01:32 -04:00
# link_to "Delete Image", @image, :confirm => "Are you sure?", :method => :delete
2008-07-19 14:06:43 -04:00
# # => <a href="/images/9" onclick="if (confirm('Are you sure?')) { var f = document.createElement('form');
2007-05-06 00:29:42 -04:00
# f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;
2008-07-19 14:06:43 -04:00
# var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');
2009-07-25 11:03:58 -04:00
# m.setAttribute('value', 'delete');var s = document.createElement('input'); s.setAttribute('type', 'hidden');
# s.setAttribute('name', 'authenticity_token'); s.setAttribute('value', 'Q/ttlxPYZ6R77B+vZ1sBkhj21G2isO9dpE6UtOHBApg=');
# f.appendChild(s)f.appendChild(m);f.submit(); };return false;">Delete Image</a>
2008-06-17 15:01:37 -04:00
def link_to ( * args , & block )
if block_given?
options = args . first || { }
html_options = args . second
concat ( link_to ( capture ( & block ) , options , html_options ) )
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
2008-07-19 14:06:43 -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' ]
convert_options_to_javascript! ( html_options , url )
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
2008-06-17 15:01:37 -04:00
href_attr = " href= \" #{ url } \" " unless href
" <a #{ href_attr } #{ tag_options } > #{ name || url } </a> "
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
#
2009-07-25 11:03:58 -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.
# * <tt>:confirm</tt> - This will add a JavaScript confirm
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.
2008-07-19 14:06:43 -04:00
#
2007-05-06 00:29:42 -04:00
# ==== Examples
# <%= button_to "New", :action => "new" %>
# # => "<form method="post" action="/controller/new" class="button-to">
# # <div><input value="New" type="submit" /></div>
# # </form>"
2005-06-16 02:17:51 -04:00
#
2007-05-06 00:29:42 -04:00
# button_to "Delete Image", { :action => "delete", :id => @image.id },
# :confirm => "Are you sure?", :method => :delete
# # => "<form method="post" action="/images/delete/1" class="button-to">
# # <div>
# # <input type="hidden" name="_method" value="delete" />
# # <input onclick="return confirm('Are you sure?');"
# # value="Delete" type="submit" />
# # </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
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
2005-09-07 08:56:33 -04:00
if confirm = html_options . delete ( " confirm " )
html_options [ " onclick " ] = " return #{ confirm_javascript_function ( confirm ) } ; "
end
2006-09-03 11:59:18 -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
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
2007-03-13 01:12:59 -04:00
" <form method= \" #{ form_method } \" action= \" #{ escape_once url } \" class= \" button-to \" ><div> " +
2007-09-25 12:50:35 -04:00
method_tag + tag ( " input " , html_options ) + request_token_tag + " </div></form> "
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 = ''
extras << " cc= #{ CGI . escape ( cc ) . gsub ( " + " , " %20 " ) } & " unless cc . nil?
extras << " bcc= #{ CGI . escape ( bcc ) . gsub ( " + " , " %20 " ) } & " unless bcc . nil?
extras << " body= #{ CGI . escape ( body ) . gsub ( " + " , " %20 " ) } & " unless body . nil?
extras << " subject= #{ CGI . escape ( subject ) . gsub ( " + " , " %20 " ) } & " unless subject . nil?
extras = " ? " << extras . gsub! ( / &?$ / , " " ) unless extras . empty?
2006-10-22 19:54:41 -04:00
email_address = email_address . to_s
2005-07-02 02:46:41 -04:00
email_address_obfuscated = email_address . dup
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 "
2008-07-11 19:57:38 -04:00
" document.write(' #{ content_tag ( " a " , name || email_address_obfuscated , 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
2008-07-31 18:44:11 -04:00
" <script type= \" #{ Mime :: JS } \" >eval(decodeURIComponent(' #{ string } '))</script> "
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
2007-01-28 09:16:25 -05:00
content_tag " a " , name || email_address_encoded , html_options . merge ( { " href " = > " #{ string } #{ extras } " } )
2005-01-24 08:48:24 -05:00
else
2005-07-02 02:46:41 -04:00
content_tag " a " , name || email_address_obfuscated , 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 )
2009-02-04 00:55:08 -05:00
url_string = CGI . unescapeHTML ( url_for ( options ) )
2007-01-12 02:02:38 -05:00
request = @controller . request
2008-10-26 12:46:17 -04:00
# We ignore any extra parameters in the request_uri if the
# submitted url doesn't have any either. This lets the function
# work with things like ?order=asc
if url_string . index ( " ? " )
request_uri = request . request_uri
else
request_uri = request . request_uri . split ( '?' ) . first
end
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
2007-06-23 13:11:01 -04:00
def convert_options_to_javascript! ( html_options , url = '' )
2006-05-27 20:33:53 -04:00
confirm , popup = html_options . delete ( " confirm " ) , html_options . delete ( " popup " )
2007-06-23 13:11:01 -04:00
method , href = html_options . delete ( " method " ) , html_options [ 'href' ]
2007-03-13 01:12:59 -04:00
2005-09-07 08:56:33 -04:00
html_options [ " onclick " ] = case
2006-05-27 20:33:53 -04:00
when popup && method
2007-03-13 01:12:59 -04:00
raise ActionView :: ActionViewError , " You can't use :popup and :method in the same link "
2005-09-07 08:56:33 -04:00
when confirm && popup
" if ( #{ confirm_javascript_function ( confirm ) } ) { #{ popup_javascript_function ( popup ) } };return false; "
2006-05-27 20:33:53 -04:00
when confirm && method
2009-03-16 11:20:15 -04:00
" if ( #{ confirm_javascript_function ( confirm ) } ) { #{ method_javascript_function ( method , url , href ) } };return false; "
2005-09-07 08:56:33 -04:00
when confirm
" return #{ confirm_javascript_function ( confirm ) } ; "
2006-05-27 20:33:53 -04:00
when method
2007-06-23 13:11:01 -04:00
" #{ method_javascript_function ( method , url , href ) } return false; "
2005-09-07 08:56:33 -04:00
when popup
2008-06-21 17:54:10 -04:00
" #{ popup_javascript_function ( popup ) } return false; "
2005-09-08 13:54:16 -04:00
else
html_options [ " onclick " ]
2004-11-23 20:04:44 -05:00
end
end
2007-03-13 01:12:59 -04:00
2005-09-07 08:56:33 -04:00
def confirm_javascript_function ( confirm )
" confirm(' #{ escape_javascript ( confirm ) } ') "
2005-09-03 20:33:45 -04:00
end
2007-03-13 01:12:59 -04:00
2005-09-07 08:56:33 -04:00
def popup_javascript_function ( popup )
popup . is_a? ( Array ) ? " window.open(this.href,' #{ popup . first } ',' #{ popup . last } '); " : " window.open(this.href); "
2005-09-03 20:33:45 -04:00
end
2007-03-13 01:12:59 -04:00
2007-06-23 13:11:01 -04:00
def method_javascript_function ( method , url = '' , href = nil )
action = ( href && url . size > 0 ) ? " ' #{ url } ' " : 'this.href'
2007-03-13 01:12:59 -04:00
submit_function =
2006-05-27 20:33:53 -04:00
" var f = document.createElement('form'); f.style.display = 'none'; " +
2007-06-23 13:11:01 -04:00
" this.parentNode.appendChild(f); f.method = 'POST'; f.action = #{ action } ; "
2007-03-13 01:12:59 -04:00
2006-05-27 20:33:53 -04:00
unless method == :post
submit_function << " var m = document.createElement('input'); m.setAttribute('type', 'hidden'); "
submit_function << " m.setAttribute('name', '_method'); m.setAttribute('value', ' #{ method } '); f.appendChild(m); "
end
2007-03-13 01:12:59 -04:00
2007-09-28 11:55:45 -04:00
if protect_against_forgery?
2007-09-22 22:32:55 -04:00
submit_function << " var s = document.createElement('input'); s.setAttribute('type', 'hidden'); "
2007-09-23 14:14:44 -04:00
submit_function << " s.setAttribute('name', ' #{ request_forgery_protection_token } '); s.setAttribute('value', ' #{ escape_javascript form_authenticity_token } '); f.appendChild(s); "
2007-09-22 22:32:55 -04:00
end
2006-05-27 20:33:53 -04:00
submit_function << " f.submit(); "
2005-09-07 08:56:33 -04:00
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