aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/type/numeric.rb
Commit message (Collapse)AuthorAgeFilesLines
* Move non-type objects into the `Type::Helpers` namespaceSean Griffin2015-02-071-36/+0
| | | | | | | The type code is actually quite accessible, and I'm planning to encourage people to look at the files in the `type` folder to learn more about how it works. This will help reduce the noise from code that is less about type casting, and more about random AR nonsense.
* Fixing numeric attrs when set to same negative valueDaniel Fox2014-12-231-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This bug occurs when an attribute of an ActiveRecord model is an ActiveRecord::Type::Integer type or a ActiveRecord::Type::Decimal type (or any other type that includes the ActiveRecord::Type::Numeric module. When the value of the attribute is negative and is set to the same negative value, it is marked as changed. Take the following example of a Person model with the integer attribute age: class Person < ActiveRecord::Base # age :integer(4) end The following will produce the error: person = Person.new(age: -1) person.age = -1 person.changes => { "age" => [-1, -1] } person.age_changed? => true The problematic line is here: module ActiveRecord module Type module Numeric ... def non_numeric_string?(value) # '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/ end end end end The regex match doesn't accept numbers with a leading '-'.
* Always assume strings with non-numeric characters change numeric typesSean Griffin2014-06-241-3/+3
| | | | | | | | We previously only did this if the old value was zero, to make sure numericality validations run and failed if the user gave 'wibble' as the value, which would be type cast to 0. However, numericality validations will fail if there are any non-numeric characters in the string, so 5 -> '5wibble' should also be marked as changed.
* Further simplify `changed?` conditional for numeric typesSean Griffin2014-06-191-12/+6
| | | | | | | `Type::Integer.new.type_cast('') # => nil`, we do not need a special case to handle this, `nil => ''` already returns false. The only case we need to handle is `0 => 'wibble'` should be changed, while `0 => '0'` should not.
* Do not type cast twice on attribute assignmentSean Griffin2014-06-071-3/+3
| | | | | | | | | | | | | The definition of `write_attribute` in dirty checking ultimately leads to the columns calling `type_cast` on the value to perform the comparison. However, this is a potentially expensive computation that we cache when it occurs in `read_attribute`. The only case that we need the non-type-cast form is for numeric, so we pass that through as well (something I'm looking to remove in the future). This also reduces the number of places that manually access various stages in an attribute's type casting lifecycle, which will aid in one of the larger refactorings that I'm working on.
* Don't mess with `_before_type_cast` for numeric typesSean Griffin2014-06-061-7/+8
|
* Refactor determination of whether the field has changedSean Griffin2014-06-031-0/+23
| | | | | The types know more about what is going on than the dirty module. Let's ask them!
* Move types to the top level `ActiveRecord` namespaceSean Griffin2014-05-271-0/+18
`ActiveRecord::ConnectionAdapters::Type::Value` => `ActiveRecord::Type::Value`