diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 9c9ed40c6fc9fa00ecb309614fe48206ff8af561..f1c8122518705e1dbdcb68f6e890249481816112 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,10 @@
*SVN*
+* Added :popup option to UrlHelper#link_to #1996 [gabriel.gironda@gmail.com]. Examples:
+
+ link_to "Help", { :action => "help" }, :popup => true
+ link_to "Busy loop", { :action => "busy" }, :popup => ['new_window', 'height=300,width=600']
+
* Drop trailing \000 if present on RAW_POST_DATA (works around bug in Safari Ajax implementation) #918
* Fix observe_field to fall back to event-based observation if frequency <= 0 #1916 [michael@schubert.cx]
diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb
index 5bcfb5e75d1fe9a989dbe937d888b286466e4745..64288cb4bdeaae228d7795b126f1eb68ca1e2f86 100644
--- a/actionpack/lib/action_view/helpers/url_helper.rb
+++ b/actionpack/lib/action_view/helpers/url_helper.rb
@@ -19,14 +19,20 @@ def url_for(options = {}, *parameters_for_method_reference)
# Creates a link tag of the given +name+ using an URL created by the set of +options+. See the valid options in
# link:classes/ActionController/Base.html#M000021. It's also possible to pass a string instead of an options hash to
# get a link tag that just points without consideration. If nil is passed as a name, the link itself will become the name.
- # The html_options have a special feature for creating javascript confirm alerts where if you pass :confirm => 'Are you sure?',
+ #
+ # The html_options has two special features. One for creating javascript confirm alerts where if you pass :confirm => 'Are you sure?',
# the link will be guarded with a JS popup asking that question. If the user accepts, the link is processed, otherwise not.
#
- # Example:
+ # The other for creating a popup window, which is done by either passing :popup with true or the options of the window in
+ # Javascript form.
+ #
+ # Examples:
# link_to "Delete this page", { :action => "destroy", :id => @page.id }, :confirm => "Are you sure?"
+ # link_to "Help", { :action => "help" }, :popup => true
+ # link_to "Busy loop", { :action => "busy" }, :popup => ['new_window', 'height=300,width=600']
def link_to(name, options = {}, html_options = nil, *parameters_for_method_reference)
html_options = (html_options || {}).stringify_keys
- convert_confirm_option_to_javascript!(html_options)
+ convert_options_to_javascript!(html_options)
if options.is_a?(String)
content_tag "a", name || options, (html_options || {}).merge("href" => options)
else
@@ -224,9 +230,23 @@ def current_page?(options)
def convert_confirm_option_to_javascript!(html_options)
if confirm = html_options.delete("confirm")
html_options["onclick"] = "return confirm('#{escape_javascript(confirm)}');"
+ return confirm
end
end
+
+ def convert_popup_option_to_javascript!(html_options, confirm_message = false)
+ if popup = html_options.delete("popup")
+ popup_js = popup.is_a?(Array) ? "window.open(this.href,'#{popup.first}','#{popup.last}');" : "window.open(this.href);"
+ html_options["onclick"] = popup_js + 'return false;' unless confirm_message
+ html_options["onclick"] = "if (confirm('#{escape_javascript(confirm_message)}')) { #{popup_js} };return false;" if confirm_message
+ end
+ end
+ def convert_options_to_javascript!(html_options)
+ confirm_message = convert_confirm_option_to_javascript!(html_options)
+ convert_popup_option_to_javascript!(html_options, confirm_message)
+ end
+
# Processes the _html_options_ hash, converting the boolean
# attributes from true/false form into the form required by
# HTML/XHTML. (An attribute is considered to be boolean if
diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb
index 8367108fabccf9e5785580032e5d805f380bb8b8..73d87f6956afd6a7acdb0e8790cfcce00d6f8ac1 100644
--- a/actionpack/test/template/url_helper_test.rb
+++ b/actionpack/test/template/url_helper_test.rb
@@ -62,6 +62,32 @@ def test_link_tag_with_javascript_confirm
)
end
+ def test_link_tag_with_popup
+ assert_equal(
+ "Hello",
+ link_to("Hello", "http://www.example.com", :popup => true)
+ )
+ assert_equal(
+ "Hello",
+ link_to("Hello", "http://www.example.com", :popup => 'true')
+ )
+ assert_equal(
+ "Hello",
+ link_to("Hello", "http://www.example.com", :popup => ['window_name', 'width=300,height=300'])
+ )
+ end
+
+ def test_link_tag_with_popup_and_javascript_confirm
+ assert_equal(
+ "Hello",
+ link_to("Hello", "http://www.example.com", { :popup => true, :confirm => "Fo' sho'?" })
+ )
+ assert_equal(
+ "Hello",
+ link_to("Hello", "http://www.example.com", { :popup => ['window_name', 'width=300,height=300'], :confirm => "Are you serious?" })
+ )
+ end
+
def test_link_to_unless
assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog")
assert "Listing", link_to_unless(false, "Listing", :action => "list", :controller => "weblog")