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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c35e4e2ae8eef848df65fefdfcb904d9445d339c8fb6d60eb19d4160aefd701
4
- data.tar.gz: 1b93fb6bfd6263a46d0f6c80541c52f490f8725da258fa44b7829a2ee2819591
3
+ metadata.gz: 87170e6fd3bc27364650b6f827e45f66f9d0d3c5396510db4818bb617db82c55
4
+ data.tar.gz: fa46155a9ce06b76259446361a70c8551691307bc39f818ad45c8da8517cd9fb
5
5
  SHA512:
6
- metadata.gz: df19dc0542f489bbcba21ebb799aee4f4b2ed9a690b1b2890a4ba676717d0bf7e8b980583cf0d0a9306c92980973f227e8d2f696bf72f54fabd5e0f6454a4a40
7
- data.tar.gz: acae8c5ea6186c23053aa80e9ed3c34070f4ba4d253a37f97f4206de29816fb80b7668e30c19c7759ca9b3dba57c60000ba86ea0f864349ab4c9a5c1d3a82c19
6
+ metadata.gz: cb16d0e6e035dd9a178484caee3b2c4ad89f549190d97bc9df76288d1c47c2e8579c6dca8844f26286f08323c5d578d224bd809101b75996fc57e48a6a7d0dd2
7
+ data.tar.gz: 244ec7c85feca9165e8293a09742a95d3003cafa85551fcabab2167d032f7d9ef7eaa81236fa2a8738e29b877e0bf8efd8353881b1f9ba559d96e42f7bb94290
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014 thomas morgan
1
+ Copyright (c) 2014-2019 thomas morgan
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -11,40 +11,80 @@ message always ends with a period.
11
11
 
12
12
  ## Installation
13
13
 
14
- Add this line to your application's Gemfile:
14
+ In your Gemfile, add:
15
15
 
16
16
  gem 'error_merger'
17
17
 
18
- And then execute:
19
18
 
20
- $ bundle
21
-
22
- Or install it yourself as:
23
-
24
- $ gem install error_merger
19
+ ## Usage
25
20
 
21
+ #### Merging
26
22
 
27
- ## Usage
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
- @user.errors.full_messages # => ["Username can't be blank"]
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
- By default merged errors are prefixed with the model name. In the above example, an error on User might look like: "User: First name must not be blank".
75
+ ```
76
+ ## Rails default behavior:
77
+ @user.errors.full_messages
78
+ # => ["Name can't be blank"]
44
79
 
45
- The default behavior is the equivalent to: `@account.errors.merge @user, 'User: '`
80
+ ## Make errors nicer for display to users:
81
+ @user.errors.full_sentences
82
+ # => ["Name can't be blank."]
46
83
 
47
- To skip prefixing, pass an empty string: `''`.
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,11 @@
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
@@ -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.add_dependency 'activemodel', '>= 4', '< 6'
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'
@@ -1,2 +1,4 @@
1
+ require 'active_model'
2
+
1
3
  require 'error_merger/version'
2
4
  require 'error_merger/error_merger'
@@ -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
- def merge(association, prefix=nil)
7
- if association.errors.respond_to? :full_message
8
- prefix ||= "#{association.errors.instance_variable_get(:@base).class.model_name.human}: "
9
- association.errors.each do |attr, error|
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
+ 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
- association.errors.each do |error|
14
- add :base, error
15
- end
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.ends_with?('.') ? m : "#{m}."
34
+ m.end_with?('.') ? m : "#{m}."
26
35
  end
27
36
 
28
- def as_sentences
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.send :include, ErrorMerger
45
+ ActiveModel::Errors.include ErrorMerger
@@ -1,3 +1,3 @@
1
1
  module ErrorMerger
2
- VERSION = '0.3.3'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -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
@@ -0,0 +1,6 @@
1
+ class Author
2
+ include ActiveModel::Model
3
+
4
+ attr_accessor :name
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ class Book
2
+ include ActiveModel::Model
3
+
4
+ attr_accessor :author, :title
5
+
6
+ end
@@ -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.3.3
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: 2018-08-21 00:00:00.000000000 Z
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: '4'
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: '4'
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: '0'
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
- rubyforge_project:
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