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/.document
DELETED
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
1.6.3
|
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'active_model/error_collecting/error_message'
|
2
|
-
require 'active_model/error_collecting/error_message_set'
|
3
|
-
require 'active_model/error_collecting/error_collection'
|
4
|
-
|
5
|
-
require 'active_model/error_collecting/reporter'
|
6
|
-
require 'active_model/error_collecting/message_reporter'
|
7
|
-
require 'active_model/error_collecting/hash_reporter'
|
8
|
-
require 'active_model/error_collecting/array_reporter'
|
9
|
-
|
10
|
-
require 'active_model/error_collecting/human_message_formatter'
|
11
|
-
require 'active_model/error_collecting/human_message_reporter'
|
12
|
-
require 'active_model/error_collecting/human_hash_reporter'
|
13
|
-
require 'active_model/error_collecting/human_array_reporter'
|
14
|
-
|
15
|
-
require 'active_model/error_collecting/machine_hash_reporter'
|
16
|
-
require 'active_model/error_collecting/machine_array_reporter'
|
17
|
-
|
18
|
-
require 'active_model/error_collecting/emulation'
|
19
|
-
require 'active_model/error_collecting/errors'
|
20
|
-
|
21
|
-
require 'active_model/error_collecting/core_ext'
|
22
|
-
|
23
|
-
module ActiveModel
|
24
|
-
module ErrorCollecting
|
25
|
-
class << self
|
26
|
-
def set_reporter(name, reporter)
|
27
|
-
name = name.to_s
|
28
|
-
@reporter_maps ||= {}
|
29
|
-
return @reporter_maps.delete(name) unless reporter
|
30
|
-
@reporter_maps[name] = get_reporter_class(name, reporter)
|
31
|
-
end
|
32
|
-
|
33
|
-
def reporters
|
34
|
-
@reporter_maps ||= {}
|
35
|
-
@reporter_maps.clone
|
36
|
-
end
|
37
|
-
|
38
|
-
def get_reporter_class(name, reporter)
|
39
|
-
return reporter if reporter.is_a? Class
|
40
|
-
class_name = "active_model/error_collecting/#{reporter}_#{name}_reporter"
|
41
|
-
class_name.classify.constantize
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
set_reporter :message, :human
|
46
|
-
set_reporter :array, :human
|
47
|
-
set_reporter :hash, :human
|
48
|
-
end
|
49
|
-
end
|
@@ -1,58 +0,0 @@
|
|
1
|
-
module ActiveModel
|
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
|
17
|
-
|
18
|
-
def format_message
|
19
|
-
return message if message && error_message.type.nil?
|
20
|
-
|
21
|
-
keys = i18n_keys
|
22
|
-
key = keys.shift
|
23
|
-
|
24
|
-
value = (attribute != :base ? base.send(:read_attribute_for_validation, attribute) : nil)
|
25
|
-
|
26
|
-
options = {
|
27
|
-
:default => keys,
|
28
|
-
:model => base.class.model_name.human,
|
29
|
-
:attribute => base.class.human_attribute_name(attribute),
|
30
|
-
:value => value
|
31
|
-
}.merge(self.options)
|
32
|
-
|
33
|
-
I18n.translate key, options
|
34
|
-
end
|
35
|
-
|
36
|
-
private
|
37
|
-
|
38
|
-
def i18n_keys
|
39
|
-
if base.class.respond_to?(:i18n_scope)
|
40
|
-
keys = base.class.lookup_ancestors.map do |klass|
|
41
|
-
[ :"#{base.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.#{type}",
|
42
|
-
:"#{base.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.#{type}" ]
|
43
|
-
end
|
44
|
-
else
|
45
|
-
keys = []
|
46
|
-
end
|
47
|
-
keys << message
|
48
|
-
keys << :"#{base.class.i18n_scope}.errors.messages.#{type}" if base.class.respond_to?(:i18n_scope)
|
49
|
-
keys << :"errors.attributes.#{attribute}.#{type}"
|
50
|
-
keys << :"errors.messages.#{type}"
|
51
|
-
|
52
|
-
keys.compact!
|
53
|
-
keys.flatten!
|
54
|
-
keys
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
@@ -1,293 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe ActiveModel::ErrorCollecting::ErrorMessage do
|
4
|
-
let(:klass) { ActiveModel::ErrorCollecting::ErrorMessage }
|
5
|
-
let(:attribute) { :name }
|
6
|
-
let(:base) { User.new }
|
7
|
-
subject(:error_message) { klass.build(base, attribute, message, options) }
|
8
|
-
|
9
|
-
describe ".normalize" do
|
10
|
-
subject { klass.normalize message }
|
11
|
-
context "when message is a symbol" do
|
12
|
-
let(:message) { :invalid }
|
13
|
-
it { should == message }
|
14
|
-
end
|
15
|
-
|
16
|
-
context "when message is nil" do
|
17
|
-
let(:message) { nil }
|
18
|
-
it { should be nil }
|
19
|
-
end
|
20
|
-
|
21
|
-
context "when message is a string" do
|
22
|
-
let(:message) { 'bad' }
|
23
|
-
it { should == message }
|
24
|
-
end
|
25
|
-
|
26
|
-
context "when message is a proc" do
|
27
|
-
let(:message) { proc { 'bad' } }
|
28
|
-
it { should == 'bad' }
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe ".identify" do
|
33
|
-
subject { klass.identify message, override }
|
34
|
-
context "when message is a symbol and" do
|
35
|
-
let(:message) { :invalid }
|
36
|
-
|
37
|
-
context "when override is a symbol" do
|
38
|
-
let(:override) { :not_good }
|
39
|
-
it { should == [:not_good, nil] }
|
40
|
-
end
|
41
|
-
|
42
|
-
context "when override is nil" do
|
43
|
-
let(:override) { nil }
|
44
|
-
it { should == [:invalid, nil] }
|
45
|
-
end
|
46
|
-
|
47
|
-
context "when override is a string" do
|
48
|
-
let(:override) { "invalid" }
|
49
|
-
it { should == [:invalid, "invalid"] }
|
50
|
-
end
|
51
|
-
|
52
|
-
context "when override is a proc" do
|
53
|
-
let(:override) { proc { "Not good" } }
|
54
|
-
it { should == [:invalid, "Not good"] }
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
context "when message is a string and" do
|
59
|
-
let(:message) { "Invalid" }
|
60
|
-
|
61
|
-
context "when override is a symbol" do
|
62
|
-
let(:override) { :not_good }
|
63
|
-
it { should == [:not_good, "Invalid"] }
|
64
|
-
end
|
65
|
-
|
66
|
-
context "when override is nil" do
|
67
|
-
let(:override) { nil }
|
68
|
-
it { should == [nil, "Invalid"] }
|
69
|
-
end
|
70
|
-
|
71
|
-
context "when override is a string" do
|
72
|
-
let(:override) { "not good" }
|
73
|
-
it { should == [nil, "not good"] }
|
74
|
-
end
|
75
|
-
|
76
|
-
context "when override is a proc" do
|
77
|
-
let(:override) { proc { "Not Good" } }
|
78
|
-
it { should == [nil, "Not Good"] }
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
context "when message is nil and" do
|
83
|
-
let(:message) { nil }
|
84
|
-
|
85
|
-
context "when override is a symbol" do
|
86
|
-
let(:override) { :not_good }
|
87
|
-
it { should == [:not_good, nil] }
|
88
|
-
end
|
89
|
-
|
90
|
-
context "when override is nil" do
|
91
|
-
let(:override) { nil }
|
92
|
-
it { should == [nil, nil] }
|
93
|
-
end
|
94
|
-
|
95
|
-
context "when override is a string" do
|
96
|
-
let(:override) { "not good" }
|
97
|
-
it { should == [nil, "not good"] }
|
98
|
-
end
|
99
|
-
|
100
|
-
context "when override is a proc" do
|
101
|
-
let(:override) { proc { "Not Good" } }
|
102
|
-
it { should == [nil, "Not Good"] }
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
context "when message is a proc and" do
|
107
|
-
let(:message) { proc {"invalid"} }
|
108
|
-
|
109
|
-
context "when override is a symbol" do
|
110
|
-
let(:override) { :not_good }
|
111
|
-
it { should == [:not_good, "invalid"] }
|
112
|
-
end
|
113
|
-
|
114
|
-
context "when override is nil" do
|
115
|
-
let(:override) { nil }
|
116
|
-
it { should == [nil, "invalid"] }
|
117
|
-
end
|
118
|
-
|
119
|
-
context "when override is a string" do
|
120
|
-
let(:override) { "not good" }
|
121
|
-
it { should == [nil, "not good"] }
|
122
|
-
end
|
123
|
-
|
124
|
-
context "when override is a proc" do
|
125
|
-
let(:override) { proc { "Not Good" } }
|
126
|
-
it { should == [nil, "Not Good"] }
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
|
132
|
-
describe ".build" do
|
133
|
-
context "when message is a symbol and" do
|
134
|
-
let(:message) { :invalid }
|
135
|
-
|
136
|
-
context "when options[:message] is a string" do
|
137
|
-
let(:options) { { message: "Not Really Valid!" } }
|
138
|
-
its(:type) { should == message }
|
139
|
-
its(:message) { should == options[:message] }
|
140
|
-
end
|
141
|
-
|
142
|
-
context "when options[:message] is a symbol" do
|
143
|
-
let(:options) { { message: :not_really_valid } }
|
144
|
-
its(:type) { should == options[:message] }
|
145
|
-
its(:message) { should == nil }
|
146
|
-
end
|
147
|
-
|
148
|
-
context "when options[:message] is nil" do
|
149
|
-
let(:options) { { message: nil } }
|
150
|
-
its(:type) { should == message }
|
151
|
-
its(:message) { should == nil }
|
152
|
-
end
|
153
|
-
|
154
|
-
context "when options[:message] is proc" do
|
155
|
-
let(:options) { { message: proc { "Not Really Valid!" } } }
|
156
|
-
its(:type) { should == message }
|
157
|
-
its(:message) { should == options[:message].call }
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
context "when message is a string and" do
|
162
|
-
let(:message) { 'Invalid!' }
|
163
|
-
|
164
|
-
context "when options[:message] is a string" do
|
165
|
-
let(:options) { { message: "Not Really Valid!" } }
|
166
|
-
its(:type) { should == nil }
|
167
|
-
its(:message) { should == options[:message] }
|
168
|
-
end
|
169
|
-
|
170
|
-
context "when options[:message] is a symbol" do
|
171
|
-
let(:options) { { message: :not_really_valid } }
|
172
|
-
its(:type) { should == options[:message] }
|
173
|
-
its(:message) { should == message }
|
174
|
-
end
|
175
|
-
|
176
|
-
context "when options[:message] is nil" do
|
177
|
-
let(:options) { { } }
|
178
|
-
its(:type) { should == nil }
|
179
|
-
its(:message) { should == message }
|
180
|
-
end
|
181
|
-
|
182
|
-
context "when options[:message] is proc" do
|
183
|
-
let(:options) { { message: proc { "Not Really Valid!" } } }
|
184
|
-
its(:type) { should == nil }
|
185
|
-
its(:message) { should == options[:message].call }
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
context "when message is a proc and" do
|
190
|
-
let(:message) { proc{ 'Invalid!' } }
|
191
|
-
|
192
|
-
context "when options[:message] is a string" do
|
193
|
-
let(:options) { { message: "Not Really Valid!" } }
|
194
|
-
its(:type) { should == nil }
|
195
|
-
its(:message) { should == options[:message] }
|
196
|
-
end
|
197
|
-
|
198
|
-
context "when options[:message] is a symbol" do
|
199
|
-
let(:options) { { message: :not_really_valid } }
|
200
|
-
its(:type) { should == options[:message] }
|
201
|
-
its(:message) { should == message.call }
|
202
|
-
end
|
203
|
-
|
204
|
-
context "when options[:message] is nil" do
|
205
|
-
let(:options) { { } }
|
206
|
-
its(:type) { should == nil }
|
207
|
-
its(:message) { should == message.call }
|
208
|
-
end
|
209
|
-
|
210
|
-
context "when options[:message] is proc" do
|
211
|
-
let(:options) { { message: proc { "Not Really Valid!" } } }
|
212
|
-
its(:type) { should == nil }
|
213
|
-
its(:message) { should == options[:message].call }
|
214
|
-
end
|
215
|
-
end
|
216
|
-
|
217
|
-
|
218
|
-
context "when message is nil and" do
|
219
|
-
let(:message) { nil }
|
220
|
-
|
221
|
-
context "when options[:message] is a string" do
|
222
|
-
let(:options) { { message: "Not Really Valid!" } }
|
223
|
-
its(:type) { should == nil }
|
224
|
-
its(:message) { should == options[:message] }
|
225
|
-
end
|
226
|
-
|
227
|
-
context "when options[:message] is a symbol" do
|
228
|
-
let(:options) { { message: :not_really_valid } }
|
229
|
-
its(:type) { should == options[:message] }
|
230
|
-
its(:message) { should == nil }
|
231
|
-
end
|
232
|
-
|
233
|
-
context "when options[:message] is nil" do
|
234
|
-
let(:options) { { } }
|
235
|
-
its(:type) { should == nil }
|
236
|
-
its(:message) { should == nil }
|
237
|
-
end
|
238
|
-
|
239
|
-
context "when options[:message] is proc" do
|
240
|
-
let(:options) { { message: proc { "Not Really Valid!" } } }
|
241
|
-
its(:type) { should == nil }
|
242
|
-
its(:message) { should == options[:message].call }
|
243
|
-
end
|
244
|
-
end
|
245
|
-
|
246
|
-
[
|
247
|
-
:if, :unless, :on, :allow_nil, :allow_blank, :strict, :message
|
248
|
-
].each do |field|
|
249
|
-
context "when options includes :#{field}" do
|
250
|
-
let(:message) { :invalid }
|
251
|
-
let(:options) { {} }
|
252
|
-
before { options[field] = proc{} }
|
253
|
-
its(:options) { should == {} }
|
254
|
-
end
|
255
|
-
end
|
256
|
-
end
|
257
|
-
|
258
|
-
describe '#initialize' do
|
259
|
-
let(:message) { :invalid }
|
260
|
-
let(:options) { { message: "Invalid!", count: 12 } }
|
261
|
-
|
262
|
-
its(:options) { should == { count: 12 } }
|
263
|
-
its(:attribute) { should == attribute }
|
264
|
-
end
|
265
|
-
|
266
|
-
describe '#<=>' do
|
267
|
-
subject { e1 <=> e2 }
|
268
|
-
|
269
|
-
context "when attribute is different" do
|
270
|
-
let(:e1) { klass.build base, :name, :invalid }
|
271
|
-
let(:e2) { klass.build base, :name1, :invalid }
|
272
|
-
it {should == nil }
|
273
|
-
end
|
274
|
-
|
275
|
-
context "when type is different" do
|
276
|
-
let(:e1) { klass.build base, :name, :invalid }
|
277
|
-
let(:e2) { klass.build base, :name, :invalid1 }
|
278
|
-
it {should == nil }
|
279
|
-
end
|
280
|
-
|
281
|
-
context "when message is different" do
|
282
|
-
let(:e1) { klass.build base, :name, :invalid, :message => "a" }
|
283
|
-
let(:e2) { klass.build base, :name, :invalid, :message => "b" }
|
284
|
-
it {should == nil }
|
285
|
-
end
|
286
|
-
|
287
|
-
context "when it is the same" do
|
288
|
-
let(:e1) { klass.build base, :name, :invalid }
|
289
|
-
let(:e2) { klass.build base, :name, :invalid }
|
290
|
-
it { should == 0 }
|
291
|
-
end
|
292
|
-
end
|
293
|
-
end
|