active_model-better_errors 1.6.3 → 1.6.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +37 -0
- data/.rspec +4 -1
- data/.ruby-gemset +1 -0
- data/.travis.yml +5 -1
- data/Gemfile +9 -13
- data/Gemfile.devtools +66 -0
- data/Guardfile +32 -0
- data/LICENSE.txt +2 -0
- data/README.md +12 -10
- data/Rakefile +20 -49
- data/active_model-better_errors.gemspec +27 -107
- data/config/devtools.yml +4 -0
- data/config/flay.yml +3 -0
- data/config/flog.yml +2 -0
- data/config/mutant.yml +3 -0
- data/config/reek.yml +103 -0
- data/config/rubocop.yml +62 -0
- data/config/yardstick.yml +2 -0
- data/lib/active_model/better_errors.rb +64 -4
- data/lib/active_model/better_errors/array_reporter.rb +14 -0
- data/lib/active_model/{error_collecting → better_errors}/emulation.rb +15 -8
- data/lib/active_model/{error_collecting → better_errors}/error_collection.rb +14 -6
- data/lib/active_model/{error_collecting → better_errors}/error_message.rb +16 -5
- data/lib/active_model/{error_collecting → better_errors}/error_message_set.rb +7 -2
- data/lib/active_model/{error_collecting → better_errors}/errors.rb +10 -3
- data/lib/active_model/better_errors/formatter.rb +26 -0
- data/lib/active_model/better_errors/hash_reporter.rb +14 -0
- data/lib/active_model/{error_collecting → better_errors}/human_array_reporter.rb +6 -1
- data/lib/active_model/{error_collecting → better_errors}/human_hash_reporter.rb +8 -3
- data/lib/active_model/better_errors/human_message_formatter.rb +66 -0
- data/lib/active_model/{error_collecting → better_errors}/human_message_reporter.rb +19 -12
- data/lib/active_model/{error_collecting → better_errors}/machine_array_reporter.rb +10 -2
- data/lib/active_model/{error_collecting → better_errors}/machine_hash_reporter.rb +10 -3
- data/lib/active_model/{error_collecting → better_errors}/message_reporter.rb +10 -5
- data/lib/active_model/{error_collecting → better_errors}/reporter.rb +7 -2
- data/lib/active_model/better_errors/version.rb +10 -0
- data/spec/spec_helper.rb +19 -10
- data/spec/support/i18n_deprecation_silence.rb +4 -0
- data/spec/support/models.rb +11 -4
- data/spec/support/string_ext.rb +14 -0
- data/spec/{lib/active_model/error_collecting → unit/lib/active_model/better_errors}/emulation_spec.rb +8 -6
- data/spec/{lib/active_model/error_collecting → unit/lib/active_model/better_errors}/error_collection_spec.rb +66 -62
- data/spec/{lib/active_model/error_collecting → unit/lib/active_model/better_errors}/error_message_set_spec.rb +27 -25
- data/spec/unit/lib/active_model/better_errors/error_message_spec.rb +315 -0
- data/spec/unit/lib/active_model/better_errors/errors_spec.rb +98 -0
- data/spec/unit/lib/active_model/better_errors/human_array_reporter_spec.rb +39 -0
- data/spec/unit/lib/active_model/better_errors/human_hash_reporter_spec.rb +37 -0
- data/spec/{lib/active_model/error_collecting → unit/lib/active_model/better_errors}/human_message_formatter_spec.rb +13 -7
- data/spec/unit/lib/active_model/better_errors/human_message_reporter_spec.rb +65 -0
- data/spec/unit/lib/active_model/better_errors/machine_array_reporter_spec.rb +45 -0
- data/spec/unit/lib/active_model/better_errors/machine_hash_reporter_spec.rb +45 -0
- data/spec/unit/lib/active_model/better_errors_spec.rb +37 -0
- metadata +98 -143
- data/.document +0 -5
- data/VERSION +0 -1
- data/lib/active_model/error_collecting.rb +0 -49
- data/lib/active_model/error_collecting/array_reporter.rb +0 -9
- data/lib/active_model/error_collecting/core_ext.rb +0 -6
- data/lib/active_model/error_collecting/hash_reporter.rb +0 -9
- data/lib/active_model/error_collecting/human_message_formatter.rb +0 -58
- data/spec/lib/active_model/better_errors_spec.rb +0 -7
- data/spec/lib/active_model/error_collecting/error_message_spec.rb +0 -293
- data/spec/lib/active_model/error_collecting/errors_spec.rb +0 -95
- data/spec/lib/active_model/error_collecting/human_array_reporter_spec.rb +0 -33
- data/spec/lib/active_model/error_collecting/human_hash_reporter_spec.rb +0 -32
- data/spec/lib/active_model/error_collecting/human_message_reporter_spec.rb +0 -61
- data/spec/lib/active_model/error_collecting/machine_array_reporter_spec.rb +0 -40
- data/spec/lib/active_model/error_collecting/machine_hash_reporter_spec.rb +0 -40
- data/spec/lib/active_model/error_collecting_spec.rb +0 -22
- data/test/integration.rb +0 -10
data/config/devtools.yml
ADDED
data/config/flay.yml
ADDED
data/config/flog.yml
ADDED
data/config/mutant.yml
ADDED
data/config/reek.yml
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
---
|
2
|
+
Attribute:
|
3
|
+
enabled: true
|
4
|
+
exclude: []
|
5
|
+
BooleanParameter:
|
6
|
+
enabled: true
|
7
|
+
exclude: []
|
8
|
+
ClassVariable:
|
9
|
+
enabled: true
|
10
|
+
exclude: []
|
11
|
+
ControlParameter:
|
12
|
+
enabled: true
|
13
|
+
exclude: []
|
14
|
+
DataClump:
|
15
|
+
enabled: true
|
16
|
+
exclude: []
|
17
|
+
max_copies: 2
|
18
|
+
min_clump_size: 2
|
19
|
+
DuplicateMethodCall:
|
20
|
+
enabled: true
|
21
|
+
exclude: []
|
22
|
+
max_calls: 1
|
23
|
+
allow_calls: []
|
24
|
+
FeatureEnvy:
|
25
|
+
enabled: true
|
26
|
+
exclude: []
|
27
|
+
IrresponsibleModule:
|
28
|
+
enabled: true
|
29
|
+
exclude: []
|
30
|
+
LongParameterList:
|
31
|
+
enabled: true
|
32
|
+
exclude: []
|
33
|
+
max_params: 2
|
34
|
+
overrides:
|
35
|
+
initialize:
|
36
|
+
max_params: 3
|
37
|
+
LongYieldList:
|
38
|
+
enabled: true
|
39
|
+
exclude: []
|
40
|
+
max_params: 2
|
41
|
+
NestedIterators:
|
42
|
+
enabled: true
|
43
|
+
exclude: []
|
44
|
+
max_allowed_nesting: 1
|
45
|
+
ignore_iterators: []
|
46
|
+
NilCheck:
|
47
|
+
enabled: true
|
48
|
+
exclude: []
|
49
|
+
RepeatedConditional:
|
50
|
+
enabled: true
|
51
|
+
exclude: []
|
52
|
+
max_ifs: 1
|
53
|
+
TooManyInstanceVariables:
|
54
|
+
enabled: true
|
55
|
+
exclude: []
|
56
|
+
max_instance_variables: 3
|
57
|
+
TooManyMethods:
|
58
|
+
enabled: true
|
59
|
+
exclude: []
|
60
|
+
max_methods: 10
|
61
|
+
TooManyStatements:
|
62
|
+
enabled: true
|
63
|
+
exclude:
|
64
|
+
- each
|
65
|
+
max_statements: 2
|
66
|
+
UncommunicativeMethodName:
|
67
|
+
enabled: true
|
68
|
+
exclude: []
|
69
|
+
reject:
|
70
|
+
- !ruby/regexp /^[a-z]$/
|
71
|
+
- !ruby/regexp /[0-9]$/
|
72
|
+
- !ruby/regexp /[A-Z]/
|
73
|
+
accept: []
|
74
|
+
UncommunicativeModuleName:
|
75
|
+
enabled: true
|
76
|
+
exclude: []
|
77
|
+
reject:
|
78
|
+
- !ruby/regexp /^.$/
|
79
|
+
- !ruby/regexp /[0-9]$/
|
80
|
+
accept: []
|
81
|
+
UncommunicativeParameterName:
|
82
|
+
enabled: true
|
83
|
+
exclude: []
|
84
|
+
reject:
|
85
|
+
- !ruby/regexp /^.$/
|
86
|
+
- !ruby/regexp /[0-9]$/
|
87
|
+
- !ruby/regexp /[A-Z]/
|
88
|
+
accept: []
|
89
|
+
UncommunicativeVariableName:
|
90
|
+
enabled: true
|
91
|
+
exclude: []
|
92
|
+
reject:
|
93
|
+
- !ruby/regexp /^.$/
|
94
|
+
- !ruby/regexp /[0-9]$/
|
95
|
+
- !ruby/regexp /[A-Z]/
|
96
|
+
accept: []
|
97
|
+
UnusedParameters:
|
98
|
+
enabled: true
|
99
|
+
exclude: []
|
100
|
+
UtilityFunction:
|
101
|
+
enabled: true
|
102
|
+
exclude: []
|
103
|
+
max_helper_calls: 0
|
data/config/rubocop.yml
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
AllCops:
|
2
|
+
Includes:
|
3
|
+
- '**/*.rake'
|
4
|
+
- 'Gemfile'
|
5
|
+
- 'Gemfile.devtools'
|
6
|
+
Excludes:
|
7
|
+
- '**/vendor/**'
|
8
|
+
- '**/benchmarks/**'
|
9
|
+
|
10
|
+
# Avoid parameter lists longer than five parameters.
|
11
|
+
ParameterLists:
|
12
|
+
Max: 5
|
13
|
+
CountKeywordArgs: true
|
14
|
+
|
15
|
+
# Avoid more than `Max` levels of nesting.
|
16
|
+
BlockNesting:
|
17
|
+
Max: 3
|
18
|
+
|
19
|
+
# Align with the style guide.
|
20
|
+
CollectionMethods:
|
21
|
+
PreferredMethods:
|
22
|
+
collect: 'map'
|
23
|
+
inject: 'reduce'
|
24
|
+
find: 'detect'
|
25
|
+
find_all: 'select'
|
26
|
+
|
27
|
+
# Do not force public/protected/private keyword to be indented at the same
|
28
|
+
# level as the def keyword. My personal preference is to outdent these keywords
|
29
|
+
# because I think when scanning code it makes it easier to identify the
|
30
|
+
# sections of code and visually separate them. When the keyword is at the same
|
31
|
+
# level I think it sort of blends in with the def keywords and makes it harder
|
32
|
+
# to scan the code and see where the sections are.
|
33
|
+
AccessModifierIndentation:
|
34
|
+
Enabled: false
|
35
|
+
|
36
|
+
# Limit line length
|
37
|
+
LineLength:
|
38
|
+
Max: 79
|
39
|
+
|
40
|
+
# Disable documentation checking until a class needs to be documented once
|
41
|
+
Documentation:
|
42
|
+
Enabled: false
|
43
|
+
|
44
|
+
# Do not favor modifier if/unless usage when you have a single-line body
|
45
|
+
IfUnlessModifier:
|
46
|
+
Enabled: false
|
47
|
+
|
48
|
+
# Allow case equality operator (in limited use within the specs)
|
49
|
+
CaseEquality:
|
50
|
+
Enabled: false
|
51
|
+
|
52
|
+
# Constants do not always have to use SCREAMING_SNAKE_CASE
|
53
|
+
ConstantName:
|
54
|
+
Enabled: false
|
55
|
+
|
56
|
+
# Not all trivial readers/writers can be defined with attr_* methods
|
57
|
+
TrivialAccessors:
|
58
|
+
Enabled: false
|
59
|
+
|
60
|
+
# Allow empty lines around body
|
61
|
+
EmptyLinesAroundBody:
|
62
|
+
Enabled: false
|
@@ -1,15 +1,75 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require 'forwardable'
|
4
|
+
require 'active_support/all'
|
5
|
+
require 'active_model'
|
6
|
+
|
7
|
+
require 'active_model/better_errors/error_message'
|
8
|
+
require 'active_model/better_errors/error_message_set'
|
9
|
+
require 'active_model/better_errors/error_collection'
|
10
|
+
|
11
|
+
require 'active_model/better_errors/formatter'
|
12
|
+
require 'active_model/better_errors/human_message_formatter'
|
13
|
+
|
14
|
+
require 'active_model/better_errors/reporter'
|
15
|
+
require 'active_model/better_errors/message_reporter'
|
16
|
+
require 'active_model/better_errors/hash_reporter'
|
17
|
+
require 'active_model/better_errors/array_reporter'
|
2
18
|
|
3
|
-
require '
|
19
|
+
require 'active_model/better_errors/human_message_reporter'
|
20
|
+
require 'active_model/better_errors/human_hash_reporter'
|
21
|
+
require 'active_model/better_errors/human_array_reporter'
|
4
22
|
|
5
|
-
require 'active_model/
|
23
|
+
require 'active_model/better_errors/machine_hash_reporter'
|
24
|
+
require 'active_model/better_errors/machine_array_reporter'
|
6
25
|
|
7
|
-
require 'active_model/
|
26
|
+
require 'active_model/better_errors/emulation'
|
27
|
+
require 'active_model/better_errors/errors'
|
8
28
|
|
9
29
|
module ActiveModel
|
30
|
+
#
|
31
|
+
# BetterErrors
|
32
|
+
#
|
33
|
+
module BetterErrors
|
34
|
+
class << self
|
35
|
+
attr_accessor :formatter
|
36
|
+
|
37
|
+
def set_reporter(name, reporter)
|
38
|
+
name = name.to_s
|
39
|
+
@reporter_maps ||= {}
|
40
|
+
return @reporter_maps.delete(name) unless reporter
|
41
|
+
@reporter_maps[name] = get_reporter_class(name, reporter)
|
42
|
+
end
|
43
|
+
|
44
|
+
def reporters
|
45
|
+
@reporter_maps ||= {}
|
46
|
+
@reporter_maps.clone
|
47
|
+
end
|
48
|
+
|
49
|
+
def get_reporter_class(name, reporter)
|
50
|
+
return reporter if reporter.is_a? Class
|
51
|
+
class_name = "#{reporter}_#{name}_reporter"
|
52
|
+
"active_model/better_errors/#{class_name}".classify.constantize
|
53
|
+
end
|
54
|
+
|
55
|
+
def format_message(base, message)
|
56
|
+
formatter.new(base, message).format_message
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
set_reporter :message, :human
|
61
|
+
set_reporter :array, :human
|
62
|
+
set_reporter :hash, :human
|
63
|
+
|
64
|
+
self.formatter = HumanMessageFormatter
|
65
|
+
end
|
66
|
+
|
67
|
+
#
|
68
|
+
# ActiveModel::Validations.errors override
|
69
|
+
#
|
10
70
|
module Validations
|
11
71
|
def errors
|
12
|
-
@errors ||=
|
72
|
+
@errors ||= BetterErrors::Errors.new(self)
|
13
73
|
end
|
14
74
|
end
|
15
75
|
end
|
@@ -1,10 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
#
|
2
4
|
# Allows included class to emulate ActiveModel::Errors class
|
3
5
|
# by defining a set of methods to delegate to facilities
|
4
6
|
# in this gem.
|
5
7
|
#
|
6
8
|
module ActiveModel
|
7
|
-
module
|
9
|
+
module BetterErrors
|
10
|
+
#
|
11
|
+
# Emulation
|
12
|
+
# The ActiveModel Emulation Layer
|
13
|
+
#
|
8
14
|
module Emulation
|
9
15
|
MODEL_METHODS = [
|
10
16
|
:clear, :include?, :get, :set, :delete, :[], :[]=,
|
@@ -44,22 +50,23 @@ module ActiveModel
|
|
44
50
|
end
|
45
51
|
end
|
46
52
|
|
47
|
-
def add(attribute, message=nil, options = {})
|
53
|
+
def add(attribute, message = nil, options = {})
|
48
54
|
if options[:strict]
|
49
55
|
error = ErrorMessage.build(attribute, message, options)
|
50
|
-
message =
|
51
|
-
|
56
|
+
message = ::ActiveModel::BetterErrors.format_message(@base, error)
|
57
|
+
full_message = full_message(attribute, message)
|
58
|
+
fail ActiveModel::StrictValidationFailed, full_message
|
52
59
|
end
|
53
60
|
error_collection.add attribute, message, options
|
54
61
|
end
|
55
62
|
|
56
|
-
def to_xml(options={})
|
57
|
-
to_a.to_xml options.reverse_merge(:
|
63
|
+
def to_xml(options = {})
|
64
|
+
to_a.to_xml options.reverse_merge(root: 'errors', skip_types: true)
|
58
65
|
end
|
59
66
|
|
60
|
-
def as_json(options=nil)
|
67
|
+
def as_json(options = nil)
|
61
68
|
to_hash
|
62
69
|
end
|
63
70
|
end
|
64
71
|
end
|
65
|
-
end
|
72
|
+
end
|
@@ -1,5 +1,10 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
module ActiveModel
|
2
|
-
module
|
4
|
+
module BetterErrors
|
5
|
+
#
|
6
|
+
# ErrorCollection
|
7
|
+
#
|
3
8
|
class ErrorCollection
|
4
9
|
include Enumerable
|
5
10
|
|
@@ -14,7 +19,7 @@ module ActiveModel
|
|
14
19
|
end
|
15
20
|
|
16
21
|
def include?(attribute)
|
17
|
-
( v = @collection[attribute]
|
22
|
+
( v = @collection[attribute]) && v.any?
|
18
23
|
end
|
19
24
|
|
20
25
|
def get(attribute)
|
@@ -40,12 +45,14 @@ module ActiveModel
|
|
40
45
|
|
41
46
|
def each
|
42
47
|
@collection.each_key do |attribute|
|
43
|
-
self[attribute].each
|
48
|
+
self[attribute].each do |error_message|
|
49
|
+
yield attribute, error_message
|
50
|
+
end
|
44
51
|
end
|
45
52
|
end
|
46
53
|
|
47
54
|
def size
|
48
|
-
values.
|
55
|
+
values.reduce(0) { |a, e| a + e.size }
|
49
56
|
end
|
50
57
|
alias_method :count, :size
|
51
58
|
|
@@ -75,11 +82,12 @@ module ActiveModel
|
|
75
82
|
end
|
76
83
|
|
77
84
|
def add(attribute, message, options = {})
|
78
|
-
self[attribute] << [
|
85
|
+
self[attribute] << [message, options]
|
79
86
|
end
|
80
87
|
|
81
88
|
def added?(attribute, message = nil, options = {})
|
82
|
-
|
89
|
+
message = ErrorMessage.build(base, attribute, message, options)
|
90
|
+
self[attribute].include? message
|
83
91
|
end
|
84
92
|
end
|
85
93
|
end
|
@@ -1,8 +1,15 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
module ActiveModel
|
2
|
-
module
|
4
|
+
module BetterErrors
|
5
|
+
#
|
6
|
+
# ErrorMessage
|
7
|
+
#
|
3
8
|
class ErrorMessage
|
4
9
|
include Comparable
|
5
|
-
CALLBACKS_OPTIONS = [
|
10
|
+
CALLBACKS_OPTIONS = [
|
11
|
+
:if, :unless, :on, :allow_nil, :allow_blank, :strict
|
12
|
+
]
|
6
13
|
|
7
14
|
# return the message either as nil, symbol, or string
|
8
15
|
def self.normalize(message)
|
@@ -35,7 +42,7 @@ module ActiveModel
|
|
35
42
|
[symbol, string]
|
36
43
|
end
|
37
44
|
|
38
|
-
def self.build(base, attribute, message, options=nil)
|
45
|
+
def self.build(base, attribute, message, options = nil)
|
39
46
|
options = options ? options : {}
|
40
47
|
options = options.except(*CALLBACKS_OPTIONS)
|
41
48
|
|
@@ -54,7 +61,7 @@ module ActiveModel
|
|
54
61
|
@options = options
|
55
62
|
end
|
56
63
|
|
57
|
-
def <=>
|
64
|
+
def <=>(other)
|
58
65
|
to_hash <=> other.to_hash
|
59
66
|
end
|
60
67
|
|
@@ -67,12 +74,16 @@ module ActiveModel
|
|
67
74
|
}
|
68
75
|
end
|
69
76
|
|
77
|
+
def as_json(*json_args)
|
78
|
+
to_hash
|
79
|
+
end
|
80
|
+
|
70
81
|
def hash
|
71
82
|
to_hash.hash
|
72
83
|
end
|
73
84
|
|
74
85
|
def to_s
|
75
|
-
|
86
|
+
::ActiveModel::BetterErrors.format_message(base, self)
|
76
87
|
end
|
77
88
|
|
78
89
|
def inspect
|
@@ -1,7 +1,12 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
module ActiveModel
|
2
|
-
module
|
4
|
+
module BetterErrors
|
5
|
+
#
|
6
|
+
# ErrorMessageSet
|
7
|
+
#
|
3
8
|
class ErrorMessageSet < Array
|
4
|
-
def initialize(base, attribute, errors=[])
|
9
|
+
def initialize(base, attribute, errors = [])
|
5
10
|
@base = base
|
6
11
|
@attribute = attribute
|
7
12
|
errors.each do |error|
|