error_merger 0.3.0 → 1.0.1

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
- SHA1:
3
- metadata.gz: 961e1f0d07b29195b46f05be818bee2acef8455f
4
- data.tar.gz: 255b7bc2dbb513c3f1fecaaa5adfb8a180fae6d5
2
+ SHA256:
3
+ metadata.gz: 04c86c83cf6f379820aa9192b42c5a957851a5ca7e115e98be6c958c53d02a37
4
+ data.tar.gz: 0c332e185ae4dfbf4a987785faa15c8c413abdbff132076d622b4f9009b0b5cb
5
5
  SHA512:
6
- metadata.gz: 942ac7d8dbd548107cafcacf51cfc7d52333df35845b1ab0910e1c89ec7c43722aff4ba64dfeb94ca0429d47114f24713b441ec4efd168c15f23c15f06384b4a
7
- data.tar.gz: ae862af43baff6cba5619d1f3cd8d303469951d48979410bac190d92b85d157d9489e101ce4fc7252272a5cff02a3b15686ca73cc70bd5eb13eebd355576a857
6
+ metadata.gz: 8e773ba97e0b1f58f8583b1a27a21173b49e392c6c201526df2886ccf10b94fefdbefedad72634f5f8d591d6ab67ef1b949dd2e2e336f9719643cbaf93072997
7
+ data.tar.gz: d16c73f04d5753ce608c5114b0eb978df96b279ccecabc1c415269ebc53707a81dba3704a9405543837f80c142a486e2751b2c49bd525cac26ec9aefb864574c
data/.gitignore CHANGED
@@ -4,6 +4,7 @@
4
4
  .config
5
5
  .yardoc
6
6
  Gemfile.lock
7
+ Gemfile.*.lock
7
8
  InstalledFiles
8
9
  _yardoc
9
10
  coverage
@@ -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,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
@@ -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.0'
21
+ spec.required_ruby_version = '>= 2.3'
22
+
23
+ spec.add_dependency 'activemodel', '>= 5.2', '< 6.2'
22
24
 
23
25
  spec.add_development_dependency 'bundler', '~> 1.3'
24
26
  spec.add_development_dependency 'rake'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activemodel', '~> 5.2.0'
4
+
5
+ # Specify your gem's dependencies in error_merger.gemspec
6
+ gemspec path: '..'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activemodel', '~> 6.0.0'
4
+
5
+ # Specify your gem's dependencies in error_merger.gemspec
6
+ gemspec path: '..'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activemodel', '~> 6.1.0'
4
+
5
+ # Specify your gem's dependencies in error_merger.gemspec
6
+ gemspec path: '..'
@@ -1,2 +1,4 @@
1
+ require 'active_model'
2
+
1
3
  require 'error_merger/version'
2
4
  require 'error_merger/error_merger'
@@ -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
- def merge(association, prefix=nil)
7
- if association.errors.respond_to? :full_message
8
- prefix ||= "#{association.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
+ 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
- association.errors.each do |error|
14
- add :base, error
15
- end
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
- map{ |attr, m| full_sentence(attr, m) }
33
+ if defined?(ActiveModel::Error)
34
+ map do |error|
35
+ m = error.full_message
36
+ m.end_with?('.') ? 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.ends_with?('.') ? m : "#{m}."
45
+ m.end_with?('.') ? m : "#{m}."
26
46
  end
27
47
 
28
- def as_sentences
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.send :include, ErrorMerger
56
+ ActiveModel::Errors.include ErrorMerger
@@ -1,3 +1,3 @@
1
1
  module ErrorMerger
2
- VERSION = '0.3.0'
2
+ VERSION = '1.0.1'
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,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: error_merger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.0.1
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: 2015-01-24 00:00:00.000000000 Z
11
+ date: 2020-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '4.0'
19
+ version: '5.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6.2'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '5.2'
30
+ - - "<"
25
31
  - !ruby/object:Gem::Version
26
- version: '4.0'
32
+ version: '6.2'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: bundler
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -66,14 +72,21 @@ files:
66
72
  - README.md
67
73
  - Rakefile
68
74
  - error_merger.gemspec
75
+ - gemfiles/Gemfile.rails52
76
+ - gemfiles/Gemfile.rails60
77
+ - gemfiles/Gemfile.rails61
69
78
  - lib/error_merger.rb
70
79
  - lib/error_merger/error_merger.rb
71
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
72
85
  homepage: https://github.com/zarqman/error_merger
73
86
  licenses:
74
87
  - MIT
75
88
  metadata: {}
76
- post_install_message:
89
+ post_install_message:
77
90
  rdoc_options: []
78
91
  require_paths:
79
92
  - lib
@@ -81,16 +94,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
94
  requirements:
82
95
  - - ">="
83
96
  - !ruby/object:Gem::Version
84
- version: '0'
97
+ version: '2.3'
85
98
  required_rubygems_version: !ruby/object:Gem::Requirement
86
99
  requirements:
87
100
  - - ">="
88
101
  - !ruby/object:Gem::Version
89
102
  version: '0'
90
103
  requirements: []
91
- rubyforge_project:
92
- rubygems_version: 2.4.3
93
- signing_key:
104
+ rubygems_version: 3.0.9
105
+ signing_key:
94
106
  specification_version: 4
95
107
  summary: Enhances the Error class on ActiveModel-compliant models
96
- 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