-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the info. I never use |
||
|
||
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 |
There was a problem hiding this comment.
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 androot
option..There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that I recall