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

[WIP] Add config option to globally format JSONAPI type. #1122

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ def fragment_cache(cached_hash, non_cached_hash)
private

def resource_identifier_type_for(serializer)
if ActiveModel::Serializer.config.jsonapi_resource_type == :singular
serializer.object.class.model_name.singular
if serializer.respond_to?(:type)
serializer.type
else
serializer.object.class.model_name.plural
type = serializer.object.class.model_name.singular
ActiveModel::Serializer.config.jsonapi_type_formatter.call(type)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/active_model/serializer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Configuration
included do |base|
base.config.array_serializer = ActiveModel::Serializer::ArraySerializer
base.config.adapter = :flatten_json
base.config.jsonapi_resource_type = :plural
base.config.jsonapi_type_formatter = -> (type) { type.pluralize }
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions test/adapter/json_api/json_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ def test_custom_keys
site: { data: { type: 'blogs', id: '1' } }
}, adapter.serializable_hash[:data][:relationships])
end

def test_override_type
post_override_serializer = Class.new(PostSerializer) do
def type
'override'
end
end
hash = ActiveModel::SerializableResource.new(@post, adapter: :json_api, serializer: post_override_serializer).serializable_hash

assert_equal('override', hash[:data][:type])
end
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions test/adapter/json_api/resource_type_config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ def setup
@author.posts = []
end

def with_jsonapi_resource_type type
old_type = ActiveModel::Serializer.config.jsonapi_resource_type
ActiveModel::Serializer.config.jsonapi_resource_type = type
def with_jsonapi_type_formatter formatter
old_formatter = ActiveModel::Serializer.config.jsonapi_type_formatter
ActiveModel::Serializer.config.jsonapi_type_formatter = formatter
yield
ensure
ActiveModel::Serializer.config.jsonapi_resource_type = old_type
ActiveModel::Serializer.config.jsonapi_type_formatter = old_formatter
end

def test_config_plural
with_adapter :json_api do
with_jsonapi_resource_type :plural do
with_jsonapi_type_formatter -> (type) { type.pluralize } do
hash = ActiveModel::SerializableResource.new(@comment).serializable_hash
assert_equal('comments', hash[:data][:type])
end
Expand All @@ -46,7 +46,7 @@ def test_config_plural

def test_config_singular
with_adapter :json_api do
with_jsonapi_resource_type :singular do
with_jsonapi_type_formatter -> (type) { type.singularize } do
hash = ActiveModel::SerializableResource.new(@comment).serializable_hash
assert_equal('comment', hash[:data][:type])
end
Expand Down