-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add symbol support for ActiveModel::Serializer.type method
The ActiveModel::Serializer.type method now accepts symbol as paremeter: class AuthorSerializer < ActiveModel::Serializer type :profile end The test file for the type was also refactored.
- Loading branch information
Yohan Robert
authored and
Yohan Robert
committed
Feb 17, 2016
1 parent
2b67363
commit e1bdbbf
Showing
5 changed files
with
83 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
attributes :name | ||
type 'profile' | ||
end | ||
|
||
class SymbolTypeSerializer < ActiveModel::Serializer | ||
attributes :name | ||
type :profile | ||
end | ||
|
||
def setup | ||
@author = Author.new(id: 1, name: 'Steve K.') | ||
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 | ||
|
||
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 | ||
end | ||
end | ||
end | ||
end | ||
end |