error_merger 0.3.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/LICENSE.txt +1 -1
- data/README.md +61 -21
- data/Rakefile +40 -0
- data/error_merger.gemspec +4 -2
- data/gemfiles/Gemfile.rails52 +6 -0
- data/gemfiles/Gemfile.rails60 +6 -0
- data/gemfiles/Gemfile.rails61 +6 -0
- data/lib/error_merger/error_merger.rb +36 -14
- data/lib/error_merger/version.rb +1 -1
- data/lib/error_merger.rb +2 -0
- data/test/error_merger_test.rb +97 -0
- data/test/models/author.rb +6 -0
- data/test/models/book.rb +6 -0
- data/test/test_helper.rb +19 -0
- metadata +27 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 205eda2c943d4f26c76a28bf4f477616f35d5978b75692f51c0955cba7cd2ab0
|
4
|
+
data.tar.gz: 39420e066d2b4185673fc7f2e6af385fd88461ae9f53a25acb620fb16ec4a1fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ffdebf8c8531d2935490a955ccefd5fea34697bb6f3209bf5c07a18e44aa84a000c4d018157257d588c15adc1a61340c12eec5e80c2c3d849c7009618dc30505
|
7
|
+
data.tar.gz: b71f83a4f941216ee71b1f47059b6445494c726c005486f3a0553de897ed7848e8bd50af6f34df70a577c21f57078bab73acd9dcbc7873f7b9b4a5ac95e9c737
|
data/.gitignore
CHANGED
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
@@ -1 +1,41 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
Rake::TestTask.new(:test) do |t|
|
6
|
+
t.libs << 'test'
|
7
|
+
t.pattern = 'test/**/*_test.rb'
|
8
|
+
t.verbose = false
|
9
|
+
end
|
10
|
+
|
11
|
+
task default: :test
|
12
|
+
|
13
|
+
GEMFILES = {
|
14
|
+
'2.6@gem52' => 'Gemfile.rails52',
|
15
|
+
'2.6@gem60' => 'Gemfile.rails60',
|
16
|
+
'2.6@gem61' => 'Gemfile.rails61',
|
17
|
+
}
|
18
|
+
|
19
|
+
namespace :test do
|
20
|
+
task :install_all do
|
21
|
+
GEMFILES.each do |gemset, gemfile|
|
22
|
+
puts "\n\n========== Running 'bundle install' with #{gemfile} ==========\n"
|
23
|
+
puts `rvm #{gemset} --create do bundle install --gemfile=gemfiles/#{gemfile}`
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'test against all supported versions of rails'
|
28
|
+
task :all do
|
29
|
+
err = 0
|
30
|
+
cmd = ENV['RUN'] || 'rake test'
|
31
|
+
GEMFILES.each do |gemset, gemfile|
|
32
|
+
puts "\n\n========== Running '#{cmd}' with #{gemfile} ==========\n"
|
33
|
+
puts `rvm #{gemset} do bundle exec --gemfile=gemfiles/#{gemfile} #{cmd}`
|
34
|
+
err += 1 unless $?.success?
|
35
|
+
end
|
36
|
+
if err > 0
|
37
|
+
puts "\n>>> #{err} gemfiles failed"
|
38
|
+
exit err # exit code is # of failed runs
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/error_merger.gemspec
CHANGED
@@ -18,8 +18,10 @@ 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
22
|
|
23
|
-
spec.
|
23
|
+
spec.add_dependency 'activemodel', '>= 5.2', '< 7.1'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler'
|
24
26
|
spec.add_development_dependency 'rake'
|
25
27
|
end
|
@@ -2,33 +2,55 @@ 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
|
-
add :base, "#{prefix}#{association.errors.full_message(attr, error)}"
|
11
|
-
end
|
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
|
+
if assoc_or_errors.respond_to?(:errors) && assoc_or_errors.errors.is_a?(ActiveModel::Errors)
|
9
|
+
errors = assoc_or_errors.errors
|
12
10
|
else
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
errors = assoc_or_errors
|
12
|
+
end
|
13
|
+
|
14
|
+
if attribute == :base && prefix.nil? && errors.instance_variable_defined?(:@base)
|
15
|
+
prefix = "#{errors.instance_variable_get(:@base).class.model_name.human}: "
|
16
|
+
else
|
17
|
+
prefix ||= nil
|
18
|
+
end
|
19
|
+
prefix = "#{prefix} " if prefix =~ /[a-z0-9]$/i
|
20
|
+
|
21
|
+
if errors.respond_to? :full_messages
|
22
|
+
errors = errors.full_messages
|
23
|
+
elsif errors.is_a? Hash
|
24
|
+
errors = errors.values.flatten
|
25
|
+
end
|
26
|
+
|
27
|
+
errors.each do |error|
|
28
|
+
add attribute, "#{prefix}#{error}"
|
16
29
|
end
|
17
30
|
end
|
18
31
|
|
19
32
|
def full_sentences
|
20
|
-
|
33
|
+
if defined?(ActiveModel::Error)
|
34
|
+
map do |error|
|
35
|
+
m = error.full_message
|
36
|
+
m =~ %r{[.?!。]$} ? m : "#{m}."
|
37
|
+
end
|
38
|
+
else
|
39
|
+
map{ |attr, m| full_sentence(attr, m) }
|
40
|
+
end
|
21
41
|
end
|
22
42
|
|
23
43
|
def full_sentence(attribute, message)
|
24
44
|
m = full_message(attribute, message)
|
25
|
-
m
|
45
|
+
m =~ %r{[.?!。]$} ? m : "#{m}."
|
26
46
|
end
|
27
47
|
|
28
|
-
def
|
48
|
+
def join_sentences
|
29
49
|
full_sentences.join ' '
|
30
50
|
end
|
31
51
|
|
52
|
+
alias_method :as_sentences, :join_sentences
|
53
|
+
|
32
54
|
end
|
33
55
|
|
34
|
-
ActiveModel::Errors.
|
56
|
+
ActiveModel::Errors.include ErrorMerger
|
data/lib/error_merger/version.rb
CHANGED
data/lib/error_merger.rb
CHANGED
@@ -0,0 +1,97 @@
|
|
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
|
+
assert_equal "Name is invalid!", @author.errors.full_sentence(:name, "is invalid!")
|
88
|
+
assert_equal "Name is invalid?", @author.errors.full_sentence(:name, "is invalid?")
|
89
|
+
assert_equal "Name is invalid。", @author.errors.full_sentence(:name, "is invalid。")
|
90
|
+
|
91
|
+
@author.errors.add :name, :invalid
|
92
|
+
assert_equal ["Name can't be blank.", "Name is invalid."], @author.errors.full_sentences
|
93
|
+
assert_equal "Name can't be blank. Name is invalid.", @author.errors.join_sentences
|
94
|
+
assert_equal "Name can't be blank. Name is invalid.", @author.errors.as_sentences
|
95
|
+
end
|
96
|
+
|
97
|
+
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:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- thomas morgan
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-10-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -16,34 +16,34 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5.2'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '7.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.2'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '7.1'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: bundler
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- - "
|
37
|
+
- - ">="
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version: '
|
39
|
+
version: '0'
|
40
40
|
type: :development
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
|
-
- - "
|
44
|
+
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: '
|
46
|
+
version: '0'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rake
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -72,14 +72,21 @@ files:
|
|
72
72
|
- README.md
|
73
73
|
- Rakefile
|
74
74
|
- error_merger.gemspec
|
75
|
+
- gemfiles/Gemfile.rails52
|
76
|
+
- gemfiles/Gemfile.rails60
|
77
|
+
- gemfiles/Gemfile.rails61
|
75
78
|
- lib/error_merger.rb
|
76
79
|
- lib/error_merger/error_merger.rb
|
77
80
|
- lib/error_merger/version.rb
|
81
|
+
- test/error_merger_test.rb
|
82
|
+
- test/models/author.rb
|
83
|
+
- test/models/book.rb
|
84
|
+
- test/test_helper.rb
|
78
85
|
homepage: https://github.com/zarqman/error_merger
|
79
86
|
licenses:
|
80
87
|
- MIT
|
81
88
|
metadata: {}
|
82
|
-
post_install_message:
|
89
|
+
post_install_message:
|
83
90
|
rdoc_options: []
|
84
91
|
require_paths:
|
85
92
|
- lib
|
@@ -87,16 +94,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
94
|
requirements:
|
88
95
|
- - ">="
|
89
96
|
- !ruby/object:Gem::Version
|
90
|
-
version: '
|
97
|
+
version: '2.3'
|
91
98
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
99
|
requirements:
|
93
100
|
- - ">="
|
94
101
|
- !ruby/object:Gem::Version
|
95
102
|
version: '0'
|
96
103
|
requirements: []
|
97
|
-
|
98
|
-
|
99
|
-
signing_key:
|
104
|
+
rubygems_version: 3.2.22
|
105
|
+
signing_key:
|
100
106
|
specification_version: 4
|
101
107
|
summary: Enhances the Error class on ActiveModel-compliant models
|
102
|
-
test_files:
|
108
|
+
test_files:
|
109
|
+
- test/error_merger_test.rb
|
110
|
+
- test/models/author.rb
|
111
|
+
- test/models/book.rb
|
112
|
+
- test/test_helper.rb
|