File tree Expand file tree Collapse file tree 2 files changed +15
-1
lines changed Expand file tree Collapse file tree 2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -228,18 +228,22 @@ def to_hash(full_messages = false)
228
228
229
229
undef :to_h
230
230
231
+ EMPTY_ARRAY = [ ] . freeze # :nodoc:
232
+
231
233
# Returns a Hash of attributes with an array of their error messages.
232
234
def messages
233
235
hash = to_hash
236
+ hash . default = EMPTY_ARRAY
234
237
hash . freeze
238
+ hash
235
239
end
236
240
237
241
# Returns a Hash of attributes with an array of their error details.
238
242
def details
239
243
hash = group_by_attribute . transform_values do |errors |
240
244
errors . map ( &:details )
241
245
end
242
- hash . default = [ ]
246
+ hash . default = EMPTY_ARRAY
243
247
hash . freeze
244
248
hash
245
249
end
Original file line number Diff line number Diff line change @@ -455,6 +455,14 @@ def test_no_key
455
455
assert_nil person . errors . as_json . default_proc
456
456
end
457
457
458
+ test "messages returns empty frozen array when when accessed with non-existent attribute" do
459
+ errors = ActiveModel ::Errors . new ( Person . new )
460
+
461
+ assert_equal [ ] , errors . messages [ :foo ]
462
+ assert_raises ( FrozenError ) { errors . messages [ :foo ] << "foo" }
463
+ assert_raises ( FrozenError ) { errors . messages [ :foo ] . clear }
464
+ end
465
+
458
466
test "full_messages doesn't require the base object to respond to `:errors" do
459
467
model = Class . new do
460
468
def initialize
@@ -621,6 +629,8 @@ def call
621
629
errors = ActiveModel ::Errors . new ( Person . new )
622
630
623
631
assert_equal [ ] , errors . details [ :foo ]
632
+ assert_raises ( FrozenError ) { errors . details [ :foo ] << "foo" }
633
+ assert_raises ( FrozenError ) { errors . details [ :foo ] . clear }
624
634
end
625
635
626
636
test "copy errors" do
You can’t perform that action at this time.
0 commit comments