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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73354179ee7bcde3fc9788714870cd06981c32ec
4
- data.tar.gz: 21b27aa4a7b13a411c780fd2b7af3316686a5843
3
+ metadata.gz: 6d71b28eafed6a675b4b31f30bf4ef0a6146406c
4
+ data.tar.gz: 878becca5e2f6b8bbda9535439d9bfdc61774d73
5
5
  SHA512:
6
- metadata.gz: 0ff23f9ae379f31dc5728421dd8caa798067b95719cda49182f1fc3a261d184d137bc13873371eb6365f3b6af285c58a51464fa31fc8a922c96754ebdf4380bd
7
- data.tar.gz: 5014f82de304d637c761b7033f0d55b471743cba24e57807995193ed3a09c7040bf1346eb88d2ae40194bfd16ef6b64528cd8824bc30657338bb3875a62ee122
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 `errors.add` method.
30
+ You can add validator type to details hash when using `ActiveModel::Errors.add` method.
31
31
 
32
32
  ```ruby
33
- class Person < ActiveRecord::Base
34
- def a_method_used_for_validation_purposes
35
- errors.add(:name, :invalid_characters)
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
- person = Person.create(name: "!@#")
40
-
41
- person.errors.details[:name]
42
- # => [{error: :invalid_characters}]
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 not allowed characters set, you can
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 Person < ActiveRecord::Base
50
- def a_method_used_for_validation_purposes
51
- errors.add(:name, :invalid_characters, not_allowed: "!@#%*()_-+=")
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
- person = Person.create(name: "John!")
56
-
57
- person.errors.details[:name]
58
- # => [{error: :invalid_characters, not_allowed: "!@#%*()_-+="}]
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
- # alias_method :initialize_dup_without_details, :initialize_dup
14
- # alias_method :initialize_dup, :initialize_dup_with_details
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
- # def initialize_dup_with_details(other)
28
- # @details = other.details.dup
29
- # initialize_dup_without_details(other)
30
- # end
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
@@ -1,5 +1,5 @@
1
1
  module ActiveModel
2
2
  module ErrorsDetails
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
@@ -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
- # TODO: investigate why it doesn't work
67
- # def test_dup_duplicates_details
68
- # errors = ActiveModel::Errors.new(Person.new)
69
- # errors.add(:name, :invalid)
70
- # errors_dup = errors.dup
71
- # errors_dup.add(:name, :taken)
72
-
73
- # refute_equal errors_dup.details, errors.details
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.0.0
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-21 00:00:00.000000000 Z
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