diff options
author | Carl Lerche <carllerche@mac.com> | 2010-03-12 14:34:13 -0800 |
---|---|---|
committer | Carl Lerche <carllerche@mac.com> | 2010-03-12 14:34:13 -0800 |
commit | 61916e408d86d19d1659cd8042de6503aecc6c98 (patch) | |
tree | 28cbadb9b39b44d993a033af5cf254655cdab58a /lib/arel/algebra | |
parent | 83c27c0b5e2e341307b7a160d831fb930a9552b4 (diff) | |
download | rails-61916e408d86d19d1659cd8042de6503aecc6c98.tar.gz rails-61916e408d86d19d1659cd8042de6503aecc6c98.tar.bz2 rails-61916e408d86d19d1659cd8042de6503aecc6c98.zip |
Add a bunch of specs for attribute type casting.
Diffstat (limited to 'lib/arel/algebra')
-rw-r--r-- | lib/arel/algebra/attributes/attribute.rb | 13 | ||||
-rw-r--r-- | lib/arel/algebra/attributes/boolean.rb | 11 |
2 files changed, 11 insertions, 13 deletions
diff --git a/lib/arel/algebra/attributes/attribute.rb b/lib/arel/algebra/attributes/attribute.rb index f4cec828e3..03cf44a552 100644 --- a/lib/arel/algebra/attributes/attribute.rb +++ b/lib/arel/algebra/attributes/attribute.rb @@ -160,16 +160,13 @@ module Arel def type_cast_to_numeric(value, method) return unless value if value.respond_to?(:to_str) - if value.to_str =~ /\A(-?(?:0|[1-9]\d*)(?:\.\d+)?|(?:\.\d+))\z/ - $1.send(method) - else - value - end + str = value.to_str.strip + return if str.empty? + return $1.send(method) if str =~ /\A(-?(?:0|[1-9]\d*)(?:\.\d+)?|(?:\.\d+))\z/ elsif value.respond_to?(method) - value.send(method) - else - raise typecast_error(value) + return value.send(method) end + raise typecast_error(value) end def typecast_error(value) diff --git a/lib/arel/algebra/attributes/boolean.rb b/lib/arel/algebra/attributes/boolean.rb index 0ca7cd6d24..d69f2465df 100644 --- a/lib/arel/algebra/attributes/boolean.rb +++ b/lib/arel/algebra/attributes/boolean.rb @@ -3,11 +3,12 @@ module Arel class Boolean < Attribute def type_cast(value) case value - when true, false then value - when nil then options[:allow_nil] ? nil : false - when 1 then true - when 0 then false - else + when true, false then value + # when nil then options[:allow_nil] ? nil : false + when nil then false + when 1 then true + when 0 then false + else case value.to_s.downcase.strip when 'true' then true when 'false' then false |