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 --- activerecord/lib/active_record/base.rb | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index b0de050185..38002ad2a0 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -920,10 +920,10 @@ module ActiveRecord #:nodoc: 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 @@ module ActiveRecord #:nodoc: 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 attr_name after it has been type cast (for example, + # Returns the value of attribute identified by attr_name 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] -- cgit v1.2.3