diff options
author | Wojciech Wnętrzak <w.wnetrzak@gmail.com> | 2015-01-04 09:18:03 +0100 |
---|---|---|
committer | Wojciech Wnętrzak <w.wnetrzak@gmail.com> | 2015-01-20 22:33:42 +0100 |
commit | cb74473db68900d336844d840dda6e10dc03fde1 (patch) | |
tree | bd1a32817e79f25cb7ced1506e302a0642fbb7fb /activemodel/test | |
parent | 08fe700e2fd57a63d1ee899b63e0e818bc0f4e69 (diff) | |
download | rails-cb74473db68900d336844d840dda6e10dc03fde1.tar.gz rails-cb74473db68900d336844d840dda6e10dc03fde1.tar.bz2 rails-cb74473db68900d336844d840dda6e10dc03fde1.zip |
Add ActiveModel::Errors#details
To be able to return type of validator, one can now call `details`
on Errors instance:
```ruby
class User < ActiveRecord::Base
validates :name, presence: true
end
```
```ruby
user = User.new; user.valid?; user.errors.details
=> {name: [{error: :blank}]}
```
Diffstat (limited to 'activemodel/test')
-rw-r--r-- | activemodel/test/cases/errors_test.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index efedd9055f..9cfba9812b 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -323,4 +323,46 @@ class ErrorsTest < ActiveModel::TestCase person.errors.expects(:generate_message).with(:name, :blank, { message: 'custom' }) person.errors.add_on_blank :name, message: 'custom' end + + test "details returns added error detail" do + person = Person.new + person.errors.add(:name, :invalid) + assert_equal({ name: [{ error: :invalid }] }, person.errors.details) + end + + test "details returns added error detail with custom option" do + person = Person.new + person.errors.add(:name, :greater_than, count: 5) + assert_equal({ name: [{ error: :greater_than, count: 5 }] }, person.errors.details) + end + + test "details do not include message option" do + person = Person.new + person.errors.add(:name, :invalid, message: "is bad") + assert_equal({ name: [{ error: :invalid }] }, person.errors.details) + end + + test "dup duplicates details" do + errors = ActiveModel::Errors.new(Person.new) + errors.add(:name, :invalid) + errors_dup = errors.dup + errors_dup.add(:name, :taken) + assert_not_same errors_dup.details, errors.details + end + + test "delete removes details on given attribute" do + errors = ActiveModel::Errors.new(Person.new) + errors.add(:name, :invalid) + errors.delete(:name) + assert_empty errors.details[:name] + end + + test "clear removes details" do + person = Person.new + person.errors.add(:name, :invalid) + + assert_equal 1, person.errors.details.count + person.errors.clear + assert person.errors.details.empty? + end end |