diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-19 11:25:55 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-19 11:25:55 +0000 |
commit | 7b5ed66122873eebb773a6418f3a94d946cc4f8c (patch) | |
tree | 63c77291a6c78c7446bb5041b062bc8485a0fb5f /activerecord | |
parent | 61960e7b37767140e9af68bd5373e06dce08492d (diff) | |
download | rails-7b5ed66122873eebb773a6418f3a94d946cc4f8c.tar.gz rails-7b5ed66122873eebb773a6418f3a94d946cc4f8c.tar.bz2 rails-7b5ed66122873eebb773a6418f3a94d946cc4f8c.zip |
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
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 3 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 23 | ||||
-rwxr-xr-x | activerecord/test/abstract_unit.rb | 2 | ||||
-rw-r--r-- | activerecord/test/fixtures/developer.rb | 6 | ||||
-rw-r--r-- | activerecord/test/fixtures/developers.yml | 3 | ||||
-rwxr-xr-x | activerecord/test/validations_test.rb | 9 |
6 files changed, 32 insertions, 14 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index e9927ae980..573fa9f618 100644 --- a/activerecord/CHANGELOG +++ b/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). 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 <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] diff --git a/activerecord/test/abstract_unit.rb b/activerecord/test/abstract_unit.rb index 293f3ba6b3..67984a4609 100755 --- a/activerecord/test/abstract_unit.rb +++ b/activerecord/test/abstract_unit.rb @@ -4,6 +4,8 @@ $:.unshift(File.dirname(__FILE__) + '/../lib') 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: diff --git a/activerecord/test/fixtures/developer.rb b/activerecord/test/fixtures/developer.rb index 1c4cc30506..78f8f54db7 100644 --- a/activerecord/test/fixtures/developer.rb +++ b/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 diff --git a/activerecord/test/fixtures/developers.yml b/activerecord/test/fixtures/developers.yml index 733455f789..fc33af99b6 100644 --- a/activerecord/test/fixtures/developers.yml +++ b/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 %>
\ No newline at end of file diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb index b6c9cfed7d..8949206706 100755 --- a/activerecord/test/validations_test.rb +++ b/activerecord/test/validations_test.rb @@ -348,4 +348,11 @@ class ValidationsTest < Test::Unit::TestCase 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
\ No newline at end of file |