active_model-errors_details 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -22
- data/active_model-errors_details.gemspec +1 -0
- data/lib/active_model/errors_details.rb +7 -6
- data/lib/active_model/errors_details/version.rb +1 -1
- data/test/test_errors_details.rb +8 -9
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6d71b28eafed6a675b4b31f30bf4ef0a6146406c
|
4
|
+
data.tar.gz: 878becca5e2f6b8bbda9535439d9bfdc61774d73
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d11968f0d4f0b9a51a8732fa21fc776b29b5d902af22c6605c9224a46c61f253497941e41db708c2fc762327be733210559454e3ad93bfe85a16d8a2dd1115be
|
7
|
+
data.tar.gz: 0a34703bcf3283612a64d1cbe3dcacfa257ce6fd1830441325593f46b4a4a406f7429c2f783c146f4faf984cb0216b64732b2feec87ad15274d687e9ffbaf699
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# ActiveModel::Errors#details
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/cowbell/active_model-errors_details.svg?branch=master)](https://travis-ci.org/cowbell/active_model-errors_details)
|
4
|
+
|
3
5
|
Feature backported from Rails 5.0 to use with Rails 4.x apps.
|
4
6
|
|
5
7
|
Background: https://github.com/rails/rails/pull/18322
|
@@ -12,9 +14,7 @@ gem install "active_model-errors_details"
|
|
12
14
|
|
13
15
|
## Usage
|
14
16
|
|
15
|
-
To check what validator type was used on invalid attribute, you can use
|
16
|
-
`errors.details[:attribute]`. It returns array of hashes where under `:error`
|
17
|
-
key you will find symbol of used validator.
|
17
|
+
To check what validator type was used on invalid attribute, you can use `errors.details[:attribute]`. It returns array of hashes where under `:error` key you will find symbol of used validator.
|
18
18
|
|
19
19
|
```ruby
|
20
20
|
class Person < ActiveRecord::Base
|
@@ -27,38 +27,39 @@ person.errors.details[:name]
|
|
27
27
|
# => [{error: :blank}]
|
28
28
|
```
|
29
29
|
|
30
|
-
You can add validator type to details hash when using `
|
30
|
+
You can add validator type to details hash when using `ActiveModel::Errors.add` method.
|
31
31
|
|
32
32
|
```ruby
|
33
|
-
class
|
34
|
-
|
35
|
-
|
33
|
+
class User < ActiveRecord::Base
|
34
|
+
validate :adulthood
|
35
|
+
|
36
|
+
def adulthood
|
37
|
+
errors.add(:age, :too_young) if age < 18
|
36
38
|
end
|
37
39
|
end
|
38
40
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
# => [{error: :
|
41
|
+
user = User.new(age: 15)
|
42
|
+
user.valid?
|
43
|
+
user.errors.details
|
44
|
+
# => {age: [{error: :too_young}]}
|
43
45
|
```
|
44
46
|
|
45
|
-
To improve error details to contain
|
46
|
-
pass additional options to `errors.add` method.
|
47
|
+
To improve error details to contain additional options, you can pass them to `ActiveModel::Errors.add` method.
|
47
48
|
|
48
49
|
```ruby
|
49
|
-
class
|
50
|
-
|
51
|
-
|
50
|
+
class User < ActiveRecord::Base
|
51
|
+
validate :adulthood
|
52
|
+
|
53
|
+
def adulthood
|
54
|
+
errors.add(:age, :too_young, years_limit: 18) if age < 18
|
52
55
|
end
|
53
56
|
end
|
54
57
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
# => [{error: :
|
58
|
+
user = User.new(age: 15)
|
59
|
+
user.valid?
|
60
|
+
user.errors.details
|
61
|
+
# => {age: [{error: :too_young, years_limit: 18}]}
|
59
62
|
```
|
60
63
|
|
61
64
|
All built in Rails validators populate details hash with corresponding
|
62
65
|
validator types.
|
63
|
-
|
64
|
-
[![Build Status](https://travis-ci.org/cowbell/active_model-email_confirmation.svg)](https://travis-ci.org/cowbell/active_model-email_confirmation)
|
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_dependency "activemodel", ">= 4.0", "< 5.0.0.alpha"
|
22
|
+
spec.add_dependency "activesupport"
|
22
23
|
|
23
24
|
spec.add_development_dependency "bundler", "~> 1.7"
|
24
25
|
spec.add_development_dependency "rake", "~> 10.0"
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require "active_model/errors_details/version"
|
2
2
|
require "active_model/errors"
|
3
|
+
require "active_support/core_ext/object/deep_dup"
|
3
4
|
|
4
5
|
module ActiveModel
|
5
6
|
module ErrorsDetails
|
@@ -10,8 +11,8 @@ module ActiveModel
|
|
10
11
|
alias_method :initialize_without_details, :initialize
|
11
12
|
alias_method :initialize, :initialize_with_details
|
12
13
|
|
13
|
-
|
14
|
-
|
14
|
+
alias_method :initialize_dup_without_details, :initialize_dup
|
15
|
+
alias_method :initialize_dup, :initialize_dup_with_details
|
15
16
|
|
16
17
|
alias_method :delete_without_details, :delete
|
17
18
|
alias_method :delete, :delete_with_details
|
@@ -24,10 +25,10 @@ module ActiveModel
|
|
24
25
|
end
|
25
26
|
end
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
def initialize_dup_with_details(other)
|
29
|
+
@details = other.details.deep_dup
|
30
|
+
initialize_dup_without_details(other)
|
31
|
+
end
|
31
32
|
|
32
33
|
def details
|
33
34
|
@details
|
data/test/test_errors_details.rb
CHANGED
@@ -63,15 +63,14 @@ class TestErrorsDetails < MiniTest::Unit::TestCase
|
|
63
63
|
assert_equal({name: [{error: :invalid}] }, person.errors.details)
|
64
64
|
end
|
65
65
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
# end
|
66
|
+
def test_dup_duplicates_details
|
67
|
+
errors = ActiveModel::Errors.new(Person.new)
|
68
|
+
errors.add(:name, :invalid)
|
69
|
+
errors_dup = errors.dup
|
70
|
+
errors_dup.add(:name, :taken)
|
71
|
+
|
72
|
+
refute_equal errors_dup.details, errors.details
|
73
|
+
end
|
75
74
|
|
76
75
|
def test_delete_removes_details_on_given_attribute
|
77
76
|
errors = ActiveModel::Errors.new(Person.new)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_model-errors_details
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wojciech Wnętrzak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -30,6 +30,20 @@ dependencies:
|
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: 5.0.0.alpha
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: activesupport
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
33
47
|
- !ruby/object:Gem::Dependency
|
34
48
|
name: bundler
|
35
49
|
requirement: !ruby/object:Gem::Requirement
|