Skip to content

Implement setting for including belongs_to relationship resource iden… #2151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
2 changes: 1 addition & 1 deletion lib/active_model/serializer/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Serializer
# @api private
Association = Struct.new(:reflection, :association_options) do
attr_reader :lazy_association
delegate :object, :include_data?, :virtual_value, :collection?, to: :lazy_association
delegate :object, :include_data?, :include_resource_identifier?, :virtual_value, :collection?, to: :lazy_association

def initialize(*)
super
Expand Down
4 changes: 4 additions & 0 deletions lib/active_model/serializer/belongs_to_reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class BelongsToReflection < Reflection
def foreign_key_on
:self
end

def include_resource_identifier?
ActiveModelSerializers.config.include_belongs_to_resource_identifier == true
end
end
end
end
4 changes: 4 additions & 0 deletions lib/active_model/serializer/lazy_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def include_data?
)
end

def include_resource_identifier?
include_data? || reflection.include_resource_identifier?
end

# @return [ActiveModel::Serializer, nil]
def serializer
return @serializer if defined?(@serializer)
Expand Down
4 changes: 4 additions & 0 deletions lib/active_model/serializer/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ def include_data?(include_slice)
end
end

def include_resource_identifier?
false
end

# @param serializer [ActiveModel::Serializer]
# @yield [ActiveModel::Serializer]
# @return [:nil, associated resource or resource collection]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(parent_serializer, serializable_resource_options, association)
def as_json
hash = {}

hash[:data] = data_for(association) if association.include_data?
hash[:data] = data_for(association) if association.include_resource_identifier?

links = links_for(association)
hash[:links] = links if links.any?
Expand Down
18 changes: 18 additions & 0 deletions test/adapter/json_api/relationship_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,24 @@ def test_relationship_including_data_explicit
assert_equal(expected, actual)
end

def test_include_belongs_to_relationships
ActiveModelSerializers.config.include_belongs_to_resource_identifier = true

expected = {
data: { id: '1337', type: 'authors' }
}

model_attributes = { author: Author.new(id: 1337), author_id: 1337 }
relationship_name = :author
model = new_model(model_attributes)
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
belongs_to :author do
include_data false
end
end
assert_equal(expected, actual)
end

private

def build_serializer_and_serialize_relationship(model, relationship_name, &block)
Expand Down