Skip to content

Commit f7b3fe6

Browse files
committed
Add failing test for AMS::Model accessor vs. attributes mutation
1 parent 6266df2 commit f7b3fe6

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

lib/active_model_serializers/model.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ def initialize(attributes = {})
2626

2727
# Defaults to the downcased model name.
2828
def id
29-
@id ||= self.class.name.downcase
29+
@id ||= self.class.name && self.class.name.downcase
3030
end
3131

3232
# Defaults to the downcased model name and updated_at
3333
def cache_key
34-
"#{self.class.name.downcase}/#{id}-#{updated_at.strftime('%Y%m%d%H%M%S%9N')}"
34+
ActiveSupport::Cache.expand_cache_key([
35+
self.class.name && self.class.name.downcase,
36+
"#{id}-#{updated_at.strftime('%Y%m%d%H%M%S%9N')}"
37+
].compact)
3538
end
3639

3740
# Defaults to the time the serializer file was modified.

test/active_model_serializers/model_test.rb

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module ActiveModelSerializers
44
class ModelTest < ActiveSupport::TestCase
55
include ActiveModel::Serializer::Lint::Tests
66

7-
def setup
7+
setup do
88
@resource = ActiveModelSerializers::Model.new
99
end
1010

@@ -18,5 +18,52 @@ def test_initialization_with_string_keys
1818

1919
assert_equal model_instance.read_attribute_for_serialization(:key), value
2020
end
21+
22+
def test_attributes_can_be_read_for_serialization
23+
klass = Class.new(ActiveModelSerializers::Model) do
24+
attributes :one, :two, :three
25+
end
26+
original_attributes = { one: 1, two: 2, three: 3 }
27+
instance = klass.new(original_attributes)
28+
29+
# Initial value
30+
expected_attributes = { id: nil, one: 1, two: 2, three: 3 }.with_indifferent_access
31+
assert_equal expected_attributes, instance.attributes
32+
assert_equal 1, instance.one
33+
assert_equal 1, instance.read_attribute_for_serialization(:one)
34+
35+
# Change via accessor
36+
instance.one = :not_one
37+
38+
expected_attributes = { id: nil, one: :not_one, two: 2, three: 3 }.with_indifferent_access
39+
assert_equal expected_attributes, instance.attributes
40+
assert_equal :not_one, instance.one
41+
assert_equal :not_one, instance.read_attribute_for_serialization(:one)
42+
end
43+
44+
def test_id_attribute_can_be_read_for_serialization
45+
klass = Class.new(ActiveModelSerializers::Model) do
46+
attributes :id, :one, :two, :three
47+
end
48+
self.class.const_set(:SomeTestModel, klass)
49+
original_attributes = { id: :ego, one: 1, two: 2, three: 3 }
50+
instance = klass.new(original_attributes)
51+
52+
# Initial value
53+
expected_attributes = { id: :ego, one: 1, two: 2, three: 3 }.with_indifferent_access
54+
assert_equal expected_attributes, instance.attributes
55+
assert_equal 1, instance.one
56+
assert_equal 1, instance.read_attribute_for_serialization(:one)
57+
58+
# Change via accessor
59+
instance.id = :superego
60+
61+
expected_attributes = { id: :superego, one: 1, two: 2, three: 3 }.with_indifferent_access
62+
assert_equal expected_attributes, instance.attributes
63+
assert_equal :superego, instance.id
64+
assert_equal :superego, instance.read_attribute_for_serialization(:id)
65+
ensure
66+
self.class.send(:remove_const, :SomeTestModel)
67+
end
2168
end
2269
end

0 commit comments

Comments
 (0)