Skip to content

Commit

Permalink
Add symbol support for ActiveModel::Serializer.type method
Browse files Browse the repository at this point in the history
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 15, 2016
1 parent 2b67363 commit a0583f1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 72 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model/serializer/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module ClassMethods
# class AdminAuthorSerializer < ActiveModel::Serializer
# type 'authors'
def type(type)
self._type = type
self._type = type ? type.to_s : nil
end
end
end
Expand Down
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
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

0 comments on commit a0583f1

Please sign in to comment.