1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
Commit graph

20 commits

Author SHA1 Message Date
Filipe Sabella
bc7b730891 Pass in base to Error.human_attribute_names
There are validation cases in which the human_attribute_name depends on
other fields of the base class.

For instance, an Address model that depends on the selected country to
localize the attribute name to be shown in error messages. E.g. the
:address1 and :address2 attributes can be displayed as very different
strings depending on whether the address is in the US or in Japan.
2020-10-22 16:50:00 -03:00
Akira Matsuda
badcaf6763 AR::Base#read_attribute_for_validation is a public_method 2020-09-16 12:15:23 +09:00
lulalala
1fee2cbc50 Rename Error#detail method as details
Plural is more expected.
2020-09-11 17:03:59 +08:00
lulalala
f9518dc972 Document model error methods
[ci skip]
2020-06-29 17:50:08 +08:00
Petrik
74cb9a6f38 Make inspect look more like regular Object#inspect
Move the # outside the < > just like regular Object#inspect
2020-05-29 21:53:35 +02:00
lulalala
fcd1e41e82 Document on ActiveModel::Errors changes
Mark private constants

Display alternative for deprecation removal warning

Annotate Error's attributes

More emphasis on adding an error instead of message

Rewrite scaffold template using new errors API

Set first and last with behavior change deprecation

Update more doc and example

Add inspect for easier debugging
2020-01-14 23:55:09 +08:00
Jonathan Hefner
cd619e9781 Fix i18n of attributes with multi-digit indexes
Follow-up to #33615 and #37447.
2019-10-17 10:23:00 -04:00
jonathankwok
236e20e6fb Don't add new lookup; replace existing one instead 2019-10-15 15:18:07 -04:00
jonathankwok
5c9b008f36 Support ActiveModel::Error translation lookup on indexed attributes.
Instances of ActiveModel::Error where `attribute` is a nested attribute
can use translation keys that don't include the index in their lookup key.
2019-10-11 17:15:02 -04:00
Akira Matsuda
4645f2d34f Passing in a Hash instance as kwargs parameters requires the "double splat" prefix 2019-09-04 12:45:27 +09:00
Edouard CHIN
d204a09df2 Switch to use class_attribute:
- Since `ActiveModel::Error` can now be inherited by
  `ActiveModel::NestedError`, when the latter generates a
  `full_message`, the `i18n_customize_full_message` accessor set in
  the parent class is not set.

  This commit fixes that by using a `class_attribute` instead.
2019-07-16 14:28:38 +02:00
Edouard CHIN
b677adede0 Move the ActiveModel:Errors#full_message method to the Error class:
- One regression introduced by the "AM errors as object" features is
  about the `full_messages` method.

  It's currently impossible to call that method if the `base` object
  passed in the constructor of `AM::Errors` doesn't respond to the
  `errors` method.
  That's because `full_messages` now makes a weird back and forth trip

  `AM::Errors#full_messages` -> `AM::Error#full_message` -> `AM::Errors#full_message`

  Since `full_message` (singular) isn't needed by AM::Errors, I moved
  it to the `AM::Error` (singular) class. This way we don't need to
  grab the `AM::Errors` object from the base.
2019-07-16 14:28:38 +02:00
Rafael França
bec7be8800
Merge pull request #36649 from Edouard-chin/ec-errors-strit-match-collection
Fix `AM::Errors.added?` trying to generate a message:
2019-07-15 20:36:40 +01:00
Edouard CHIN
06756290d5 Fix AM::Errors.added? trying to generate a message:
- When a ActiveRecord record get saved and validated as part of a
  collection association, the errors attribute are changed to reflect
  the children names. You end up with an error attribute that will
  look like this:

  `author.errors # {:'books.title' => [:blank]}`

  2fe20cb55c/activerecord/lib/active_record/autosave_association.rb (L331-L340)

  We then can't check if the `books.title` errors was added using
  `ActiveModel::Errors#added?` because it tries to generate a message
  to make the match and end up calling the "books.title" method
  on the Author.

  ```
  author.errors.added?(:'books.title', :blank) => NoMethodError: undefined method `books.title'
  ```

  This patch modify the behaviour of `strict_match?` to not generate
  a message to make the comparison but instead make a strict
  comparison with the `options` from the error.
2019-07-10 23:33:32 +02:00
Edouard CHIN
07ff343857 Fix errors getting duplicated when passed validations options:
- In 86620cc3aa, a change was made
  on how we remove error duplication on a record for autosave
  association

  This fix has one caveat where validation having a `if` / `unless`
  options passed as a proc would be considered different.
  Example:

  ```ruby
  class Book < ApplicationRecord
    has_one :author

    validates :title, presence: true, if -> { true }
    validates :title, presence: true, if -> { true }
  end

  Book.new.valid? # false
  Book.errors.full_messages # ["title can't be blank", "title can't be blank"]
  ```

  While this example might sound strange, I think it's better to
  ignore `AM::Validations` options (if, unless ...) when making the
  comparison.
2019-07-10 18:40:12 +02:00
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
lulalala
e7834214a6 Fix equality comparison raising error bug 2019-03-31 22:59:13 +08:00
lulalala
86620cc3aa Allow errors to remove duplicates, and ensure cyclic associations w/ autosave duplicate errors can be removed
See SHA 7550f0a016
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