From 967339e4d324daa659e5f625d48a56dc17d5287e Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 19 Feb 2005 12:49:14 +0000 Subject: [PATCH] Added FormTagHelper that provides a number of methods for creating form tags that doesn't rely on conventions with an object assigned to the template like FormHelper does. With the FormTagHelper, you provide the names and values yourself. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@687 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 + .../action_view/helpers/form_tag_helper.rb | 62 +++++++++++++++++++ .../lib/action_view/helpers/tag_helper.rb | 26 -------- .../test/template/form_tag_helper_test.rb | 19 ++++++ 4 files changed, 83 insertions(+), 26 deletions(-) create mode 100644 actionpack/lib/action_view/helpers/form_tag_helper.rb create mode 100644 actionpack/test/template/form_tag_helper_test.rb diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 3c4f8ee249..342f96f3ee 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added FormTagHelper that provides a number of methods for creating form tags that doesn't rely on conventions with an object assigned to the template like FormHelper does. With the FormTagHelper, you provide the names and values yourself. + * Added Iran and Irak to the countries list used by FormOptions#country_select and FormOptions#country_options_for_select * Renamed link_to_image to link_image_to (since thats what it actually does) -- kept alias for the old method name diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb new file mode 100644 index 0000000000..ba5fc39537 --- /dev/null +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -0,0 +1,62 @@ +require 'cgi' +require File.dirname(__FILE__) + '/tag_helper' + +module ActionView + module Helpers + # Provides a number of methods for creating form tags that doesn't rely on conventions with an object assigned to the template like + # FormHelper does. With the FormTagHelper, you provide the names and values yourself. + module FormTagHelper + # Starts a form tag that points the action to an url configured with url_for_options just like + # ActionController::Base#url_for. The method for the form defaults to POST. + # + # Options: + # * :multipart - If set to true, the enctype is set to "multipart/form-data". + def form_tag(url_for_options = {}, options = {}, *parameters_for_url) + html_options = { "method" => "post" }.merge(options) + + if html_options[:multipart] + html_options["enctype"] = "multipart/form-data" + html_options.delete(:multipart) + end + + html_options["action"] = url_for(url_for_options, *parameters_for_url) + + tag("form", html_options, true) + end + + alias_method :start_form_tag, :form_tag + + # Outputs "" + def end_form_tag + "" + end + + def text_field_tag(name, value = nil, options = {}) + tag("input", {"type" => "text", "name" => name, "id" => name, "value" => value}.update(options)) + end + + def password_field_tag(name = "password", value = nil, options = {}) + tag("input", {"type" => "password", "name" => name, "id" => name, "value" => value}.update(options)) + end + + def text_area_tag(name, content = nil, options = {}) + if options[:size] + options["cols"], options["rows"] = options[:size].split("x") + options.delete(:size) + end + + content_tag("textarea", content, { "name" => name, "id" => name }.update(options)) + end + + def check_box_tag(name, value = "1", checked = false, options = {}) + html_options = {"type" => "checkbox", "name" => name, "id" => name, "value" => value}.update(options) + html_options["checked"] = "checked" if checked + tag("input", html_options) + end + + def submit_tag(value = "Save changes", options = {}) + tag("input", {"type" => "submit", "name" => "submit", "value" => value}.update(options)) + end + end + end +end diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index 120dc0165b..5ebfecb8bd 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -22,32 +22,6 @@ module ActionView "<#{name}#{tag_options(options)}>#{content}" end - # Starts a form tag that points the action to an url configured with url_for_options just like - # ActionController::Base#url_for. The method for the form defaults to POST. - # - # Options: - # * :multipart - If set to true, the enctype is set to "multipart/form-data". - def form_tag(url_for_options = {}, options = {}, *parameters_for_url) - html_options = { "method" => "post" }.merge(options) - - if html_options[:multipart] - html_options["enctype"] = "multipart/form-data" - html_options.delete(:multipart) - end - - html_options["action"] = url_for(url_for_options, *parameters_for_url) - - tag("form", html_options, true) - end - - alias_method :start_form_tag, :form_tag - - # Outputs "" - def end_form_tag - "" - end - - private def tag_options(options) unless options.empty? diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb new file mode 100644 index 0000000000..5806c879fd --- /dev/null +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -0,0 +1,19 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_tag_helper' + +class TagHelperTest < Test::Unit::TestCase + include ActionView::Helpers::TagHelper + include ActionView::Helpers::FormTagHelper + + MethodToTag = { + %(text_field_tag("title", "Hello!")) => %(), + %(text_field_tag("title", "Hello!", "class" => "admin")) => %(), + %(password_field_tag) => %(), + %(text_area_tag("body", "hello world", :size => "20x40")) => %(), + %(check_box_tag("admin")) => %(), + } + + def test_tags + MethodToTag.each { |method, tag| assert_equal(eval(method), tag) } + end +end \ No newline at end of file