Skip to content
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

Add symbol support for Serializer.type method #1515

Merged
merged 1 commit into from
Feb 21, 2016
Merged
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@
Breaking changes:

Features:
- [#1515](https://github.com/rails-api/active_model_serializers/pull/1515) Adds support for symbols to the
`ActiveModel::Serializer.type` method. (@groyoh)
- [#1504](https://github.com/rails-api/active_model_serializers/pull/1504) Adds the changes missing from #1454
and add more tests for resource identifier and relationship objects. Fix association block with link
returning `data: nil`.(@groyoh)
20 changes: 19 additions & 1 deletion docs/general/serializers.md
Original file line number Diff line number Diff line change
@@ -107,12 +107,30 @@ end

#### ::type

e.g.
The `::type` method defines the JSONAPI [type](http://jsonapi.org/format/#document-resource-object-identification) that will be rendered for this serializer.
It either takes a `String` or `Symbol` as parameter.

Note: This method is useful only when using the `:json_api` adapter.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though, TODO: it should replace the json_key method and root option..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an open PR for this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I recall


Examples:
```ruby
class UserProfileSerializer < ActiveModel::Serializer
type 'profile'
end
class AuthorProfileSerializer < ActiveModel::Serializer
type :profile
end
```

With the `:json_api` adapter, the previous serializers would be rendered as:

``` json
{
"data": {
"id": "1",
"type": "profile"
}
}
```

#### ::link
2 changes: 1 addition & 1 deletion lib/active_model/serializer/type.rb
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ module ClassMethods
# class AdminAuthorSerializer < ActiveModel::Serializer
# type 'authors'
def type(type)
self._type = type
self._type = type && type.to_s
end
end
end
71 changes: 0 additions & 71 deletions test/adapter/json_api/resource_type_config_test.rb

This file was deleted.

61 changes: 61 additions & 0 deletions test/adapter/json_api/type_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'test_helper'

module ActiveModel
class Serializer
module Adapter
class JsonApi
class TypeTest < ActiveSupport::TestCase
class StringTypeSerializer < ActiveModel::Serializer
attribute :name
type 'profile'
end

class SymbolTypeSerializer < ActiveModel::Serializer
attribute :name
type :profile
end

setup do
@author = Author.new(id: 1, name: 'Steve K.')
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to use setup do @author = Author.new(id: 1, name: 'Steve K.') end to avoid the need to call super (which is lacking here)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the info. I never use ActiveSupport::TestCase so I did not know.


def test_config_plural
with_jsonapi_resource_type :plural do
assert_type(@author, 'authors')
end
end

def test_config_singular
with_jsonapi_resource_type :singular do
assert_type(@author, 'author')
end
end

def test_explicit_string_type_value
assert_type(@author, 'profile', serializer: StringTypeSerializer)
end

def test_explicit_symbol_type_value
assert_type(@author, 'profile', serializer: SymbolTypeSerializer)
end

private

def assert_type(resource, expected_type, opts = {})
opts = opts.reverse_merge(adapter: :json_api)
hash = serializable(resource, opts).serializable_hash
assert_equal(expected_type, hash.fetch(:data).fetch(:type))
end

def with_jsonapi_resource_type inflection
old_inflection = ActiveModelSerializers.config.jsonapi_resource_type
ActiveModelSerializers.config.jsonapi_resource_type = inflection
yield
ensure
ActiveModelSerializers.config.jsonapi_resource_type = old_inflection
end
end
end
end
end
end