Skip to content

Commit

Permalink
Prevent loading association when include_data is set to false
Browse files Browse the repository at this point in the history
This should fix #1707.
  • Loading branch information
Yohan Robert committed May 24, 2016
1 parent cbca135 commit bcbadc9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Breaking changes:
Features:

Fixes:
- [#1710](https://github.com/rails-api/active_model_serializers/pull/1710) Prevent association loading when `include_data` option
is set to `false`. (@groyoh)

Misc:
- [#1734](https://github.com/rails-api/active_model_serializers/pull/1734) Adds documentation for conditional attribute (@lambda2)
Expand Down
6 changes: 3 additions & 3 deletions lib/active_model/serializer/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ def value(serializer)

if block
block_value = instance_exec(serializer, &block)
if block_value == :nil
serializer.read_attribute_for_serialization(name)
else
if block_value != :nil
block_value
elsif @_include_data
serializer.read_attribute_for_serialization(name)
end
else
serializer.read_attribute_for_serialization(name)
Expand Down
13 changes: 9 additions & 4 deletions test/adapter/json_api/relationships_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class Serializer
module Adapter
class JsonApi
class RelationshipTest < ActiveSupport::TestCase
RelationshipAuthor = Class.new(::Model)
class RelationshipAuthor < ::Model; end

class RelationshipAuthorSerializer < ActiveModel::Serializer
has_one :bio do
link :self, '//example.com/link_author/relationships/bio'
Expand Down Expand Up @@ -71,7 +72,6 @@ def cached_roles

def setup
@post = Post.new(id: 1337, comments: [], author: nil)
@blog = Blog.new(id: 1337, name: 'extra')
@bio = Bio.new(id: 1337)
@like = Like.new(id: 1337)
@role = Role.new(id: 'from-record')
Expand All @@ -82,7 +82,6 @@ def setup
@author = RelationshipAuthor.new(
id: 1337,
posts: [@post],
blog: @blog,
reviewer: @reviewer,
bio: @bio,
likes: [@like],
Expand Down Expand Up @@ -158,10 +157,16 @@ def test_relationship_meta
end

def test_relationship_not_including_data
@author.define_singleton_method(:read_attribute_for_serialization) do |attr|
fail 'should not be called' if attr == :blog
super(attr)
end
expected = {
links: { self: '//example.com/link_author/relationships/blog' }
}
assert_relationship(:blog, expected)
assert_nothing_raised do
assert_relationship(:blog, expected)
end
end

def test_relationship_including_data_explicit
Expand Down

0 comments on commit bcbadc9

Please sign in to comment.