Commit Graph

782 Commits

Author SHA1 Message Date
Ryuta Kamizono
c81af6ae72 Enable Layout/EmptyLinesAroundAccessModifier cop
We sometimes say "✂️ newline after `private`" in a code review (e.g.
https://github.com/rails/rails/pull/18546#discussion_r23188776,
https://github.com/rails/rails/pull/34832#discussion_r244847195).

Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style
`EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059).

That cop and enforced style will reduce the our code review cost.
2019-06-13 12:00:45 +09:00
yuuji.yaginuma
ac1ba44f8d Fix test case name to fit file name
This fixes the following warnings.

```
/rails/activemodel/test/cases/nested_error_test.rb:9: warning: method redefined; discarding old test_initialize
/rails/activemodel/test/cases/error_test.rb:29: warning: previous definition of test_initialize was here
```
2019-05-04 17:49:25 +09:00
Aaron Patterson
2ada222f1e
any? should be delegated to the errors list
Otherwise we get deprecation warnings in the generated scaffold template files
2019-04-30 16:54:40 -05:00
Rafael França
d4d145a679
Merge pull request #32313 from lulalala/model_error_as_object
Model error as object
2019-04-24 16:16:00 -04:00
Daniel Colson
e7a28c3990
Add attribute_names to ActiveModel::Attributes
This adds `.attribute_names` and `#attribute_names` to
`ActiveModel::Attributes` along the same lines as the corresponding
methods in `ActiveRecord::AttributeMethods` (see
[`.attribute_names`][class_method] and
[`#attribute_names`][instance_method].

While I was here I also added documentation for '#attributes', which I
added in 043ce35b186. The whole class is still `#:nodoc:` so I don't
think this will have any effect for now.

[class_method]: cc834db1d0/activerecord/lib/active_record/attribute_methods.rb (L154-L160)
[instance_method]: cc834db1d0/activerecord/lib/active_record/attribute_methods.rb (L299-L301)
2019-04-22 19:48:17 -04:00
Chris Salzberg
d73cac3ada
Ensure acceptance validator is not applied more than once to Person 2019-04-14 22:40:22 +09:00
Chris Salzberg
2e27801145
Ensure multiple anonymous modules are not included into Topic in tests
Each acceptance validator applied to a model class includes an instance
of a module builder (LazilyDefineAttributes) into that class. In tests,
if the original model class is not subclassed, these modules pile up and
cannot be removed, potentially leading to flakey specs and false
positive/negatives.

To avoid this, always use subclasses in tests whose names (constants)
can be removed when the test is done.
2019-04-14 17:21:49 +09:00
Chris Salzberg
63b5bdea51
Remove unused method_name from AttributeMethodMatch 2019-04-12 23:35:26 +09:00
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
lulalala
5e24c33350 Spec for display original raw type in details 2019-03-31 22:59:13 +08:00
lulalala
aaa0c32797 Set default array to details
maintaining behavior errors.details[:foo].any?
2019-03-31 22:59:13 +08:00
lulalala
f7f42a2d0e Fix messages[]= does not override value 2019-03-31 22:59:13 +08:00
lulalala
e7834214a6 Fix equality comparison raising error bug 2019-03-31 22:59:13 +08:00
lulalala
23dd7c0285 Assert deprecation 2019-03-31 22:59:12 +08:00
lulalala
abee034368 Raise deprecation for calling [:f] = 'b' or [:f] << 'b'
Revert some tests to ensure back compatibility
2019-03-31 22:59:12 +08:00
lulalala
67d262f70f Add deprecation to slice! 2019-03-31 22:59:12 +08:00
lulalala
86620cc3aa Allow errors to remove duplicates, and ensure cyclic associations w/ autosave duplicate errors can be removed
See SHA 7550f0a016ee6647aaa76c0c0ae30bebc3867288
2019-03-31 22:59:12 +08:00
lulalala
cccbac6df6 Add a transitional method objects, for accessing the array directly.
This is because we try to accommodate old hash behavior, so `first` and `last` now does not return Error object.
2019-03-31 22:59:12 +08:00
lulalala
655036b09a Fix spec as generate_message is no longer called during validation 2019-03-31 22:59:12 +08:00
lulalala
ea77205a9f Add convenience method group_by_attribute
Many operations need grouping of errors by attributes, e.g. ActiveRecord::AutosaveAssociation#association_valid?

Refactor other methods using group_by_attribute
2019-03-31 22:59:12 +08:00
lulalala
d9011e3935 Change errors
Allow `each` to behave in new way if block arity is 1

Ensure dumped marshal from Rails 5 can be loaded

Make errors compatible with marshal and YAML dumps from previous versions of Rails

Add deprecation warnings

Ensure each behave like the past, sorted by attribute
2019-03-31 22:59:12 +08:00
lulalala
ef68d3e35c Add ActiveModel::Error and NestedError
Add initialize_dup to deep dup.

Move proc eval and flexible message position out to Errors,
because proc eval is needed for Errors#added? and Errors#delete
2019-03-31 22:59:12 +08:00
lulalala
db0256cad7 Fix misalignment caused by SHA eebb9ddf9ba559a510975c486fe59a4edc9da97d 2019-03-31 22:59:12 +08: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 52fddcc653458456f98b3683dffd781cf00b35fe.

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