Skip to content

Commit

Permalink
Merge pull request #1581 from remear/meta-blank-omit
Browse files Browse the repository at this point in the history
Omit meta when blank
  • Loading branch information
NullVoxPopuli committed Mar 14, 2016
2 parents 8cd7f0f + e8286b6 commit daabb89
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/active_model_serializers/adapter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def root
end

def include_meta(json)
json[meta_key] = meta if meta
json[meta_key] = meta unless meta.blank?
json
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model_serializers/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def resource_object_for(serializer)
# :'git-ref' => 'abc123'
# }
meta = meta_for(serializer)
resource_object[:meta] = meta unless meta.nil?
resource_object[:meta] = meta unless meta.blank?

resource_object
end
Expand Down
32 changes: 32 additions & 0 deletions test/adapter/json_api/resource_meta_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ class MetaBlockPostSerializer < ActiveModel::Serializer
end
end

class MetaBlockPostBlankMetaSerializer < ActiveModel::Serializer
attributes :id
meta do
{}
end
end

class MetaBlockPostEmptyStringSerializer < ActiveModel::Serializer
attributes :id
meta do
''
end
end

def setup
@post = Post.new(id: 1337, comments: [], author: nil)
end
Expand Down Expand Up @@ -61,6 +75,24 @@ def test_meta_object_resource_in_array
}
assert_equal(expected, hash)
end

def test_meta_object_blank_omitted
hash = ActiveModel::SerializableResource.new(
@post,
serializer: MetaBlockPostBlankMetaSerializer,
adapter: :json_api
).serializable_hash
refute hash[:data].key? :meta
end

def test_meta_object_empty_string_omitted
hash = ActiveModel::SerializableResource.new(
@post,
serializer: MetaBlockPostEmptyStringSerializer,
adapter: :json_api
).serializable_hash
refute hash[:data].key? :meta
end
end
end
end
Expand Down
68 changes: 68 additions & 0 deletions test/serializers/meta_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,38 @@ def test_meta_is_present_with_root
assert_equal(expected, actual)
end

def test_meta_is_not_included_when_blank
actual = ActiveModel::SerializableResource.new(
@blog,
adapter: :json,
serializer: AlternateBlogSerializer,
meta: {}
).as_json
expected = {
blog: {
id: 1,
title: 'AMS Hints'
}
}
assert_equal(expected, actual)
end

def test_meta_is_not_included_when_empty_string
actual = ActiveModel::SerializableResource.new(
@blog,
adapter: :json,
serializer: AlternateBlogSerializer,
meta: ''
).as_json
expected = {
blog: {
id: 1,
title: 'AMS Hints'
}
}
assert_equal(expected, actual)
end

def test_meta_is_not_included_when_root_is_missing
actual = ActiveModel::SerializableResource.new(
@blog,
Expand Down Expand Up @@ -78,6 +110,42 @@ def test_meta_key_is_used_with_json_api
assert_equal(expected, actual)
end

def test_meta_key_is_not_present_when_blank_object_with_json_api
actual = ActiveModel::SerializableResource.new(
@blog,
adapter: :json_api,
serializer: AlternateBlogSerializer,
meta: {},
meta_key: 'haha_meta'
).as_json
expected = {
data: {
id: '1',
type: 'blogs',
attributes: { title: 'AMS Hints' }
}
}
assert_equal(expected, actual)
end

def test_meta_key_is_not_present_when_empty_string_with_json_api
actual = ActiveModel::SerializableResource.new(
@blog,
adapter: :json_api,
serializer: AlternateBlogSerializer,
meta: '',
meta_key: 'haha_meta'
).as_json
expected = {
data: {
id: '1',
type: 'blogs',
attributes: { title: 'AMS Hints' }
}
}
assert_equal(expected, actual)
end

def test_meta_is_not_present_on_arrays_without_root
actual = ActiveModel::SerializableResource.new(
[@blog],
Expand Down

0 comments on commit daabb89

Please sign in to comment.