aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/type/helpers
Commit message (Collapse)AuthorAgeFilesLines
* Fix non_numeric_string?Yoshiyuki Kinjo2018-09-071-1/+1
| | | | | | | | | | | | | | | | For example, dirty checking was not right for the following case: ``` model.int_column = "+5" model.float_column = "0.5E+1" model.decimal_column = "0.5e-3" ``` It is enough to see whether leading character is a digit for avoiding invalid numeric expression like 'wibble' to be type-casted to 0, as this method's comment says. Fixes #33801
* Faster time_value.rbschneems2018-08-291-1/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The multiplication of the value takes a long time when we can instead mutate and use the string value directly. The `microsec` perf increases speed by 27% in the ideal case (which is the most common). ``` original_string = ".443959" require 'benchmark/ips' Benchmark.ips do |x| x.report("multiply") { string = original_string.dup (string.to_r * 1_000_000).to_i } x.report("new ") { string = original_string.dup if string && string.start_with?(".".freeze) && string.length == 7 string[0] = ''.freeze string.to_i end } x.compare! end # Warming up -------------------------------------- # multiply 125.783k i/100ms # new 146.543k i/100ms # Calculating ------------------------------------- # multiply 1.751M (± 3.3%) i/s - 8.805M in 5.033779s # new 2.225M (± 2.1%) i/s - 11.137M in 5.007110s # Comparison: # new : 2225289.7 i/s # multiply: 1751254.2 i/s - 1.27x slower ```
* Add missing require for string to timezone conversionAnnie-Claude Côté2018-05-161-0/+1
| | | | Inside user_input_in_time_zone we call in_time_zone on the value and value can be a String.
* Allow multiparameter assigned attributes to be used with `text_field`Sean Griffin2017-07-171-0/+4
| | | | | | | | | | | | | | | | | | | | | Between 4.2 and 5.0 the behavior of how multiparameter attributes interact with `_before_type_cast` changed. In 4.2 it returns the post-type-cast value. After 5.0, it returns the hash that gets sent to the type. This behavior is correct, but will cause an issue if you then tried to render that value in an input like `text_field` or `hidden_field`. In this case, we want those fields to use the post-type-cast form, instead of the `_before_type_cast` (the main reason it uses `_before_type_cast` at all is to avoid losing data when casting a non-numeric string to integer). I've opted to modify `came_from_user?` rather than introduce a new method for this as I want to avoid complicating that contract further, and technically the multiparameter hash didn't come from assignment, it was constructed internally by AR. Close #27888.
* Use frozen string literal in activemodel/Kir Shatrov2017-07-164-0/+8
|
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-024-4/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Enforce frozen string in RubocopKir Shatrov2017-07-014-0/+4
|
* Merge pull request #25296 from kamipo/use_inspect_for_type_cast_for_schemaRafael França2017-02-281-1/+1
|\ | | | | Use `inspect` in `type_cast_for_schema` for date/time and decimal values
| * Use `inspect` in `type_cast_for_schema` for date/time and decimal valuesRyuta Kamizono2016-12-111-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently dumping defaults on schema is inconsistent. Before: ```ruby create_table "defaults", force: :cascade do |t| t.string "string_with_default", default: "Hello!" t.date "date_with_default", default: '2014-06-05' t.datetime "datetime_with_default", default: '2014-06-05 07:17:04' t.time "time_with_default", default: '2000-01-01 07:17:04' t.decimal "decimal_with_default", default: 1234567890 end ``` After: ```ruby create_table "defaults", force: :cascade do |t| t.string "string_with_default", default: "Hello!" t.date "date_with_default", default: "2014-06-05" t.datetime "datetime_with_default", default: "2014-06-05 07:17:04" t.time "time_with_default", default: "2000-01-01 07:17:04" t.decimal "decimal_with_default", default: "1234567890" end ```
* | Change ActiveModel::Type::Helpers to :nodoc: [ci skip]MSP-Greg2016-12-164-8/+8
|/
* Fix `apply_seconds_precision` not to be affected by `mathn`Ryuta Kamizono2016-11-271-1/+1
| | | | | | | | | | | | | | | | | | Currently `apply_seconds_precision` cannnot round usec when after `require 'mathn'`. ``` irb(main):001:0> 1234 / 1000 * 1000 => 1000 irb(main):002:0> 1234 - 1234 % 1000 => 1000 irb(main):003:0> require 'mathn' => true irb(main):004:0> 1234 / 1000 * 1000 => 1234 irb(main):005:0> 1234 - 1234 % 1000 => 1000 ```
* Add more rubocop rules about whitespacesRafael Mendonça França2016-10-291-1/+1
|
* Fix broken comments indentation caused by rubocop auto-correct [ci skip]Ryuta Kamizono2016-09-141-1/+1
| | | | | | All indentation was normalized by rubocop auto-correct at 80e66cc4d90bf8c15d1a5f6e3152e90147f00772. But comments was still kept absolute position. This commit aligns comments with method definitions for consistency.
* apply case-in-assignment patternXavier Noria2016-09-021-6/+7
|
* RuboCop is 100% green :tada:Xavier Noria2016-09-021-1/+1
|
* normalizes indentation and whitespace across the projectXavier Noria2016-08-062-26/+26
|
* applies new string literal convention in activemodel/libXavier Noria2016-08-061-1/+1
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Clean up the implementation of AR::DirtySean Griffin2015-09-241-0/+8
| | | | | | | | | | | | | This moves a bit more of the logic required for dirty checking into the attribute objects. I had hoped to remove the `with_value_from_database` stuff, but unfortunately just calling `dup` on the attribute objects isn't enough, since the values might contain deeply nested data structures. I think this can be cleaned up further. This makes most dirty checking become lazy, and reduces the number of object allocations and amount of CPU time when assigning a value. This opens the door (but doesn't quite finish) to improving the performance of writes to a place comparable to 4.1
* Fixed taking precision into count when assigning a value to timestamp attributeBogdan Gusiev2015-09-231-5/+8
| | | | | | | | | | | | | | | | | Timestamp column can have less precision than ruby timestamp In result in how big a fraction of a second can be stored in the database. m = Model.create! m.created_at.usec == m.reload.created_at.usec # => false # due to different seconds precision in Time.now and database column If the precision is low enough, (mysql default is 0, so it is always low enough by default) the value changes when model is reloaded from the database. This patch fixes that issue ensuring that any timestamp assigned as an attribute is converted to column precision under the attribute.
* Move the appropriate type tests to the Active Model suiteSean Griffin2015-09-211-2/+8
| | | | | | | | | Any tests for a type which is not overridden by Active Record, and does not test the specifics of the attributes API interacting in more complex ways have no reason to be in the Active Record suite. Doing this revealed that the implementation of the date and time types in AM was actually completely broken, and incapable of returning any value other than `nil`.
* Various stylistic nitpicksSean Griffin2015-09-211-4/+3
| | | | | | | We do not need to require each file from AM individually, the type module does that for us. Even if the classes are extremely small right now, I'd rather keep any custom classes needed by AR in their own files, as they can easily have more complex changes in the future.
* Move ActiveRecord::Type to ActiveModelKir Shatrov2015-09-214-0/+148
The first step of bringing typecasting to ActiveModel