1. 21 1月, 2019 1 次提交
    • A
      Fix year value when casting a multiparameter time hash · ccdedeb9
      Andrew White 提交于
      When assigning a hash to a time attribute that's missing a year
      component (e.g. a `time_select` with `:ignore_date` set to `true`)
      then the year defaults to 1970 instead of the expected 2000. This
      results in the attribute changing as a result of the save.
      
      Before:
      
          event = Event.new(start_time: { 4 => 20, 5 => 30 })
          event.start_time # => 1970-01-01 20:30:00 UTC
          event.save
          event.reload
          event.start_time # => 2000-01-01 20:30:00 UTC
      
      After:
      
          event = Event.new(start_time: { 4 => 20, 5 => 30 })
          event.start_time # => 2000-01-01 20:30:00 UTC
          event.save
          event.reload
          event.start_time # => 2000-01-01 20:30:00 UTC
      ccdedeb9
  2. 12 11月, 2018 1 次提交
    • R
      Ensure casting by decimal attribute when querying · a741208f
      Ryuta Kamizono 提交于
      Related 34cc301f.
      
      `QueryAttribute#value_for_database` calls only `type.serialize`, and
      `Decimal#serialize` is a no-op unlike other attribute types.
      
      Whether or not `serialize` will invoke `cast` is undefined in our test
      cases, but it actually does not work properly unless it does so for now.
      a741208f
  3. 21 10月, 2018 1 次提交
  4. 29 9月, 2018 1 次提交
    • Y
      Add `Style/RedundantFreeze` to remove redudant `.freeze` · aa3dcabd
      Yasuo Honda 提交于
      Since Rails 6.0 will support Ruby 2.4.1 or higher
      `# frozen_string_literal: true` magic comment is enough to make string object frozen.
      This magic comment is enabled by `Style/FrozenStringLiteralComment` cop.
      
      * Exclude these files not to auto correct false positive `Regexp#freeze`
       - 'actionpack/lib/action_dispatch/journey/router/utils.rb'
       - 'activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb'
      
      It has been fixed by https://github.com/rubocop-hq/rubocop/pull/6333
      Once the newer version of RuboCop released and available at Code Climate these exclude entries should be removed.
      
      * Replace `String#freeze` with `String#-@` manually if explicit frozen string objects are required
      
       - 'actionpack/test/controller/test_case_test.rb'
       - 'activemodel/test/cases/type/string_test.rb'
       - 'activesupport/lib/active_support/core_ext/string/strip.rb'
       - 'activesupport/test/core_ext/string_ext_test.rb'
       - 'railties/test/generators/actions_test.rb'
      aa3dcabd
  5. 07 9月, 2018 1 次提交
    • Y
      Fix non_numeric_string? · ba406d9c
      Yoshiyuki Kinjo 提交于
      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
      ba406d9c
  6. 30 8月, 2018 1 次提交
    • S
      Faster time_value.rb · e81b0ddd
      schneems 提交于
      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
      ```
      e81b0ddd
  7. 29 5月, 2018 1 次提交
    • R
      Ensure casting by boolean attribute when querying · 34cc301f
      Ryuta Kamizono 提交于
      `QueryAttribute#value_for_database` calls only `type.serialize`, and
      `Boolean#serialize` is a no-op unlike other attribute types.
      
      It caused the issue #32624. Whether or not `serialize` will invoke
      `cast` is undefined in our test cases, but it actually does not work
      properly unless it does so for now.
      
      Fixes #32624.
      34cc301f
  8. 25 5月, 2018 1 次提交
  9. 17 5月, 2018 2 次提交
  10. 12 3月, 2018 2 次提交
    • A
      Normalize date component when writing to time columns · 3f95054f
      Andrew White 提交于
      For legacy reasons Rails stores time columns on sqlite as full
      timestamp strings. However because the date component wasn't being
      normalized this meant that when they were read back they were being
      prefixed with 2001-01-01 by ActiveModel::Type::Time. This had a
      twofold result - first it meant that the fast code path wasn't being
      used because the string was invalid and second it was corrupting the
      second fractional component being read by the Date._parse code path.
      
      Fix this by a combination of normalizing the timestamps on writing
      and also changing Active Model to be more lenient when detecting
      whether a string starts with a date component before creating the
      dummy time value for parsing.
      3f95054f
    • A
      Apply time column precision on assignment · 4d9126cf
      Andrew White 提交于
      In #20317, datetime columns had their precision applied on assignment but
      that behaviour wasn't applied to time columns - this commit fixes that.
      
      Fixes #30301.
      4d9126cf
  11. 02 3月, 2018 1 次提交
  12. 23 2月, 2018 1 次提交
  13. 18 2月, 2018 1 次提交
  14. 21 10月, 2017 1 次提交
  15. 08 8月, 2017 1 次提交
  16. 17 7月, 2017 2 次提交
    • S
      Allow multiparameter assigned attributes to be used with `text_field` · 1519e976
      Sean Griffin 提交于
      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.
      1519e976
    • K
      Use frozen string literal in activemodel/ · d7b1521d
      Kir Shatrov 提交于
      d7b1521d
  17. 06 7月, 2017 1 次提交
  18. 02 7月, 2017 1 次提交
  19. 01 7月, 2017 2 次提交
  20. 12 4月, 2017 1 次提交
    • M
      Don't freeze input strings · 8de7df5b
      Matthew Draper 提交于
      See 34321e4a for background on
      ImmutableString vs String.
      
      Our String type cannot delegate typecasting to ImmutableString, because
      the latter freezes its input: duplicating the value after that gives us
      an unfrozen result, but still mutates the originally passed object.
      8de7df5b
  21. 25 2月, 2017 2 次提交
  22. 15 2月, 2017 1 次提交
  23. 22 1月, 2017 1 次提交
    • K
      Make BigDecimal casting consistent on different platforms · 7ec30400
      Kir Shatrov 提交于
      Right now it behaves differently on JRuby:
      
      ```
      --- expected
      +++ actual
      @@ -1 +1 @@
      -#<BigDecimal:5f3c866c,'0.333333333333333333',18(20)>
      +#<BigDecimal:16e0afab,'0.3333333333333333',16(20)>
      ```
      
      My initial PR (https://github.com/rails/rails/pull/27324)
      offered to let the precision to be decided by the platform and
      change the test expection, but other contributors suggested
      that we should change the default precision in Rails
      to be consistent of all platforms.
      
      The value (18) comes from the max default precision that comes
      from casting Rational(1/3) to BigDecimal.
      7ec30400
  24. 01 1月, 2017 2 次提交
  25. 23 12月, 2016 1 次提交
  26. 17 12月, 2016 1 次提交
  27. 11 12月, 2016 1 次提交
    • R
      Use `inspect` in `type_cast_for_schema` for date/time and decimal values · b01740f9
      Ryuta Kamizono 提交于
      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
      ```
      b01740f9
  28. 27 11月, 2016 1 次提交
    • R
      Fix `apply_seconds_precision` not to be affected by `mathn` · 6700f857
      Ryuta Kamizono 提交于
      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
      ```
      6700f857
  29. 29 10月, 2016 1 次提交
  30. 15 10月, 2016 1 次提交
  31. 14 9月, 2016 1 次提交
  32. 06 9月, 2016 1 次提交
  33. 02 9月, 2016 2 次提交