Refactor #form_tag to allow easy extending. [Rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5972 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Rick Olson
2007-01-17 06:25:30 +00:00
parent 9ec31ba741
commit 1ff84503b4
2 changed files with 39 additions and 21 deletions

View File

@@ -1,5 +1,7 @@
*SVN*
* Refactor #form_tag to allow easy extending. [Rick]
* Update to Prototype 1.5.0. [Sam Stephenson]
* RecordInvalid, RecordNotSaved => 422 Unprocessable Entity, StaleObjectError => 409 Conflict. #7097 [dkubb]

View File

@@ -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