aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/form_tag_helper.rb
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2007-01-17 06:25:30 +0000
committerRick Olson <technoweenie@gmail.com>2007-01-17 06:25:30 +0000
commit1ff84503b4a6f1da1cee125d5f872d760b159ba2 (patch)
tree9c04729be30b2ada7ee0217057f549033fa4db58 /actionpack/lib/action_view/helpers/form_tag_helper.rb
parent9ec31ba7411525bbc2ed9df3679420042d4ba08d (diff)
downloadrails-1ff84503b4a6f1da1cee125d5f872d760b159ba2.tar.gz
rails-1ff84503b4a6f1da1cee125d5f872d760b159ba2.tar.bz2
rails-1ff84503b4a6f1da1cee125d5f872d760b159ba2.zip
Refactor #form_tag to allow easy extending. [Rick]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5972 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view/helpers/form_tag_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/form_tag_helper.rb58
1 files changed, 37 insertions, 21 deletions
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("</form>", 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("</form>", block.binding)
+ end
end
end
end