diff options
Diffstat (limited to 'actionpack/lib/action_view/helpers/url_helper.rb')
-rw-r--r-- | actionpack/lib/action_view/helpers/url_helper.rb | 80 |
1 files changed, 61 insertions, 19 deletions
diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index 4424dbba42..bd179ef0b3 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -120,10 +120,6 @@ module ActionView # * <tt>:confirm => 'question?'</tt> - This will add a JavaScript confirm # prompt with the question specified. If the user accepts, the link is # processed normally, otherwise no action is taken. - # * <tt>:popup => true || array of window options</tt> - This will force the - # 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 - # that are passed to the <tt>window.open</tt> JavaScript call. # * <tt>:method => symbol of HTTP verb</tt> - This modifier will dynamically # create an HTML form and immediately submit the form for processing using # the HTTP verb specified. Useful for having links perform a POST operation @@ -136,10 +132,6 @@ module ActionView # the request object's methods for <tt>post?</tt>, <tt>delete?</tt> or <tt>put?</tt>. # * The +html_options+ will accept a hash of html attributes for the link tag. # - # You can mix and match the +html_options+ with the exception of - # <tt>:popup</tt> and <tt>:method</tt> which will raise an - # <tt>ActionView::ActionViewError</tt> exception. - # # ==== Examples # 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 @@ -203,17 +195,11 @@ module ActionView # link_to "Nonsense search", searches_path(:foo => "bar", :baz => "quux") # # => <a href="/searches?foo=bar&baz=quux">Nonsense search</a> # - # The three options specific to +link_to+ (<tt>:confirm</tt>, <tt>:popup</tt>, and <tt>:method</tt>) are used as follows: + # The three options specific to +link_to+ (<tt>:confirm</tt> and <tt>:method</tt>) are used as follows: # # link_to "Visit Other Site", "http://www.rubyonrails.org/", :confirm => "Are you sure?" # # => <a href="http://www.rubyonrails.org/" onclick="return confirm('Are you sure?');">Visit Other Site</a> # - # link_to "Help", { :action => "help" }, :popup => true - # # => <a href="/testing/help/" onclick="window.open(this.href);return false;">Help</a> - # - # 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> - # # link_to "Delete Image", @image, :confirm => "Are you sure?", :method => :delete # # => <a href="/images/9" onclick="if (confirm('Are you sure?')) { var f = document.createElement('form'); # f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href; @@ -232,11 +218,11 @@ module ActionView html_options = args[2] url = url_for(options) + html_options = convert_options_to_data_attributes(options, html_options) 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) else tag_options = nil @@ -305,11 +291,10 @@ module ActionView request_token_tag = tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token) end - url = options.is_a?(String) ? options : self.url_for(options) name ||= url - - convert_options_to_javascript!(html_options, url) + + html_options = convert_options_to_data_attributes(options, html_options) html_options.merge!("type" => "submit", "value" => name) @@ -562,6 +547,63 @@ module ActionView end private + def convert_options_to_data_attributes(options, html_options) + html_options = {} if html_options.nil? + html_options = html_options.stringify_keys + + if (options.is_a?(Hash) && options.key?('remote')) || (html_options.is_a?(Hash) && html_options.key?('remote')) + html_options['data-remote'] = 'true' + options.delete('remote') if options.is_a?(Hash) + html_options.delete('remote') if html_options.is_a?(Hash) + end + + confirm = html_options.delete("confirm") + + if html_options.key?("popup") + ActiveSupport::Deprecation.warn(":popup has been deprecated", caller) + end + + method, href = html_options.delete("method"), html_options['href'] + + if confirm && method + add_confirm_to_attributes!(html_options, confirm) + add_method_to_attributes!(html_options, method) + elsif confirm + add_confirm_to_attributes!(html_options, confirm) + elsif method + add_method_to_attributes!(html_options, method) + end + + html_options["data-url"] = options[:url] if options.is_a?(Hash) && options[:url] + + html_options + end + + def add_confirm_to_attributes!(html_options, confirm) + html_options["data-confirm"] = confirm if confirm + end + + def add_method_to_attributes!(html_options, method) + html_options["rel"] = "nofollow" if method && method.to_s.downcase != "get" + html_options["data-method"] = method if method + end + + def options_for_javascript(options) + if options.empty? + '{}' + else + "{#{options.keys.map { |k| "#{k}:#{options[k]}" }.sort.join(', ')}}" + end + end + + def array_or_string_for_javascript(option) + if option.kind_of?(Array) + "['#{option.join('\',\'')}']" + elsif !option.nil? + "'#{option}'" + end + 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 |