Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit 7b5ed66122873eebb773a6418f3a94d946cc4f8c 1 parent 61960e7
@dhh dhh authored
View
3  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).
View
2  actionpack/lib/action_view/helpers/active_record_helper.rb
@@ -132,7 +132,6 @@ def to_tag(options = {})
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 @@ def tag(name, options)
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))
View
14 actionpack/lib/action_view/helpers/form_helper.rb
@@ -135,15 +135,15 @@ def to_input_field_tag(field_type, options = {})
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 @@ def to_radio_button_tag(tag_value, options={})
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 @@ def value
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"
View
5 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
View
7 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
View
3  activerecord/CHANGELOG
@@ -1,5 +1,8 @@
*SVN*
+* Added respondence to *_before_type_cast for all attributes to return their string-state before they were type casted by the column type.
+ This is helpful for getting "100,000" back on a integer-based validation where the value would normally be "100".
+
* Fixed the automated timestamping feature when running under Rails' development environment that resets the inheritable attributes on each request.
* Added Base#update_attributes that'll accept a hash of attributes and save the record (returning true if it passed validation, false otherwise).
View
23 activerecord/lib/active_record/base.rb
@@ -920,10 +920,10 @@ def ensure_proper_type
def method_missing(method_id, *arguments)
method_name = method_id.id2name
-
-
if method_name =~ read_method? && @attributes.include?($1)
return read_attribute($1)
+ elsif method_name =~ read_untyped_method? && @attributes.include?($1)
+ return read_attribute_before_type_cast($1)
elsif method_name =~ write_method? && @attributes.include?($1)
write_attribute($1, arguments[0])
elsif method_name =~ query_method? && @attributes.include?($1)
@@ -933,25 +933,30 @@ def method_missing(method_id, *arguments)
end
end
- def read_method?() /^([a-zA-Z][-_\w]*)[^=?]*$/ end
- def write_method?() /^([a-zA-Z][-_\w]*)=.*$/ end
- def query_method?() /^([a-zA-Z][-_\w]*)\?$/ end
+ def read_method?() /^([a-zA-Z][-_\w]*)[^=?]*$/ end
+ def read_untyped_method?() /^([a-zA-Z][-_\w]*)_before_type_cast$/ end
+ def write_method?() /^([a-zA-Z][-_\w]*)=.*$/ end
+ def query_method?() /^([a-zA-Z][-_\w]*)\?$/ end
- # Returns the value of attribute identified by <tt>attr_name</tt> after it has been type cast (for example,
+ # Returns the value of attribute identified by <tt>attr_name</tt> after it has been type cast (for example,
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
def read_attribute(attr_name) #:doc:
if @attributes.keys.include? attr_name
if column = column_for_attribute(attr_name)
- @attributes[attr_name] = unserializable_attribute?(attr_name, column) ?
+ unserializable_attribute?(attr_name, column) ?
unserialize_attribute(attr_name) : column.type_cast(@attributes[attr_name])
+ else
+ @attributes[attr_name]
end
-
- @attributes[attr_name]
else
nil
end
end
+ def read_attribute_before_type_cast(attr_name)
+ @attributes[attr_name]
+ end
+
# Returns true if the attribute is of a text column and marked for serialization.
def unserializable_attribute?(attr_name, column)
@attributes[attr_name] && column.send(:type) == :text && @attributes[attr_name].is_a?(String) && self.class.serialized_attributes[attr_name]
View
2  activerecord/test/abstract_unit.rb
@@ -4,6 +4,8 @@
require 'test/unit'
require 'active_record'
require 'active_record/fixtures'
+require 'active_record/support/binding_of_caller'
+require 'active_record/support/breakpoint'
require 'connection'
class Test::Unit::TestCase #:nodoc:
View
6 activerecord/test/fixtures/developer.rb
@@ -1,8 +1,6 @@
class Developer < ActiveRecord::Base
has_and_belongs_to_many :projects
- protected
- def validate
- errors.add_on_boundary_breaking("name", 3..20)
- end
+ validates_inclusion_of :salary, :in => 50000..200000
+ validates_length_of :name, :within => 3..20
end
View
3  activerecord/test/fixtures/developers.yml
@@ -1,13 +1,16 @@
david:
id: 1
name: David
+ salary: 80000
jamis:
id: 2
name: Jamis
+ salary: 150000
<% for digit in 3..10 %>
dev_<%= digit %>:
id: <%= digit %>
name: fixture_<%= digit %>
+ salary: 100000
<% end %>
View
9 activerecord/test/validations_test.rb
@@ -348,4 +348,11 @@ def test_validates_length_of_custom_errors_for_is_with_wrong_length
assert_equal "hoo 5", t.errors["title"]
end
-end
+
+ def test_throw_away_typing
+ d = Developer.create "name" => "David", "salary" => "100,000"
+ assert !d.valid?
+ assert_not_equal "100,000", d.salary
+ assert_equal "100,000", d.salary_before_type_cast
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.