aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG5
-rw-r--r--actionpack/lib/action_view/helpers/url_helper.rb26
-rw-r--r--actionpack/test/template/url_helper_test.rb26
3 files changed, 54 insertions, 3 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 9c9ed40c6f..f1c8122518 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 5bcfb5e75d..64288cb4bd 100644
--- a/actionpack/lib/action_view/helpers/url_helper.rb
+++ b/actionpack/lib/action_view/helpers/url_helper.rb
@@ -19,14 +19,20 @@ module ActionView
# 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 @@ module ActionView
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 8367108fab..73d87f6956 100644
--- a/actionpack/test/template/url_helper_test.rb
+++ b/actionpack/test/template/url_helper_test.rb
@@ -62,6 +62,32 @@ class UrlHelperTest < Test::Unit::TestCase
)
end
+ def test_link_tag_with_popup
+ assert_equal(
+ "<a href=\"http://www.example.com\" onclick=\"window.open(this.href);return false;\">Hello</a>",
+ link_to("Hello", "http://www.example.com", :popup => true)
+ )
+ assert_equal(
+ "<a href=\"http://www.example.com\" onclick=\"window.open(this.href);return false;\">Hello</a>",
+ link_to("Hello", "http://www.example.com", :popup => 'true')
+ )
+ assert_equal(
+ "<a href=\"http://www.example.com\" onclick=\"window.open(this.href,'window_name','width=300,height=300');return false;\">Hello</a>",
+ 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(
+ "<a href=\"http://www.example.com\" onclick=\"if (confirm('Fo\\' sho\\'?')) { window.open(this.href); };return false;\">Hello</a>",
+ link_to("Hello", "http://www.example.com", { :popup => true, :confirm => "Fo' sho'?" })
+ )
+ assert_equal(
+ "<a href=\"http://www.example.com\" onclick=\"if (confirm('Are you serious?')) { window.open(this.href,'window_name','width=300,height=300'); };return false;\">Hello</a>",
+ 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 "<a href=\"http://www.example.com\">Listing</a>", link_to_unless(false, "Listing", :action => "list", :controller => "weblog")