aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/errors_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* Revert "No such class since 8d2866bb80fbe81acb04f5b0c44f152f571fb29f"Rafael Mendonça França2019-08-021-1/+1
| | | | This reverts commit dd779c9686f49f5ed6dda8ad5a1cb3b0788e1dd4.
* No such class since 8d2866bb80fbe81acb04f5b0c44f152f571fb29fAkira Matsuda2019-08-021-1/+1
|
* Modify behaviour of `AM::Errors#to_h`:Edouard CHIN2019-07-231-2/+3
| | | | | | | | | | | | | | | | | | | | | - `AM::Error#to_h` was kind of broken before and would return in the hash values a single error message. ```ruby person = Person.new person.errors.add(:name, "cannot be blank") person.errors.add(:name, "too long") puts person.errors.to_h # {name: 'too long'} ``` Since an attribute can have different errors, the previous behavior didn't make much sense. Now, `ActiveModel::Errors#to_hash` correctly returns an array of error messages containing all the errors for an attribute. However, one can easily be surprised by this change, so let's deprecated it first.
* Deprecated `AM::Errors#to_h`:Edouard CHIN2019-07-231-0/+10
| | | | | | | | | | | | | | | | - In ef4d3215b1198c456780b8d18aa62be7795b9b8c I made a change to pass `AM::Error` object in case the arity of the block passed to `each` accepted less than 2 arguments. This is causing one issue for `to_h` as it expects the argument passed to the block to be an Array (and were are passing it an instance of `AM::Error`). There is no real reason to use `to_h` anymore since `to_hash` exists Deprecating `to_h` inf favor of `to_hash` Co-Authored-By: Rafael França <rafael@franca.dev>
* Fix deprecation on `AM::Errors` when each is called indirectly:Edouard CHIN2019-07-201-0/+8
| | | | | | | | | | | | - `AM::Errors#each` is implemented for the `Enumerator` module and get called indirectly by a bunch of method in the ruby land (map, first, select ...) These methods have a `-1` arity as they are written in C and they wrongly trigger a deprecation warning. This commit fixes that and correctectly return a `AM::Error` object when `each` is called with a negative arity.
* Move the `ActiveModel:Errors#full_message` method to the `Error` class:Edouard CHIN2019-07-161-0/+21
| | | | | | | | | | | | | | | | - 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.
* Merge pull request #36654 from Edouard-chin/ec-errors-deleteRafael França2019-07-151-0/+6
|\ | | | | Returns `nil` when `AM::Errors#delete` doesn't delete anything:
| * Returns `nil` when `AM::Errors#delete` doesn't delete anything:Edouard CHIN2019-07-111-0/+6
| | | | | | | | | | | | | | | | | | - `AM::Errors#delete` currently returns an empty array when trying to delete an error that doesn't exist. This behaviour is surprising and I think it would be better to no return a truthy value but instead return nil like `Hash#delete` does.
* | Fix `AM::Errors.added?` trying to generate a message:Edouard CHIN2019-07-101-0/+22
|/ | | | | | | | | | | | | | | | | | | | | | | | - 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]}` https://github.com/rails/rails/blob/2fe20cb55c76e6e50ec3a4ec5b03bbb65adba290/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.
* any? should be delegated to the errors listAaron Patterson2019-04-301-0/+11
| | | | Otherwise we get deprecation warnings in the generated scaffold template files
* Spec for display original raw type in detailslulalala2019-03-311-0/+18
|
* Set default array to detailslulalala2019-03-311-0/+6
| | | | maintaining behavior errors.details[:foo].any?
* Fix messages[]= does not override valuelulalala2019-03-311-0/+8
|
* Raise deprecation for calling `[:f] = 'b'` or `[:f] << 'b'`lulalala2019-03-311-6/+54
| | | | Revert some tests to ensure back compatibility
* Add deprecation to slice!lulalala2019-03-311-5/+3
|
* Add a transitional method `objects`, for accessing the array directly.lulalala2019-03-311-6/+6
| | | | This is because we try to accommodate old hash behavior, so `first` and `last` now does not return Error object.
* Add convenience method group_by_attributelulalala2019-03-311-0/+8
| | | | | | Many operations need grouping of errors by attributes, e.g. ActiveRecord::AutosaveAssociation#association_valid? Refactor other methods using group_by_attribute
* Change errorslulalala2019-03-311-49/+173
| | | | | | | | | | | | 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
* Add `ActiveModel::Errors#of_kind?`bogdanvlviv2019-01-041-1/+79
| | | | Related to https://github.com/rails/rails/pull/34817#issuecomment-451508668
* Add slice! method to ActiveModel::ErrorsDaniel Lopez Prat2018-11-211-0/+24
|
* Fix ignored options in the `#added?` methodRonan Limon Duparcmeur2018-11-131-0/+10
| | | | Fixes #34416
* Ensure attribute is a symbol in the added? methodJeremy Baker2018-07-141-0/+6
|
* Replace `assert !` with `assert_not`Daniel Colson2018-04-191-4/+4
| | | | | This autocorrects the violations after adding a custom cop in 3305c78dcd.
* Remove usage of strip_heredoc in the framework in favor of <<~Rafael Mendonça França2018-02-161-2/+1
| | | | | Some places we can't remove because Ruby still don't have a method equivalent to strip_heredoc to be called in an already existent string.
* Use assert_empty and assert_not_emptyDaniel Colson2018-01-251-3/+3
|
* Use assert_predicate and assert_not_predicateDaniel Colson2018-01-251-4/+4
|
* Use respond_to test helpersDaniel Colson2018-01-251-1/+1
|
* fix bug on added? methodAlessandro Rodi2017-11-131-0/+7
| | | | fix rubocop issues
* Use frozen string literal in activemodel/Kir Shatrov2017-07-161-0/+2
|
* Add ActiveModel::Errors#merge!Jahfer Husain2017-07-071-0/+12
| | | | | | | | | | | | | | | | | ActiveModel::Errors#merge! allows ActiveModel::Errors to append errors from a separate ActiveModel::Errors instance onto their own. Example: person = Person.new person.errors.add(:name, :blank) errors = ActiveModel::Errors.new(Person.new) errors.add(:name, :invalid) person.errors.merge!(errors) puts person.errors.messages # => { name: ["can't be blank", "is invalid"] }
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
|
* Fix ActiveModel::Errors #keys, #valuesbogdanvlviv2017-03-281-0/+16
| | | | | | | | | | | | | | | | | | Before: person.errors.keys # => [] person.errors.values # => [] person.errors[:name] # => [] person.errors.keys # => [:name] person.errors.values # => [[]] After: person.errors.keys # => [] person.errors.values # => [] person.errors[:name] # => [] person.errors.keys # => [] person.errors.values # => [] Related to #23468
* Missing require for strip_heredocAkira Matsuda2017-01-171-0/+1
|
* Missing require "yaml"Akira Matsuda2016-12-121-0/+1
|
* Make ActiveModel::Errors backward compatible with 4.2Rafael Mendonça França2016-12-081-0/+20
| | | | | | | If a Error object was serialized in the database as YAML in the Rails 4.2 version, if we load in the Rails 5.0 version it will miss the @details instance variable so methods like #clear and #add will start to fail.
* Allow indifferent access in ActiveModel::ErrorsKenichi Kamiya2016-11-221-3/+8
| | | | | | `#[]` has already applied indifferent access, but some methods does not. `#include?`, `#has_key?`, `#key?`, `#delete` and `#full_messages_for`.
* Removed deprecated methods in ActiveModel::ErrorsRafael Mendonça França2016-10-101-92/+0
| | | | `#get`, `#set`, `[]=`, `add_on_empty` and `add_on_blank`.
* Do not leak the Errors default proc when calling to_hash or as_jsonJean Boussier2016-09-271-0/+10
|
* improve error message when include assertions failMichael Grosser2016-09-161-2/+2
| | | | | | assert [1, 3].includes?(2) fails with unhelpful "Asserting failed" message assert_includes [1, 3], 2 fails with "Expected [1, 3] to include 2" which makes it easier to debug and more obvious what went wrong
* applies new string literal convention in activemodel/testXavier Noria2016-08-061-17/+17
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Add test to better demonstrate `ActiveModel::Errors#added?` behaviorJon Moss2016-06-211-0/+6
| | | | Fixes #25410.
* Ensure that instances of `ActiveModel::Errors` can be marshalledSean Griffin2016-05-301-0/+9
| | | | | | | | | | | | | | | | We now use default procs inside of the errors object, which gets included by default when marshaling anything that includes `ActiveModel::Validations`. This means that Active Record objects cannot be marshalled. We strip and apply the default proc ourselves. This will ensure the objects are YAML serializable as well, since YAML falls back to marshal implementations now. This is less important, however, as the errors aren't included when dumping Active Record objects. This commit does not include a changelog entry, as 5.0 is still in RC status at the time of writing, and 5.0.0 will not release with the bug this fixes. Fixes #25165
* Followup of #18322Vipul A M2016-05-021-1/+1
| | | | Mirror the documented new behavior of including details, when performing errors test.
* Do not create a hash key when calling ActiveModel::Errors#include?Esteban Pastorino2016-03-241-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | From: https://github.com/rails/rails/issues/24279 Problem: By doing `record.errors.include? :foo`, it adds a new key to the @messages hash that defaults to an empty array. This happens because of a combination of these 2 commits: https://github.com/rails/rails/commit/b97035df64f5b2f912425c4a7fcb6e6bb3ddab8d (Added in Rails 4.1) and https://github.com/rails/rails/commit/6ec8ba16d85d5feaccb993c9756c1edcbbf0ba13#diff-fdcf8b65b5fb954372c6fe1ddf284c78R76 (Rails 5.0) By adding the default proc that returns an array for non-existing keys, ruby adds that key to the hash. Solution: Change `#include?` to check with `has_key?` and then check if that value is `present?`. Add test case for ActiveModels::Errors#include?
* `ActiveRecord::Base#becomes` should copy the errorsVokhmin Alexey V2015-12-141-0/+10
|
* Use private method call assertions in Active Model tests.Kasper Timm Hansen2015-07-101-24/+9
| | | | Also fix Minitest constant reference.
* Remove use of mocha from Active ModelRoque Pinel2015-05-281-23/+45
|
* Adds/Corrects use case for adding an error messageZamith2015-05-041-0/+6
| | | | | I believe this is a use case that was supposed to be supported, and it's a small fix.
* Merge pull request #19021 from morgoth/activemodel-errors-refactoringRafael Mendonça França2015-03-301-0/+6
|\ | | | | Simplify and alias ActiveModel::Errors methods where possible