instructure-active_model-better_errors 1.6.3.rails2.5 → 1.6.5.rails2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/.gitignore +37 -0
- data/.rspec +4 -1
- data/.ruby-gemset +1 -0
- data/.travis.yml +5 -1
- data/Gemfile +9 -11
- data/Gemfile.devtools +66 -0
- data/Guardfile +32 -0
- data/LICENSE.txt +2 -0
- data/README.md +12 -10
- data/Rakefile +20 -49
- 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/instructure-active_model-better_errors.gemspec +25 -112
- data/lib/active_model/better_errors/array_reporter.rb +14 -0
- data/lib/active_model/{error_collecting → better_errors}/emulation.rb +13 -6
- data/lib/active_model/{error_collecting → better_errors}/error_collection.rb +14 -6
- data/lib/active_model/{error_collecting → better_errors}/error_message.rb +12 -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/{error_collecting → better_errors}/human_message_formatter.rb +16 -20
- 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/lib/active_model/better_errors.rb +67 -3
- data/spec/spec_helper.rb +21 -14
- 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 +58 -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 +79 -171
- data/.document +0 -5
- data/VERSION +0 -1
- data/lib/active_model/error_collecting/array_reporter.rb +0 -9
- data/lib/active_model/error_collecting/hash_reporter.rb +0 -9
- data/lib/active_model/error_collecting.rb +0 -47
- data/spec/lib/active_model/better_errors_spec.rb +0 -7
- data/spec/lib/active_model/error_collecting/error_message_spec.rb +0 -309
- 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
@@ -1,118 +1,31 @@
|
|
1
|
-
#
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
-
# -*- encoding: utf-8 -*-
|
1
|
+
# coding: utf-8
|
5
2
|
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'active_model/better_errors/version'
|
9
6
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
s.files = [
|
20
|
-
".document",
|
21
|
-
".gitmodules",
|
22
|
-
".rspec",
|
23
|
-
".travis.yml",
|
24
|
-
"Gemfile",
|
25
|
-
"LICENSE.txt",
|
26
|
-
"README.md",
|
27
|
-
"Rakefile",
|
28
|
-
"VERSION",
|
29
|
-
"instructure-active_model-better_errors.gemspec",
|
30
|
-
"lib/active_model/better_errors.rb",
|
31
|
-
"lib/active_model/error_collecting.rb",
|
32
|
-
"lib/active_model/error_collecting/array_reporter.rb",
|
33
|
-
"lib/active_model/error_collecting/emulation.rb",
|
34
|
-
"lib/active_model/error_collecting/error_collection.rb",
|
35
|
-
"lib/active_model/error_collecting/error_message.rb",
|
36
|
-
"lib/active_model/error_collecting/error_message_set.rb",
|
37
|
-
"lib/active_model/error_collecting/errors.rb",
|
38
|
-
"lib/active_model/error_collecting/hash_reporter.rb",
|
39
|
-
"lib/active_model/error_collecting/human_array_reporter.rb",
|
40
|
-
"lib/active_model/error_collecting/human_hash_reporter.rb",
|
41
|
-
"lib/active_model/error_collecting/human_message_formatter.rb",
|
42
|
-
"lib/active_model/error_collecting/human_message_reporter.rb",
|
43
|
-
"lib/active_model/error_collecting/machine_array_reporter.rb",
|
44
|
-
"lib/active_model/error_collecting/machine_hash_reporter.rb",
|
45
|
-
"lib/active_model/error_collecting/message_reporter.rb",
|
46
|
-
"lib/active_model/error_collecting/reporter.rb",
|
47
|
-
"spec/lib/active_model/better_errors_spec.rb",
|
48
|
-
"spec/lib/active_model/error_collecting/emulation_spec.rb",
|
49
|
-
"spec/lib/active_model/error_collecting/error_collection_spec.rb",
|
50
|
-
"spec/lib/active_model/error_collecting/error_message_set_spec.rb",
|
51
|
-
"spec/lib/active_model/error_collecting/error_message_spec.rb",
|
52
|
-
"spec/lib/active_model/error_collecting/errors_spec.rb",
|
53
|
-
"spec/lib/active_model/error_collecting/human_array_reporter_spec.rb",
|
54
|
-
"spec/lib/active_model/error_collecting/human_hash_reporter_spec.rb",
|
55
|
-
"spec/lib/active_model/error_collecting/human_message_formatter_spec.rb",
|
56
|
-
"spec/lib/active_model/error_collecting/human_message_reporter_spec.rb",
|
57
|
-
"spec/lib/active_model/error_collecting/machine_array_reporter_spec.rb",
|
58
|
-
"spec/lib/active_model/error_collecting/machine_hash_reporter_spec.rb",
|
59
|
-
"spec/lib/active_model/error_collecting_spec.rb",
|
60
|
-
"spec/spec_helper.rb",
|
61
|
-
"spec/support/models.rb",
|
62
|
-
"test/integration.rb"
|
63
|
-
]
|
64
|
-
s.homepage = "http://github.com/codekitchen/active_model-better_errors"
|
65
|
-
s.licenses = ["MIT"]
|
66
|
-
s.require_paths = ["lib"]
|
67
|
-
s.rubygems_version = "1.8.23"
|
68
|
-
s.summary = "API consumable error messages with ActiveModel::Errors drop-in compatibility."
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "instructure-active_model-better_errors"
|
9
|
+
spec.version = ActiveModel::BetterErrors::VERSION
|
10
|
+
spec.authors = ["Aaron Qian", "Byron Anderson", "Brian Palmer"]
|
11
|
+
spec.email = ["aq1018@gmail.com", "byronanderson32@gmail.com", "brianp@instructure.com"]
|
12
|
+
spec.description = "API consumable error messages with ActiveModel::Errors drop-in compatibility."
|
13
|
+
spec.summary = "active_model-better_errors is a ActiveModel::Errors compatible library to help you customize the presentation of your error messages."
|
14
|
+
spec.homepage = "http://github.com/aq1018/active_model-better_errors"
|
15
|
+
spec.license = "MIT"
|
69
16
|
|
70
|
-
|
71
|
-
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
72
21
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
s.add_development_dependency(%q<bundler>, [">= 0"])
|
81
|
-
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
82
|
-
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
83
|
-
s.add_development_dependency(%q<bcrypt-ruby>, ["~> 3.0.0"])
|
84
|
-
s.add_development_dependency(%q<mocha>, [">= 0.12.1"])
|
85
|
-
s.add_development_dependency(%q<sqlite3>, [">= 0"])
|
86
|
-
s.add_development_dependency(%q<debugger>, [">= 0"])
|
87
|
-
else
|
88
|
-
s.add_dependency(%q<activerecord>, ["~> 2.3.11"])
|
89
|
-
s.add_dependency(%q<rake>, [">= 0"])
|
90
|
-
s.add_dependency(%q<rspec>, ["~> 2.12.0"])
|
91
|
-
s.add_dependency(%q<pry>, [">= 0"])
|
92
|
-
s.add_dependency(%q<yard>, [">= 0"])
|
93
|
-
s.add_dependency(%q<rdoc>, [">= 0"])
|
94
|
-
s.add_dependency(%q<bundler>, [">= 0"])
|
95
|
-
s.add_dependency(%q<jeweler>, [">= 0"])
|
96
|
-
s.add_dependency(%q<simplecov>, [">= 0"])
|
97
|
-
s.add_dependency(%q<bcrypt-ruby>, ["~> 3.0.0"])
|
98
|
-
s.add_dependency(%q<mocha>, [">= 0.12.1"])
|
99
|
-
s.add_dependency(%q<sqlite3>, [">= 0"])
|
100
|
-
s.add_dependency(%q<debugger>, [">= 0"])
|
101
|
-
end
|
102
|
-
else
|
103
|
-
s.add_dependency(%q<activerecord>, ["~> 2.3.11"])
|
104
|
-
s.add_dependency(%q<rake>, [">= 0"])
|
105
|
-
s.add_dependency(%q<rspec>, ["~> 2.12.0"])
|
106
|
-
s.add_dependency(%q<pry>, [">= 0"])
|
107
|
-
s.add_dependency(%q<yard>, [">= 0"])
|
108
|
-
s.add_dependency(%q<rdoc>, [">= 0"])
|
109
|
-
s.add_dependency(%q<bundler>, [">= 0"])
|
110
|
-
s.add_dependency(%q<jeweler>, [">= 0"])
|
111
|
-
s.add_dependency(%q<simplecov>, [">= 0"])
|
112
|
-
s.add_dependency(%q<bcrypt-ruby>, ["~> 3.0.0"])
|
113
|
-
s.add_dependency(%q<mocha>, [">= 0.12.1"])
|
114
|
-
s.add_dependency(%q<sqlite3>, [">= 0"])
|
115
|
-
s.add_dependency(%q<debugger>, [">= 0"])
|
116
|
-
end
|
22
|
+
spec.add_development_dependency "bundler"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec", "~> 2.12.0"
|
25
|
+
spec.add_development_dependency "mocha", '>= 0.12.1'
|
26
|
+
spec.add_development_dependency "simplecov"
|
27
|
+
spec.add_development_dependency "yard"
|
28
|
+
spec.add_development_dependency "rdoc"
|
117
29
|
end
|
118
30
|
|
31
|
+
|
@@ -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, :[], :[]=,
|
@@ -49,17 +55,18 @@ module ActiveModel
|
|
49
55
|
options = { message: options } unless options.is_a?(Hash)
|
50
56
|
if options[:strict]
|
51
57
|
error = ErrorMessage.build(attribute, message, options)
|
52
|
-
message =
|
53
|
-
|
58
|
+
message = ::ActiveModel::BetterErrors.format_message(@base, error)
|
59
|
+
full_message = full_message(attribute, message)
|
60
|
+
fail ActiveModel::StrictValidationFailed, full_message
|
54
61
|
end
|
55
62
|
error_collection.add attribute, message, options
|
56
63
|
end
|
57
64
|
|
58
|
-
def to_xml(options={})
|
59
|
-
to_a.to_xml options.reverse_merge(:
|
65
|
+
def to_xml(options = {})
|
66
|
+
to_a.to_xml options.reverse_merge(root: 'errors', skip_types: true)
|
60
67
|
end
|
61
68
|
|
62
|
-
def as_json(options=nil)
|
69
|
+
def as_json(options = nil)
|
63
70
|
to_hash
|
64
71
|
end
|
65
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
|
|
@@ -76,7 +83,7 @@ module ActiveModel
|
|
76
83
|
end
|
77
84
|
|
78
85
|
def to_s
|
79
|
-
|
86
|
+
::ActiveModel::BetterErrors.format_message(base, self)
|
80
87
|
end
|
81
88
|
|
82
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|
|
@@ -1,5 +1,10 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
module ActiveModel
|
2
|
-
module
|
4
|
+
module BetterErrors
|
5
|
+
#
|
6
|
+
# Errors
|
7
|
+
#
|
3
8
|
class Errors
|
4
9
|
include Emulation
|
5
10
|
|
@@ -28,7 +33,9 @@ module ActiveModel
|
|
28
33
|
|
29
34
|
def set_reporter(type, reporter)
|
30
35
|
type = type.to_s
|
31
|
-
klass = ::ActiveModel::
|
36
|
+
klass = ::ActiveModel::BetterErrors
|
37
|
+
.get_reporter_class(type, reporter)
|
38
|
+
|
32
39
|
@reporter_classes[type] = klass
|
33
40
|
@reporters.delete type
|
34
41
|
end
|
@@ -40,7 +47,7 @@ module ActiveModel
|
|
40
47
|
end
|
41
48
|
|
42
49
|
def reporter_classes
|
43
|
-
::ActiveModel::
|
50
|
+
::ActiveModel::BetterErrors.reporters
|
44
51
|
end
|
45
52
|
|
46
53
|
def get_reporter_class(type)
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
module BetterErrors
|
5
|
+
#
|
6
|
+
# Abstract Formatter class
|
7
|
+
class Formatter
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
def_delegators :@error_message, :attribute, :message, :options
|
11
|
+
|
12
|
+
attr_reader :base, :error_message
|
13
|
+
|
14
|
+
def initialize(base, error_message)
|
15
|
+
@base, @error_message = base, error_message
|
16
|
+
end
|
17
|
+
|
18
|
+
#
|
19
|
+
# @abstract
|
20
|
+
# Formats the error message into a comsumable string.
|
21
|
+
# see HumanMessageFormatter for more details.
|
22
|
+
def format_message
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -1,11 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
module ActiveModel
|
2
|
-
module
|
4
|
+
module BetterErrors
|
5
|
+
#
|
6
|
+
# HumanHashReporter
|
7
|
+
#
|
3
8
|
class HumanHashReporter < HashReporter
|
4
9
|
def to_hash
|
5
|
-
collection.to_hash.
|
10
|
+
collection.to_hash.reduce({}) do |hash, kv|
|
6
11
|
attribute, error_message_set = kv
|
7
12
|
hash[attribute] = error_message_set.map do |error_message|
|
8
|
-
|
13
|
+
::ActiveModel::BetterErrors.format_message(base, error_message)
|
9
14
|
end
|
10
15
|
hash
|
11
16
|
end
|
@@ -1,22 +1,13 @@
|
|
1
|
-
|
2
|
-
module ErrorCollecting
|
3
|
-
class HumanMessageFormatter
|
4
|
-
extend Forwardable
|
5
|
-
|
6
|
-
def_delegators :@error_message, :attribute, :message, :options
|
7
|
-
|
8
|
-
attr_reader :base, :error_message
|
9
|
-
|
10
|
-
def initialize(base, error_message)
|
11
|
-
@base, @error_message = base, error_message
|
12
|
-
end
|
13
|
-
|
14
|
-
def type
|
15
|
-
@error_message.type || :invalid
|
16
|
-
end
|
1
|
+
# encoding: utf-8
|
17
2
|
|
3
|
+
module ActiveModel
|
4
|
+
module BetterErrors
|
5
|
+
#
|
6
|
+
# HumanMessageFormatter
|
7
|
+
#
|
8
|
+
class HumanMessageFormatter < Formatter
|
18
9
|
def format_message
|
19
|
-
return message if message &&
|
10
|
+
return message if message && type.nil?
|
20
11
|
|
21
12
|
keys = i18n_keys
|
22
13
|
key = keys.shift
|
@@ -33,6 +24,10 @@ module ActiveModel
|
|
33
24
|
|
34
25
|
private
|
35
26
|
|
27
|
+
def type
|
28
|
+
error_message.type || :invalid
|
29
|
+
end
|
30
|
+
|
36
31
|
def value
|
37
32
|
return if attribute == :base
|
38
33
|
base.send :read_attribute, attribute
|
@@ -41,11 +36,12 @@ module ActiveModel
|
|
41
36
|
def ancestor_keys
|
42
37
|
return [] unless base.class.respond_to?(:i18n_scope)
|
43
38
|
scope = base.class.i18n_scope
|
39
|
+
|
44
40
|
base.class.lookup_ancestors.map do |klass|
|
45
|
-
|
41
|
+
key_base = "#{scope}.errors.models.#{klass.model_name.i18n_key}"
|
46
42
|
[
|
47
|
-
:"#{
|
48
|
-
:"#{
|
43
|
+
:"#{key_base}.attributes.#{attribute}.#{type}",
|
44
|
+
:"#{key_base}.#{type}"
|
49
45
|
]
|
50
46
|
end
|
51
47
|
end
|
@@ -1,31 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
module ActiveModel
|
2
|
-
module
|
4
|
+
module BetterErrors
|
5
|
+
#
|
6
|
+
# HumanMessageReporter
|
7
|
+
#
|
3
8
|
class HumanMessageReporter < MessageReporter
|
4
9
|
def full_messages
|
5
10
|
@collection.map do |attribute, error_message|
|
6
|
-
|
7
|
-
|
11
|
+
message = ::ActiveModel::BetterErrors.format_message(
|
12
|
+
base, error_message
|
13
|
+
)
|
8
14
|
full_message attribute, message
|
9
15
|
end
|
10
16
|
end
|
11
17
|
|
12
18
|
def full_message(attribute, message)
|
13
19
|
return message if attribute == :base
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
:
|
20
|
-
|
20
|
+
str = attribute.to_s.gsub('.', '_').humanize
|
21
|
+
str = base.class.human_attribute_name(attribute, default: str)
|
22
|
+
|
23
|
+
I18n.t(
|
24
|
+
'errors.format',
|
25
|
+
default: '%{attribute} %{message}',
|
26
|
+
attribute: str,
|
27
|
+
message: message
|
28
|
+
)
|
21
29
|
end
|
22
30
|
|
23
31
|
# This method is not used internally.
|
24
32
|
# This is for API Compatibility with ActiveModel::Errors only
|
25
33
|
def generate_message(attribute, type = :invalid, options = {})
|
26
34
|
error_message = ErrorMessage.build(base, attribute, type, options)
|
27
|
-
|
28
|
-
formatter.format_message
|
35
|
+
::ActiveModel::BetterErrors.format_message(base, error_message)
|
29
36
|
end
|
30
37
|
end
|
31
38
|
end
|
@@ -1,5 +1,10 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
module ActiveModel
|
2
|
-
module
|
4
|
+
module BetterErrors
|
5
|
+
#
|
6
|
+
# MachineArrayReporter
|
7
|
+
#
|
3
8
|
class MachineArrayReporter < ArrayReporter
|
4
9
|
def to_a
|
5
10
|
collection.to_a.map do |error_message|
|
@@ -11,7 +16,10 @@ module ActiveModel
|
|
11
16
|
result = {}
|
12
17
|
result[:attribute] = error_message.attribute.to_s
|
13
18
|
result[:type] = error_message.type || :invalid
|
14
|
-
|
19
|
+
|
20
|
+
options = error_message.options
|
21
|
+
result[:options] = options unless options.blank?
|
22
|
+
|
15
23
|
result
|
16
24
|
end
|
17
25
|
end
|
@@ -1,8 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
module ActiveModel
|
2
|
-
module
|
4
|
+
module BetterErrors
|
5
|
+
#
|
6
|
+
# MachineHashReporter
|
7
|
+
#
|
3
8
|
class MachineHashReporter < HashReporter
|
4
9
|
def to_hash
|
5
|
-
collection.to_hash.
|
10
|
+
collection.to_hash.reduce({}) do |hash, kv|
|
6
11
|
attribute, error_message_set = kv
|
7
12
|
hash[attribute] = error_message_set.map do |error_message|
|
8
13
|
format_error_message(error_message)
|
@@ -14,7 +19,9 @@ module ActiveModel
|
|
14
19
|
def format_error_message(error_message)
|
15
20
|
result = {}
|
16
21
|
result[:type] = error_message.type || :invalid
|
17
|
-
|
22
|
+
|
23
|
+
options = error_message.options
|
24
|
+
result[:options] = options unless options.blank?
|
18
25
|
result
|
19
26
|
end
|
20
27
|
end
|
@@ -1,17 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
module ActiveModel
|
2
|
-
module
|
4
|
+
module BetterErrors
|
5
|
+
#
|
6
|
+
# MessageReporter
|
7
|
+
#
|
3
8
|
class MessageReporter < Reporter
|
4
9
|
def full_messages
|
5
|
-
|
10
|
+
fail 'abstract method'
|
6
11
|
end
|
7
12
|
|
8
13
|
def full_message(attribute, message)
|
9
|
-
|
14
|
+
fail 'abstract method'
|
10
15
|
end
|
11
16
|
|
12
17
|
def generate_message(attribute, type = :invalid, options = {})
|
13
|
-
|
18
|
+
fail 'abstract method'
|
14
19
|
end
|
15
20
|
end
|
16
21
|
end
|
17
|
-
end
|
22
|
+
end
|
@@ -1,5 +1,10 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
module ActiveModel
|
2
|
-
module
|
4
|
+
module BetterErrors
|
5
|
+
#
|
6
|
+
# Reporter
|
7
|
+
#
|
3
8
|
class Reporter
|
4
9
|
attr_reader :collection
|
5
10
|
def initialize(collection)
|
@@ -11,4 +16,4 @@ module ActiveModel
|
|
11
16
|
end
|
12
17
|
end
|
13
18
|
end
|
14
|
-
end
|
19
|
+
end
|