error_merger 0.3.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +61 -21
- data/Rakefile +10 -0
- data/error_merger.gemspec +3 -1
- data/lib/error_merger.rb +2 -0
- data/lib/error_merger/error_merger.rb +24 -13
- data/lib/error_merger/version.rb +1 -1
- data/test/error_merger_test.rb +94 -0
- data/test/models/author.rb +6 -0
- data/test/models/book.rb +6 -0
- data/test/test_helper.rb +19 -0
- metadata +17 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87170e6fd3bc27364650b6f827e45f66f9d0d3c5396510db4818bb617db82c55
|
4
|
+
data.tar.gz: fa46155a9ce06b76259446361a70c8551691307bc39f818ad45c8da8517cd9fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb16d0e6e035dd9a178484caee3b2c4ad89f549190d97bc9df76288d1c47c2e8579c6dca8844f26286f08323c5d578d224bd809101b75996fc57e48a6a7d0dd2
|
7
|
+
data.tar.gz: 244ec7c85feca9165e8293a09742a95d3003cafa85551fcabab2167d032f7d9ef7eaa81236fa2a8738e29b877e0bf8efd8353881b1f9ba559d96e42f7bb94290
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -11,40 +11,80 @@ message always ends with a period.
|
|
11
11
|
|
12
12
|
## Installation
|
13
13
|
|
14
|
-
|
14
|
+
In your Gemfile, add:
|
15
15
|
|
16
16
|
gem 'error_merger'
|
17
17
|
|
18
|
-
And then execute:
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
Or install it yourself as:
|
23
|
-
|
24
|
-
$ gem install error_merger
|
19
|
+
## Usage
|
25
20
|
|
21
|
+
#### Merging
|
26
22
|
|
27
|
-
|
23
|
+
```
|
24
|
+
@user = User.new name: nil
|
25
|
+
@user.valid? # => false
|
26
|
+
@account = Account.new group: 'invalid'
|
27
|
+
@account.valid? # => false
|
28
|
+
|
29
|
+
## By default, will prefix with the merged model's name:
|
30
|
+
@account.errors.merge @user
|
31
|
+
@account.errors.full_messages # will include errors for both Account and User
|
32
|
+
# => ["Group is invalid", "User: Name can't be blank"]
|
33
|
+
|
34
|
+
## Disable the prefix:
|
35
|
+
@account.errors.merge @user, ''
|
36
|
+
# @account.errors.merge @user, false # equivalent to ''
|
37
|
+
@account.errors.full_messages
|
38
|
+
# => ["Group is invalid", "Name can't be blank"]
|
39
|
+
|
40
|
+
## Or change it:
|
41
|
+
@account.errors.merge @user, 'Member'
|
42
|
+
@account.errors.full_messages
|
43
|
+
# => ["Group is invalid", "Member Name can't be blank"]
|
44
|
+
|
45
|
+
## By default, merged errors are associated with the model, not an association
|
46
|
+
## (by adding the errors to :base). An attribute may be specified. Since the
|
47
|
+
## standard rendering of the error message will use the attribute name as part
|
48
|
+
## of the error message, auto-prefixing is disabled.
|
49
|
+
@account.errors.merge @user, attribute: :user
|
50
|
+
@account.errors.messages
|
51
|
+
# => {group: ["is invalid"], user: ["Name can't be blank"]}
|
52
|
+
@account.errors.full_messages
|
53
|
+
# => ["Group is invalid", "User Name can't be blank"]
|
54
|
+
|
55
|
+
## A prefix can still be added, however:
|
56
|
+
@account.errors.merge @user, 'Member', attribute: :user
|
57
|
+
@account.errors.full_messages
|
58
|
+
# => ["Group is invalid", "User Member Name can't be blank"]
|
59
|
+
```
|
28
60
|
|
61
|
+
_Hint: to merge the actual attributes directly, use the built-in `merge!` method instead._
|
62
|
+
```
|
63
|
+
## This may cause weird behavior if both models have the same attribute (eg: both
|
64
|
+
## have a :name attribute).
|
65
|
+
@account.errors.merge! @user.errors
|
66
|
+
@account.errors.messages
|
67
|
+
# => {group: ["is invalid"], name: ["can't be blank"]}
|
68
|
+
@account.errors.full_messages
|
69
|
+
# => ["Group is invalid", "Name can't be blank"]
|
29
70
|
```
|
30
|
-
@user = User.new user_params
|
31
|
-
@user.valid? # => false
|
32
|
-
@account = Account.new account_params
|
33
|
-
@account.valid? # => false
|
34
|
-
@account.errors.merge @user
|
35
71
|
|
36
|
-
@account.errors.full_messages # => will include errors for both Account and User
|
37
72
|
|
38
|
-
|
39
|
-
@user.errors.full_sentences # => ["Username can't be blank."]
|
40
|
-
@user.errors.as_sentences # => "First name can't be blank. Last name can't be blank."
|
41
|
-
```
|
73
|
+
### Sentences
|
42
74
|
|
43
|
-
|
75
|
+
```
|
76
|
+
## Rails default behavior:
|
77
|
+
@user.errors.full_messages
|
78
|
+
# => ["Name can't be blank"]
|
44
79
|
|
45
|
-
|
80
|
+
## Make errors nicer for display to users:
|
81
|
+
@user.errors.full_sentences
|
82
|
+
# => ["Name can't be blank."]
|
46
83
|
|
47
|
-
|
84
|
+
## Combine sentences into a single string:
|
85
|
+
@user.errors.join_sentences
|
86
|
+
# => "First name can't be blank. Last name can't be blank."
|
87
|
+
```
|
48
88
|
|
49
89
|
|
50
90
|
## Contributing
|
data/Rakefile
CHANGED
data/error_merger.gemspec
CHANGED
@@ -18,7 +18,9 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
|
21
|
-
spec.
|
21
|
+
spec.required_ruby_version = '>= 2.3'
|
22
|
+
|
23
|
+
spec.add_dependency 'activemodel', '>= 5', '< 6.1'
|
22
24
|
|
23
25
|
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
26
|
spec.add_development_dependency 'rake'
|
data/lib/error_merger.rb
CHANGED
@@ -2,17 +2,26 @@ module ErrorMerger
|
|
2
2
|
|
3
3
|
# merges an association's Errors set into the current set
|
4
4
|
# eg: @user.errors.merge @account
|
5
|
-
# @user.errors.merge @account, "Account ##{@account.id}:
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
5
|
+
# @user.errors.merge @account, "Account ##{@account.id}:"
|
6
|
+
# @user.errors.merge @account, attribute: :account
|
7
|
+
def merge(assoc_or_errors, prefix=nil, attribute: :base)
|
8
|
+
errors = assoc_or_errors.respond_to?(:errors) ? assoc_or_errors.errors : assoc_or_errors
|
9
|
+
|
10
|
+
if attribute == :base && prefix.nil? && errors.instance_variable_defined?(:@base)
|
11
|
+
prefix = "#{errors.instance_variable_get(:@base).class.model_name.human}: "
|
12
12
|
else
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
prefix ||= nil
|
14
|
+
end
|
15
|
+
prefix = "#{prefix} " if prefix =~ /[a-z0-9]$/i
|
16
|
+
|
17
|
+
if errors.respond_to? :full_messages
|
18
|
+
errors = errors.full_messages
|
19
|
+
elsif errors.is_a? Hash
|
20
|
+
errors = errors.values.flatten
|
21
|
+
end
|
22
|
+
|
23
|
+
errors.each do |error|
|
24
|
+
add attribute, "#{prefix}#{error}"
|
16
25
|
end
|
17
26
|
end
|
18
27
|
|
@@ -22,13 +31,15 @@ module ErrorMerger
|
|
22
31
|
|
23
32
|
def full_sentence(attribute, message)
|
24
33
|
m = full_message(attribute, message)
|
25
|
-
m.
|
34
|
+
m.end_with?('.') ? m : "#{m}."
|
26
35
|
end
|
27
36
|
|
28
|
-
def
|
37
|
+
def join_sentences
|
29
38
|
full_sentences.join ' '
|
30
39
|
end
|
31
40
|
|
41
|
+
alias_method :as_sentences, :join_sentences
|
42
|
+
|
32
43
|
end
|
33
44
|
|
34
|
-
ActiveModel::Errors.
|
45
|
+
ActiveModel::Errors.include ErrorMerger
|
data/lib/error_merger/version.rb
CHANGED
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ErrorMergerTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
setup do
|
6
|
+
@author = Author.new
|
7
|
+
@author.errors.add :name, :blank
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
test "merge Model" do
|
12
|
+
@book = Book.new
|
13
|
+
@book.errors.merge @author
|
14
|
+
assert_equal({base: ["Author: Name can't be blank"]}, @book.errors.messages)
|
15
|
+
assert_equal ["Author: Name can't be blank"], @book.errors.full_messages
|
16
|
+
|
17
|
+
@book = Book.new
|
18
|
+
@book.errors.merge @author, ''
|
19
|
+
assert_equal({base: ["Name can't be blank"]}, @book.errors.messages)
|
20
|
+
assert_equal ["Name can't be blank"], @book.errors.full_messages
|
21
|
+
|
22
|
+
@book = Book.new
|
23
|
+
@book.errors.merge @author, false
|
24
|
+
assert_equal({base: ["Name can't be blank"]}, @book.errors.messages)
|
25
|
+
assert_equal ["Name can't be blank"], @book.errors.full_messages
|
26
|
+
|
27
|
+
@book = Book.new
|
28
|
+
@book.errors.merge @author, attribute: :author
|
29
|
+
assert_equal({author: ["Name can't be blank"]}, @book.errors.messages)
|
30
|
+
assert_equal ["Author Name can't be blank"], @book.errors.full_messages
|
31
|
+
|
32
|
+
@book = Book.new
|
33
|
+
@book.errors.merge @author, '- ', attribute: :author
|
34
|
+
assert_equal({author: ["- Name can't be blank"]}, @book.errors.messages)
|
35
|
+
assert_equal ["Author - Name can't be blank"], @book.errors.full_messages
|
36
|
+
end
|
37
|
+
|
38
|
+
test "merge AM::Errors" do
|
39
|
+
@book = Book.new
|
40
|
+
@book.errors.merge @author.errors
|
41
|
+
assert_equal({base: ["Author: Name can't be blank"]}, @book.errors.messages)
|
42
|
+
|
43
|
+
@book = Book.new
|
44
|
+
@book.errors.merge @author.errors, ''
|
45
|
+
assert_equal({base: ["Name can't be blank"]}, @book.errors.messages)
|
46
|
+
end
|
47
|
+
|
48
|
+
test "merge Array" do
|
49
|
+
@error_array = ["Something is invalid"]
|
50
|
+
|
51
|
+
@book = Book.new
|
52
|
+
@book.errors.merge @error_array
|
53
|
+
assert_equal({base: ["Something is invalid"]}, @book.errors.messages)
|
54
|
+
|
55
|
+
@book = Book.new
|
56
|
+
@book.errors.merge @error_array, 'Prefix', attribute: :whatev
|
57
|
+
assert_equal({whatev: ["Prefix Something is invalid"]}, @book.errors.messages)
|
58
|
+
assert_equal ["Whatev Prefix Something is invalid"], @book.errors.full_messages
|
59
|
+
end
|
60
|
+
|
61
|
+
test "merge Hash" do
|
62
|
+
@error_hash = {something: "Something is invalid"}
|
63
|
+
@error_hash_2 = {something: ["Something is invalid"]}
|
64
|
+
|
65
|
+
@book = Book.new
|
66
|
+
@book.errors.merge @error_hash, ''
|
67
|
+
assert_equal({base: ["Something is invalid"]}, @book.errors.messages)
|
68
|
+
|
69
|
+
@book = Book.new
|
70
|
+
@book.errors.merge @error_hash_2, ''
|
71
|
+
assert_equal({base: ["Something is invalid"]}, @book.errors.messages)
|
72
|
+
end
|
73
|
+
|
74
|
+
test "merge Errors-like" do
|
75
|
+
e1 = ["Not valid"]
|
76
|
+
e1.instance_variable_set :@base, Author.new
|
77
|
+
|
78
|
+
@book = Book.new
|
79
|
+
@book.errors.merge e1
|
80
|
+
assert_equal({base: ["Author: Not valid"]}, @book.errors.messages)
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
test "sentences" do
|
85
|
+
assert_equal "Name is invalid.", @author.errors.full_sentence(:name, "is invalid")
|
86
|
+
assert_equal "Name is invalid.", @author.errors.full_sentence(:name, "is invalid.")
|
87
|
+
|
88
|
+
@author.errors.add :name, :invalid
|
89
|
+
assert_equal ["Name can't be blank.", "Name is invalid."], @author.errors.full_sentences
|
90
|
+
assert_equal "Name can't be blank. Name is invalid.", @author.errors.join_sentences
|
91
|
+
assert_equal "Name can't be blank. Name is invalid.", @author.errors.as_sentences
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
data/test/models/book.rb
ADDED
data/test/test_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'error_merger'
|
2
|
+
require 'active_support/test_case'
|
3
|
+
require 'active_support/testing/autorun'
|
4
|
+
|
5
|
+
# Filter out Minitest backtrace while allowing backtrace from other libraries
|
6
|
+
# to be shown.
|
7
|
+
Minitest.backtrace_filter = Minitest::BacktraceFilter.new
|
8
|
+
|
9
|
+
%w(author book).each do |f|
|
10
|
+
require "models/#{f}"
|
11
|
+
end
|
12
|
+
|
13
|
+
# Load fixtures from the engine
|
14
|
+
if ActiveSupport::TestCase.respond_to?(:fixture_path=)
|
15
|
+
ActiveSupport::TestCase.fixture_path = File.expand_path("fixtures", __dir__)
|
16
|
+
ActionDispatch::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path
|
17
|
+
ActiveSupport::TestCase.file_fixture_path = ActiveSupport::TestCase.fixture_path + "/files"
|
18
|
+
ActiveSupport::TestCase.fixtures :all
|
19
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: error_merger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- thomas morgan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -16,20 +16,20 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '6'
|
22
|
+
version: '6.1'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: '
|
29
|
+
version: '5'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '6'
|
32
|
+
version: '6.1'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: bundler
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -75,6 +75,10 @@ files:
|
|
75
75
|
- lib/error_merger.rb
|
76
76
|
- lib/error_merger/error_merger.rb
|
77
77
|
- lib/error_merger/version.rb
|
78
|
+
- test/error_merger_test.rb
|
79
|
+
- test/models/author.rb
|
80
|
+
- test/models/book.rb
|
81
|
+
- test/test_helper.rb
|
78
82
|
homepage: https://github.com/zarqman/error_merger
|
79
83
|
licenses:
|
80
84
|
- MIT
|
@@ -87,16 +91,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
91
|
requirements:
|
88
92
|
- - ">="
|
89
93
|
- !ruby/object:Gem::Version
|
90
|
-
version: '
|
94
|
+
version: '2.3'
|
91
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
96
|
requirements:
|
93
97
|
- - ">="
|
94
98
|
- !ruby/object:Gem::Version
|
95
99
|
version: '0'
|
96
100
|
requirements: []
|
97
|
-
|
98
|
-
rubygems_version: 2.7.6
|
101
|
+
rubygems_version: 3.0.2
|
99
102
|
signing_key:
|
100
103
|
specification_version: 4
|
101
104
|
summary: Enhances the Error class on ActiveModel-compliant models
|
102
|
-
test_files:
|
105
|
+
test_files:
|
106
|
+
- test/error_merger_test.rb
|
107
|
+
- test/models/author.rb
|
108
|
+
- test/models/book.rb
|
109
|
+
- test/test_helper.rb
|