Skip to content

Commit 407f27e

Browse files
committed
Returning immutable attributes protects against surprises
1 parent fd2d302 commit 407f27e

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

lib/active_model_serializers/model.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ def updated_at
4242
defined?(@updated_at) ? @updated_at : File.mtime(__FILE__)
4343
end
4444

45+
# @return [Hash] immutable representation of declared attributes
4546
def attributes
4647
attribute_names.each_with_object({}) do |attribute_name, result|
47-
result[attribute_name] = public_send(attribute_name)
48-
end.with_indifferent_access
48+
result[attribute_name.freeze] = public_send(attribute_name).freeze
49+
end.with_indifferent_access.freeze
4950
end
5051

5152
# The following methods are needed to be minimally implemented for ActiveModel::Errors

test/cache_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ def test_fragment_fetch_with_virtual_attributes
532532
role_hash = role_serializer.fetch_attributes_fragment(adapter_instance)
533533
assert_equal(role_hash, expected_result)
534534

535-
role.attributes[:id] = 'this has been updated'
535+
role.id = 'this has been updated'
536536
role.name = 'this was cached'
537537

538538
role_hash = role_serializer.fetch_attributes_fragment(adapter_instance)

0 commit comments

Comments
 (0)