Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/active_model_serializers/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Model
attr_reader :attributes, :errors

def initialize(attributes = {})
@attributes = attributes
@attributes = attributes.with_indifferent_access
@errors = ActiveModel::Errors.new(self)
super
end
Expand All @@ -30,7 +30,7 @@ def updated_at
end

def read_attribute_for_serialization(key)
if key == :id || key == 'id'
if key == :id
attributes.fetch(key) { id }
else
attributes[key]
Expand Down
6 changes: 4 additions & 2 deletions test/action_controller/adapter_selector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def render_skipping_adapter

def test_render_using_default_adapter
get :render_using_default_adapter
assert_equal '{"name":"Name 1","description":"Description 1"}', response.body
expected = '{"name":"Name 1","description":"Description 1"}'
assert_equal expected, response.body
end

def test_render_using_adapter_override
Expand All @@ -46,7 +47,8 @@ def test_render_using_adapter_override

def test_render_skipping_adapter
get :render_skipping_adapter
assert_equal '{"name":"Name 1","description":"Description 1","comments":"Comments 1"}', response.body
expected = '{"name":"Name 1","description":"Description 1","comments":"Comments 1"}'
assert_equal expected, response.body
end
end
end
Expand Down
14 changes: 9 additions & 5 deletions test/cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,9 @@ def test_a_serializer_rendered_by_two_adapter_returns_differently_fetch_attribut
# Assert attributes are serialized correctly
serializable_alert = serializable(alert, serializer: AlertSerializer, adapter: :attributes)
attributes_serialization = serializable_alert.as_json
assert_equal expected_fetch_attributes, alert.attributes
assert_equal alert.attributes, attributes_serialization
alert_attributes_symbolized = alert.attributes.symbolize_keys
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see the value of adding a test for symbolizing, but it looks like you're replacing the existing one?

assert_equal expected_fetch_attributes, alert_attributes_symbolized
assert_equal alert_attributes_symbolized, attributes_serialization
attributes_cache_key = serializable_alert.adapter.serializer.cache_key(serializable_alert.adapter)
assert_equal attributes_serialization, cache_store.fetch(attributes_cache_key)

Expand Down Expand Up @@ -312,12 +313,15 @@ def test_fetch_attributes_from_cache
manual_cached_attributes = ActiveModel::Serializer.cache_read_multi(serializers, adapter_instance, include_directive)
assert_equal manual_cached_attributes, cached_attributes

assert_equal cached_attributes["#{@comment.cache_key}/#{adapter_instance.cache_key}"], Comment.new(id: 1, body: 'ZOMG A COMMENT').attributes
assert_equal cached_attributes["#{@comment.post.cache_key}/#{adapter_instance.cache_key}"], Post.new(id: 'post', title: 'New Post', body: 'Body').attributes
expected_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT').attributes.symbolize_keys
assert_equal cached_attributes["#{@comment.cache_key}/#{adapter_instance.cache_key}"], expected_comment
expected_post = Post.new(id: 'post', title: 'New Post', body: 'Body').attributes.symbolize_keys
assert_equal cached_attributes["#{@comment.post.cache_key}/#{adapter_instance.cache_key}"], expected_post

writer = @comment.post.blog.writer
writer_cache_key = writer.cache_key
assert_equal cached_attributes["#{writer_cache_key}/#{adapter_instance.cache_key}"], Author.new(id: 'author', name: 'Joao M. D. Moura').attributes
expected_attributes = Author.new(id: 'author', name: 'Joao M. D. Moura').attributes.symbolize_keys
assert_equal cached_attributes["#{writer_cache_key}/#{adapter_instance.cache_key}"], expected_attributes
end
end

Expand Down