From 1ff84503b4a6f1da1cee125d5f872d760b159ba2 Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Wed, 17 Jan 2007 06:25:30 +0000 Subject: Refactor #form_tag to allow easy extending. [Rick] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5972 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/action_view/helpers/form_tag_helper.rb | 58 ++++++++++++++-------- 1 file changed, 37 insertions(+), 21 deletions(-) (limited to 'actionpack/lib/action_view/helpers/form_tag_helper.rb') diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index e2e796e7c8..3de3a9372b 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -31,29 +31,11 @@ module ActionView # If "put", "delete", or another verb is used, a hidden input with name _method # is added to simulate the verb over post. def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block) - html_options = options.stringify_keys - html_options["enctype"] = "multipart/form-data" if html_options.delete("multipart") - html_options["action"] = url_for(url_for_options, *parameters_for_url) - - method_tag = "" - - case method = html_options.delete("method").to_s - when /^get$/i # must be case-insentive, but can't use downcase as might be nil - html_options["method"] = "get" - when /^post$/i, "", nil - html_options["method"] = "post" - else - html_options["method"] = "post" - method_tag = content_tag(:div, tag(:input, :type => "hidden", :name => "_method", :value => method), :style => 'margin:0;padding:0') - end - + html_options = html_options_for_form(url_for_options, options, *parameters_for_url) if block_given? - content = capture(&block) - concat(tag(:form, html_options, true) + method_tag, block.binding) - concat(content, block.binding) - concat("", block.binding) + form_tag_in_block(html_options, &block) else - tag(:form, html_options, true) + method_tag + form_tag_html(html_options) end end @@ -171,6 +153,40 @@ module ActionView def image_submit_tag(source, options = {}) tag :input, { "type" => "image", "src" => image_path(source) }.update(options.stringify_keys) end + + private + def html_options_for_form(url_for_options, options, *parameters_for_url) + returning options.stringify_keys do |html_options| + html_options["enctype"] = "multipart/form-data" if html_options.delete("multipart") + html_options["action"] = url_for(url_for_options, *parameters_for_url) + end + end + + def extra_tags_for_form(html_options) + case method = html_options.delete("method").to_s + when /^get$/i # must be case-insentive, but can't use downcase as might be nil + html_options["method"] = "get" + '' + when /^post$/i, "", nil + html_options["method"] = "post" + '' + else + html_options["method"] = "post" + content_tag(:div, tag(:input, :type => "hidden", :name => "_method", :value => method), :style => 'margin:0;padding:0') + end + end + + def form_tag_html(html_options) + extra_tags = extra_tags_for_form(html_options) + tag(:form, html_options, true) + extra_tags + end + + def form_tag_in_block(html_options, &block) + content = capture(&block) + concat(form_tag_html(html_options), block.binding) + concat(content, block.binding) + concat("", block.binding) + end end end end -- cgit v1.2.3