Skip to content

Commit

Permalink
return nil if id is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
InteNs committed Jun 11, 2019
1 parent 4b0c230 commit 28a172e
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def data_for_one(association)
else
association.reflection.type.to_s
end
# TODO: probably return nil if association.object is nil?
ResourceIdentifier.for_type_with_id(type, id, serializable_resource_options)
else
# TODO(BF): Process relationship without evaluating lazy_association
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def self.for_type_with_id(type, id, options)
type = inflect_type(type)
type = type_for(:no_class_needed, type, options)
if id.blank?
{ type: type }
nil
else
{ id: id.to_s, type: type }
end
Expand Down
4 changes: 4 additions & 0 deletions test/adapter/json_api/relationship_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def test_relationship_with_nil_model
end

def test_relationship_with_nil_model_and_belongs_to_id_on_self
original_config = ActiveModelSerializers.config.jsonapi_use_foreign_key_on_belongs_to_relationship
ActiveModelSerializers.config.jsonapi_use_foreign_key_on_belongs_to_relationship = true

expected = { data: nil }
Expand All @@ -46,7 +47,10 @@ def test_relationship_with_nil_model_and_belongs_to_id_on_self
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
belongs_to :blog
end

assert_equal(expected, actual)
ensure
ActiveModelSerializers.config.jsonapi_use_foreign_key_on_belongs_to_relationship = original_config
end

def test_relationship_with_data_array
Expand Down
3 changes: 1 addition & 2 deletions test/adapter/json_api/type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ def test_for_type_with_id
def test_for_type_with_id_given_blank_id
id = ''
actual = ResourceIdentifier.for_type_with_id('admin_user', id, {})
expected = { type: 'admin-users' }
assert_equal actual, expected
assert_nil actual
end

def test_for_type_with_id_inflected
Expand Down

0 comments on commit 28a172e

Please sign in to comment.