Skip to content

Commit

Permalink
Refactor #form_tag to allow easy extending. [Rick]
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5972 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
technoweenie committed Jan 17, 2007
1 parent 9ec31ba commit 1ff8450
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
@@ -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]
Expand Down
58 changes: 37 additions & 21 deletions actionpack/lib/action_view/helpers/form_tag_helper.rb
Expand Up @@ -31,29 +31,11 @@ module FormTagHelper
# 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

Expand Down Expand Up @@ -171,6 +153,40 @@ def submit_tag(value = "Save changes", options = {})
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

0 comments on commit 1ff8450

Please sign in to comment.