aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/type
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/type')
-rw-r--r--activerecord/lib/active_record/type/decimal.rb10
-rw-r--r--activerecord/lib/active_record/type/integer.rb6
-rw-r--r--activerecord/lib/active_record/type/numeric.rb2
-rw-r--r--activerecord/lib/active_record/type/serialized.rb2
-rw-r--r--activerecord/lib/active_record/type/string.rb4
-rw-r--r--activerecord/lib/active_record/type/unsigned_integer.rb15
-rw-r--r--activerecord/lib/active_record/type/value.rb8
7 files changed, 41 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/type/decimal.rb b/activerecord/lib/active_record/type/decimal.rb
index d10778eeb6..7b2bee2c42 100644
--- a/activerecord/lib/active_record/type/decimal.rb
+++ b/activerecord/lib/active_record/type/decimal.rb
@@ -16,7 +16,7 @@ module ActiveRecord
def cast_value(value)
case value
when ::Float
- BigDecimal(value, float_precision)
+ convert_float_to_big_decimal(value)
when ::Numeric, ::String
BigDecimal(value, precision.to_i)
else
@@ -28,6 +28,14 @@ module ActiveRecord
end
end
+ def convert_float_to_big_decimal(value)
+ if precision
+ BigDecimal(value, float_precision)
+ else
+ value.to_d
+ end
+ end
+
def float_precision
if precision.to_i > ::Float::DIG + 1
::Float::DIG + 1
diff --git a/activerecord/lib/active_record/type/integer.rb b/activerecord/lib/active_record/type/integer.rb
index 750f353472..fc260a081a 100644
--- a/activerecord/lib/active_record/type/integer.rb
+++ b/activerecord/lib/active_record/type/integer.rb
@@ -5,7 +5,7 @@ module ActiveRecord
def initialize(*)
super
- @range = -max_value...max_value
+ @range = min_value...max_value
end
def type
@@ -46,6 +46,10 @@ module ActiveRecord
limit = self.limit || 4
1 << (limit * 8 - 1) # 8 bits per byte with one bit for sign
end
+
+ def min_value
+ -max_value
+ end
end
end
end
diff --git a/activerecord/lib/active_record/type/numeric.rb b/activerecord/lib/active_record/type/numeric.rb
index fa43266504..674f996f38 100644
--- a/activerecord/lib/active_record/type/numeric.rb
+++ b/activerecord/lib/active_record/type/numeric.rb
@@ -29,7 +29,7 @@ module ActiveRecord
# 'wibble'.to_i will give zero, we want to make sure
# that we aren't marking int zero to string zero as
# changed.
- value.to_s !~ /\A\d+\.?\d*\z/
+ value.to_s !~ /\A-?\d+\.?\d*\z/
end
end
end
diff --git a/activerecord/lib/active_record/type/serialized.rb b/activerecord/lib/active_record/type/serialized.rb
index 3191a868ef..3cac03464e 100644
--- a/activerecord/lib/active_record/type/serialized.rb
+++ b/activerecord/lib/active_record/type/serialized.rb
@@ -29,7 +29,7 @@ module ActiveRecord
def changed_in_place?(raw_old_value, value)
return false if value.nil?
- subtype.changed_in_place?(raw_old_value, coder.dump(value))
+ subtype.changed_in_place?(raw_old_value, type_cast_for_database(value))
end
def accessor
diff --git a/activerecord/lib/active_record/type/string.rb b/activerecord/lib/active_record/type/string.rb
index fbc0af2c5a..cf95e25be0 100644
--- a/activerecord/lib/active_record/type/string.rb
+++ b/activerecord/lib/active_record/type/string.rb
@@ -21,6 +21,10 @@ module ActiveRecord
end
end
+ def text?
+ true
+ end
+
private
def cast_value(value)
diff --git a/activerecord/lib/active_record/type/unsigned_integer.rb b/activerecord/lib/active_record/type/unsigned_integer.rb
new file mode 100644
index 0000000000..ed3e527483
--- /dev/null
+++ b/activerecord/lib/active_record/type/unsigned_integer.rb
@@ -0,0 +1,15 @@
+module ActiveRecord
+ module Type
+ class UnsignedInteger < Integer # :nodoc:
+ private
+
+ def max_value
+ super * 2
+ end
+
+ def min_value
+ 0
+ end
+ end
+ end
+end
diff --git a/activerecord/lib/active_record/type/value.rb b/activerecord/lib/active_record/type/value.rb
index 9456a4a56c..60ae47db3d 100644
--- a/activerecord/lib/active_record/type/value.rb
+++ b/activerecord/lib/active_record/type/value.rb
@@ -50,6 +50,10 @@ module ActiveRecord
# These predicates are not documented, as I need to look further into
# their use, and see if they can be removed entirely.
+ def text? # :nodoc:
+ false
+ end
+
def number? # :nodoc:
false
end
@@ -91,8 +95,8 @@ module ActiveRecord
# Convenience method for types which do not need separate type casting
# behavior for user and database inputs. Called by
- # `type_cast_from_database` and `type_cast_from_user` for all values
- # except `nil`.
+ # +type_cast_from_database+ and +type_cast_from_user+ for all values
+ # except +nil+.
def cast_value(value) # :doc:
value
end