Commit Graph

735 Commits

Author SHA1 Message Date
Ryuta Kamizono 50fba828d5 Refactor `has_secure_password` to extract dedicated attribute module
Follow up of #26764 and #35700.

And add test case for #35700.
2019-04-05 01:55:00 +09:00
Fumiaki MATSUSHIMA 61c4be4777 Output junit format test report 2019-04-04 14:34:46 +09:00
Ryuta Kamizono 406d3a926c
Merge pull request #35794 from kamipo/type_cast_symbol_false
Type cast falsy boolean symbols on boolean attribute as false
2019-03-30 05:07:07 +09:00
Ryuta Kamizono 2d12f800f1 Type cast falsy boolean symbols on boolean attribute as false
Before 34cc301, type casting by boolean attribute when querying is a
no-op, so finding by truthy boolean string (i.e.
`where(value: "true") # => value = 'true'`) didn't work as expected
(matches it to FALSE in MySQL #32624). By type casting is ensured, a
value on boolean attribute is always serialized to TRUE or FALSE.

In PostgreSQL, `where(value: :false) # => value = 'false'` was a valid
SQL, so 34cc301 is a regresson for PostgreSQL since all symbol values
are serialized as TRUE.

I'd say using `:false` is mostly a developer's mistake (user's input
basically comes as a string), but `:false` on boolean attribute is
serialized as TRUE is not a desirable behavior for anybody.

This allows falsy boolean symbols as false, i.e.
`klass.create(value: :false).value? # => false` and
`where(value: :false) # => value = FALSE`.

Fixes #35676.
2019-03-30 04:18:25 +09:00
Abhay Nikam 9841f6897b Fixed the test description for i18n-customize-full-message after rename in #35789 2019-03-30 00:00:45 +05:30
Prathamesh Sonpatki d8ba2f7c56
Rename `i18n_full_message` config option to `i18n_customize_full_message`
- I feel `i18n_customize_full_messages` explains the meaning of the
  config better.
- Followup of https://github.com/rails/rails/pull/32956
2019-03-29 21:38:48 +05:30
Samantha John 755112c7b1 Replace “can not” with “cannot”. 2019-03-06 16:35:52 -05:00
alkesh26 38941df2aa activemodel typo fix. 2019-02-25 17:16:10 +05:30
Ryuta Kamizono f8a798c8e6
Merge pull request #35336 from kamipo/dont_allow_non_numeric_string_matches_to_zero
Don't allow `where` with non numeric string matches to 0 values
2019-02-21 18:58:44 +09:00
Ryuta Kamizono 9c9c950d02 Revert "Speed up integer casting from DB"
This reverts commit 52fddcc653.

52fddcc was to short-circuit `ensure_in_range` in `cast_value`. But that
caused a regression for empty string deserialization.

Since 7c6f393, `ensure_in_range` is moved into `serialize`. As 52fddcc
said, the absolute gain is quite small. So I've reverted that commit to
fix the regression.
2019-02-21 13:11:42 +09:00
Ryuta Kamizono 357cd23d3a Don't allow `where` with non numeric string matches to 0 values
This is a follow-up of #35310.

Currently `Topic.find_by(id: "not-a-number")` matches to a `id = 0`
record. That is considered as silently leaking information.

If non numeric string is given to find by an integer column, it should
not be matched to any record.

Related #12793.
2019-02-20 22:00:56 +09:00
Ryuta Kamizono 4ea067017a Merge pull request #29651 from Sayanc93/return-correct-date
Return correct date in ActiveModel for time to date conversions
2019-02-18 16:04:47 +09:00
Ryuta Kamizono 907280ddfd Fix type cast with values hash for Date type
`value_from_multiparameter_assignment` defined by
`AcceptsMultiparameterTime` helper requires `default_timezone` method
which is defined at `TimeValue` helper.
Since `Date` type doesn't include `TimeValue`, I've extracted `Timezone`
helper to be shared by `Date`, `DateTime`, and `Time` types.
2019-02-18 04:20:52 +09:00
Ryuta Kamizono 25b3cbb241 Add edge test cases for integer and string types 2019-02-17 21:44:15 +09:00
alkesh26 8b66ea5d2c activemodel typo fixes. 2019-01-31 02:43:44 +05:30
Edouard CHIN f01e38509c Fix NumericalityValidator on object responding to `to_f`:
- If you had a PORO that acted like a Numeric, the validator would
  work correctly because it was previously using `Kernel.Float`
  which is implicitely calling `to_f` on the passed argument.

  Since rails/rails@d126c0d , we are now using `BigDecimal` which does
  not implicitely call `to_f` on the argument, making the validator
  fail with an underlying `TypeError` exception.

  This patch replate the `is_decimal?` check with `Kernel.Float`.
  Using `Kernel.Float` as argument for the BigDecimal call has two
  advantages:

  1. It calls `to_f` implicetely for us.
  2. It's also smart enough to detect that `Kernel.Float("a")` isn't a
     Numeric and will raise an error.
     We don't need the `is_decimal?` check thanks to that.

  Passing `Float::DIG` as second argument to `BigDecimal` is mandatory
  because the precision can't be omitted when passing a Float.
  `Float::DIG` is what is used internally by ruby when calling
  `123.to_d`

  https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/lib/bigdecimal/util.rb#L47

- Another small issue introduced in https://github.com/rails/rails/pull/34693
  would now raise a TypeError because `Regexp#===` will just return
  false if the passed argument isn't a string or symbol, whereas
  `Regexp#match?` will.
2019-01-22 20:46:16 +01:00
Andrew White ccdedeb9d5
Fix year value when casting a multiparameter time hash
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
2019-01-21 09:23:12 +00:00
bogdanvlviv d5c26c43c0
Add `ActiveModel::Errors#of_kind?`
Related to https://github.com/rails/rails/pull/34817#issuecomment-451508668
2019-01-04 20:47:31 +02:00
Ryuta Kamizono 892e38c78e Enable `Style/RedundantBegin` cop to avoid newly adding redundant begin block
Currently we sometimes find a redundant begin block in code review
(e.g. https://github.com/rails/rails/pull/33604#discussion_r209784205).

I'd like to enable `Style/RedundantBegin` cop to avoid that, since
rescue/else/ensure are allowed inside do/end blocks in Ruby 2.5
(https://bugs.ruby-lang.org/issues/12906), so we'd probably meets with
that situation than before.
2018-12-21 06:12:42 +09:00
Ryuta Kamizono 8034dde023 Module#{define_method,alias_method,undef_method,remove_method} become public since Ruby 2.5
https://bugs.ruby-lang.org/issues/14133
2018-12-21 01:39:18 +09:00
Gannon McGibbon d126c0d6c0 Fix numericality equality validation on floats 2018-12-12 12:02:12 -05:00
Daniel Lopez Prat ee2b84f3cb
Add slice! method to ActiveModel::Errors 2018-11-21 08:56:19 +09:00
Ronan Limon Duparcmeur 13b77fa1cb Fix ignored options in the `#added?` method
Fixes #34416
2018-11-13 09:00:20 +01:00
wilddima 26cdd01eab Add new exception message to datetime from hash cast 2018-10-21 10:05:48 +02:00
Sharang Dashputre 3c4b729f48 Fix spellings for 'unmarshall(ing/ed)' & 'marshall(ing/ed)' 2018-10-02 13:55:39 +05:30
Yasuo Honda aa3dcabd87 Add `Style/RedundantFreeze` to remove redudant `.freeze`
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'
2018-09-29 07:18:44 +00:00
Rafael França 6556898884
Merge pull request #30676 from artofhuman/import-assert-attrs-error-message
Improve error message when assign wrong attributes to model
2018-09-26 14:10:24 -04:00
Rafael Mendonça França f679933daa
Change the empty block style to have space inside of the block 2018-09-25 13:19:35 -04:00
Kasper Timm Hansen 22dc2b3db8
Merge pull request #33949 from sjain1107/no-private-def
Remove private def
2018-09-23 19:39:15 +02:00
Sakshi Jain 0fe2bb816f Remove private def 2018-09-23 21:27:44 +05:30
yuuji.yaginuma 1b86d90136 Enable `Performance/UnfreezeString` cop
In Ruby 2.3 or later, `String#+@` is available and `+@` is faster than `dup`.

```ruby
# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "benchmark-ips"
end

Benchmark.ips do |x|
  x.report('+@') { +"" }
  x.report('dup') { "".dup }
  x.compare!
end
```

```
$ ruby -v benchmark.rb
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
Warming up --------------------------------------
                  +@   282.289k i/100ms
                 dup   187.638k i/100ms
Calculating -------------------------------------
                  +@      6.775M (± 3.6%) i/s -     33.875M in   5.006253s
                 dup      3.320M (± 2.2%) i/s -     16.700M in   5.032125s

Comparison:
                  +@:  6775299.3 i/s
                 dup:  3320400.7 i/s - 2.04x  slower

```
2018-09-23 08:56:55 +09:00
Ryuta Kamizono c3e569550c
Merge pull request #33804 from yskkin/num_string
Fix non_numeric_string?
2018-09-08 05:56:22 +09:00
Rafael França dd29fabebf
Merge pull request #33615 from Larochelle/i18n_full_message_with_nested_attributes
`ActiveModel.full_message` interaction with `index_errors`
2018-09-07 13:24:09 -04:00
Yoshiyuki Kinjo ba406d9c22 Fix non_numeric_string?
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
2018-09-07 16:17:15 +09:00
Ryuta Kamizono 47a6d788dd Fix numericality validator to still use value before type cast except Active Record
The purpose of fe9547b is to work type casting to value from database.

But that was caused not to use the value before type cast even except
Active Record.

There we never guarantees that the value before type cast was going to
the used in this validation, but we should not change the behavior
unless there is some particular reason.

To restore original behavior, still use the value before type cast if
`came_from_user?` is undefined (i.e. except Active Record).

Fixes #33651.
Fixes #33686.
2018-08-24 00:44:02 +09:00
Martin Larochelle 8d2f3179e6 Call human_attribute_name with a string instead of a symbole 2018-08-16 11:50:13 -04:00
Martin Larochelle 0b54641878 `ActiveModel.full_message` interaction with `index_errors` 2018-08-14 12:01:23 -04:00
bogdanvlviv 3a0a8cf604
Fix test failure
```
...
(snip)
............F
Failure:
JsonSerializationTest#test_as_json_should_return_a_hash_if_include_root_
in_json_is_true [/home/travis/build/rails/rails/activemodel/test/cases/serializers/json_serialization_test.rb:145]:
Expected: 2006-08-01 00:00:00 UTC
  Actual: "2006-08-01T00:00:00.000Z"
rails test home/travis/build/rails/rails/activemodel/test/cases/serializers/json_serialization_test.rb:136
(snip)
...
```

Related to #31503
2018-08-11 19:19:04 +03:00
Eileen M. Uchitelle 09e1452eeb
Merge pull request #31503 from bogdan/timestamp-as-json
Fix AM::Serializers::JSON#as_json method for timestamps
2018-08-11 10:37:49 -04:00
Dillon Welch d108288c2f
Turn on performance based cops
Use attr_reader/attr_writer instead of methods

method is 12% slower

Use flat_map over map.flatten(1)

flatten is 66% slower

Use hash[]= instead of hash.merge! with single arguments

merge! is 166% slower

See https://github.com/rails/rails/pull/32337 for more conversation
2018-07-23 15:37:06 -07:00
Jeremy Baker a19918124d Ensure attribute is a symbol in the added? method 2018-07-14 13:19:46 -05:00
bogdanvlviv e62e68e25b
has_secure_password: use `recovery_password` instead of `activation_token`
Since we have `has_secure_token`, it is too confusing to use `_token`
suffix with `has_secure_password`.
Context https://github.com/rails/rails/pull/33307#discussion_r200807185
2018-07-08 14:12:27 +03:00
bogdanvlviv 382b5ca7dd
Improve `SecurePasswordTest#test_authenticate`
- Ensure that execution of `authenticate`/`authenticate_XXX` returns
`self` if password is correct, otherwise `false` (as mentioned in the documentation).
- Test `authenticate_password`.
2018-07-06 20:21:58 +03:00
Rafael Mendonça França 08dde0f355
Merge pull request #26764 from choncou/improve_has_secure_password
Allow configurable attribute name on `#has_secure_password`
2018-06-28 13:01:21 +02:00
Rafael França d3f659e526
Merge pull request #32956 from Shopify/i18n_activemodel_errors_full_message
Allow to override the full_message error format
2018-06-11 10:10:23 -04:00
Martin Larochelle 32513c4b35 Add global config for config.active_model.i18n_full_message 2018-06-05 13:25:24 -04:00
Martin Larochelle 7d09874a71 Allow to override the full_message error format 2018-05-22 16:31:30 -04:00
Annie-Claude Côté 6ff593ef87 Fix user_input_in_time_zone to coerce non valid string into nil
Before it was coercing an invalid string into "2000-01-01 00:00:00".
2018-05-16 17:01:07 -04:00
Ryuta Kamizono 1dc17e7b2e Fix `CustomCops/AssertNot` to allow it to have failure message
Follow up of #32605.
2018-05-13 11:32:47 +09:00
Semyon Pupkov 2253f6cca1 Improve error message when assign wrong attributes to model 2018-04-28 16:27:16 +05:00