From 7b5ed66122873eebb773a6418f3a94d946cc4f8c Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 19 Dec 2004 11:25:55 +0000 Subject: Added respondence to *_before_type_cast for all attributes to return their string-state before they were type casted by the column type. Added use of *_before_type_cast for all input and text fields. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@215 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 3 +++ actionpack/lib/action_view/helpers/active_record_helper.rb | 2 -- actionpack/lib/action_view/helpers/form_helper.rb | 14 +++++++++----- actionpack/test/template/active_record_helper_test.rb | 5 +++++ actionpack/test/template/form_helper_test.rb | 7 ++++++- 5 files changed, 23 insertions(+), 8 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 548c9b530f..d81c12f027 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,8 @@ *SVN* +* Added use of *_before_type_cast for all input and text fields. This is helpful for getting "100,000" back on a integer-based + validation where the value would normally be "100". + * Added Request#port_string to get something like ":8080" back on 8080 and "" on 80 (or 443 with https). * Added Request#domain (returns string) and Request#subdomains (returns array). diff --git a/actionpack/lib/action_view/helpers/active_record_helper.rb b/actionpack/lib/action_view/helpers/active_record_helper.rb index 07ede179e6..6855fa49c5 100644 --- a/actionpack/lib/action_view/helpers/active_record_helper.rb +++ b/actionpack/lib/action_view/helpers/active_record_helper.rb @@ -132,7 +132,6 @@ module ActionView end alias_method :tag_without_error_wrapping, :tag - def tag(name, options) if object.respond_to?("errors") && object.errors.respond_to?("on") error_wrapping(tag_without_error_wrapping(name, options), object.errors.on(@method_name)) @@ -142,7 +141,6 @@ module ActionView end alias_method :content_tag_without_error_wrapping, :content_tag - def content_tag(name, value, options) if object.respond_to?("errors") && object.errors.respond_to?("on") error_wrapping(content_tag_without_error_wrapping(name, value, options), object.errors.on(@method_name)) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 253b389a79..d9a1c7d55e 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -135,15 +135,15 @@ module ActionView html_options.merge!({ "size" => options["maxlength"]}) if options["maxlength"] && !options["size"] html_options.delete("size") if field_type == "hidden" html_options.merge!({ "type" => field_type}) - html_options.merge!({ "value" => value.to_s }) unless options["value"] + html_options.merge!({ "value" => value_before_type_cast }) unless options["value"] add_default_name_and_id(html_options) tag("input", html_options) end def to_radio_button_tag(tag_value, options={}) html_options = DEFAULT_FIELD_OPTIONS.merge(options) - html_options.merge!({"checked"=>"checked"}) if value == tag_value - html_options.merge!({"type"=>"radio", "value"=>tag_value.to_s}) + html_options.merge!({ "checked" => "checked" }) if value == tag_value + html_options.merge!({ "type" => "radio", "value"=> tag_value.to_s }) add_default_name_and_id(html_options) tag("input", html_options) @@ -152,11 +152,11 @@ module ActionView def to_text_area_tag(options = {}) options = DEFAULT_TEXT_AREA_OPTIONS.merge(options) add_default_name_and_id(options) - content_tag("textarea", html_escape(value), options) + content_tag("textarea", html_escape(value_before_type_cast), options) end def to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0") - options.merge!({"checked" => "checked"}) if !value.nil? && ((value.is_a?(TrueClass) || value.is_a?(FalseClass)) ? value : value.to_i > 0) + options.merge!({ "checked" => "checked" }) if !value.nil? && ((value.is_a?(TrueClass) || value.is_a?(FalseClass)) ? value : value.to_i > 0) options.merge!({ "type" => "checkbox", "value" => checked_value }) add_default_name_and_id(options) tag("input", options) << tag("input", ({ "name" => options['name'], "type" => "hidden", "value" => unchecked_value })) @@ -191,6 +191,10 @@ module ActionView object.send(@method_name) unless object.nil? end + def value_before_type_cast + object.send(@method_name + "_before_type_cast") unless object.nil? + end + private def add_default_name_and_id(options) options['name'] = tag_name unless options.has_key? "name" diff --git a/actionpack/test/template/active_record_helper_test.rb b/actionpack/test/template/active_record_helper_test.rb index f9a2727067..e650b4f0b8 100644 --- a/actionpack/test/template/active_record_helper_test.rb +++ b/actionpack/test/template/active_record_helper_test.rb @@ -14,6 +14,11 @@ class ActiveRecordHelperTest < Test::Unit::TestCase include ActionView::Helpers::UrlHelper Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on) + Post.class_eval do + alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast) + alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast) + alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast) + end Column = Struct.new("Column", :type, :name, :human_name) def setup diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index d81e2ac270..c6128a9337 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -6,7 +6,12 @@ class FormHelperTest < Test::Unit::TestCase include ActionView::Helpers::FormHelper old_verbose, $VERBOSE = $VERBOSE, nil - Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on) + Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on, :cost) + Post.class_eval do + alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast) + alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast) + alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast) + end $VERBOSE = old_verbose def setup -- cgit v1.2.3