Skip to content

Commit

Permalink
Fixes #1211 - retrieve the key from the reflection options when build…
Browse files Browse the repository at this point in the history
…ing associations

fixes #1211

adjust where the reflection key/name comes from, and update test accordingly"

remove try from fix. It was a little misloading, but the key of a reflection comes from the reflection's options

update changelog

add qualifying asserts to cehck if the keys were appropriately set

update assert and changelog

I literally don't even
  • Loading branch information
NullVoxPopuli committed Oct 1, 2015
1 parent ff87ac8 commit fc20908
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Features:
associations for JSON and Attributes adapters via the `include` option (@NullVoxPopuli, @beauby).

Fixes:
- [#1214](https://github.com/rails-api/active_model_serializers/pull/1214) retrieve the key from the reflection options when building associations (@NullVoxPopuli, @hut8)

Misc:
- [#1178](https://github.com/rails-api/active_model_serializers/pull/1178) env CAPTURE_STDERR=false lets devs see hard failures (@bf4)
Expand Down
3 changes: 2 additions & 1 deletion lib/active_model/serializer/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def associations(include_tree = DEFAULT_INCLUDE_TREE)

Enumerator.new do |y|
self.class._reflections.each do |reflection|
next unless include_tree.key?(reflection.name)
key = reflection.options.fetch(:key, reflection.name)
next unless include_tree.key?(key)
y.yield reflection.build_association(self, instance_options)
end
end
Expand Down
8 changes: 7 additions & 1 deletion test/action_controller/json_api/linked_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def render_resource_with_include

def render_resource_with_include_of_custom_key_by_original
setup_post
render json: @post, include: [:comments], adapter: :json_api, serializer: PostWithCustomKeysSerializer
render json: @post, include: [:reviews], adapter: :json_api, serializer: PostWithCustomKeysSerializer
end

def render_resource_with_nested_include
Expand Down Expand Up @@ -146,6 +146,12 @@ def test_render_resource_with_include_of_custom_key_by_original
get :render_resource_with_include_of_custom_key_by_original
response = JSON.parse(@response.body)
assert response.key? 'included'

relationships = response['data']['relationships']

assert_includes relationships, 'reviews'
assert_includes relationships, 'writer'
assert_includes relationships, 'site'
end

def test_render_resource_with_nested_include
Expand Down

0 comments on commit fc20908

Please sign in to comment.