mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Merge pull request #639 from gauthier-delacroix/Unsuffixed-default-associations-keys
Allow JSONAPI unsuffixed associations keys
This commit is contained in:
commit
5acd98e61b
12
README.md
12
README.md
@ -247,6 +247,18 @@ end
|
|||||||
BlogSerializer.new(object, key_format: :lower_camel)
|
BlogSerializer.new(object, key_format: :lower_camel)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Changing the default association key type
|
||||||
|
|
||||||
|
You can specify that serializers use unsuffixed names as association keys by default.
|
||||||
|
|
||||||
|
`````ruby
|
||||||
|
ActiveModel::Serializer.setup do |config|
|
||||||
|
config.default_key_type = :name
|
||||||
|
end
|
||||||
|
````
|
||||||
|
|
||||||
|
This will build association keys like `comments` or `author` instead of `comment_ids` or `author_id`.
|
||||||
|
|
||||||
## Getting the old version
|
## Getting the old version
|
||||||
|
|
||||||
If you find that your project is already relying on the old rails to_json
|
If you find that your project is already relying on the old rails to_json
|
||||||
|
|||||||
@ -5,7 +5,11 @@ module ActiveModel
|
|||||||
def initialize(name, *args)
|
def initialize(name, *args)
|
||||||
super
|
super
|
||||||
@root_key = @embedded_key
|
@root_key = @embedded_key
|
||||||
@key ||= "#{name.to_s.singularize}_ids"
|
@key ||= case CONFIG.default_key_type
|
||||||
|
when :name then name.to_s.pluralize
|
||||||
|
else "#{name.to_s.singularize}_ids"
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializer_class(object, _)
|
def serializer_class(object, _)
|
||||||
|
|||||||
@ -5,7 +5,10 @@ module ActiveModel
|
|||||||
def initialize(name, *args)
|
def initialize(name, *args)
|
||||||
super
|
super
|
||||||
@root_key = @embedded_key.to_s.pluralize
|
@root_key = @embedded_key.to_s.pluralize
|
||||||
@key ||= "#{name}_id"
|
@key ||= case CONFIG.default_key_type
|
||||||
|
when :name then name.to_s.singularize
|
||||||
|
else "#{name}_id"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializer_class(object, options = {})
|
def serializer_class(object, options = {})
|
||||||
|
|||||||
22
test/fixtures/poro.rb
vendored
22
test/fixtures/poro.rb
vendored
@ -148,3 +148,25 @@ module TestNamespace
|
|||||||
class ProfileSerializer < ::ProfileSerializer; end
|
class ProfileSerializer < ::ProfileSerializer; end
|
||||||
class UserSerializer < ::UserSerializer; end
|
class UserSerializer < ::UserSerializer; end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ActiveModel::Serializer.setup do |config|
|
||||||
|
config.default_key_type = :name
|
||||||
|
end
|
||||||
|
|
||||||
|
class NameKeyUserSerializer < ActiveModel::Serializer
|
||||||
|
attributes :name, :email
|
||||||
|
|
||||||
|
has_one :profile
|
||||||
|
end
|
||||||
|
|
||||||
|
class NameKeyPostSerializer < ActiveModel::Serializer
|
||||||
|
attributes :title, :body
|
||||||
|
|
||||||
|
has_many :comments
|
||||||
|
end
|
||||||
|
|
||||||
|
ActiveModel::Serializer.setup do |config|
|
||||||
|
config.default_key_type = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -242,6 +242,24 @@ module ActiveModel
|
|||||||
}
|
}
|
||||||
}, @post_serializer.as_json)
|
}, @post_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_associations_name_key_embedding_ids_serialization_using_serializable_hash
|
||||||
|
@association = NameKeyPostSerializer._associations[:comments]
|
||||||
|
@association.embed = :ids
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id }
|
||||||
|
}, NameKeyPostSerializer.new(@post).serializable_hash)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_associations_name_key_embedding_ids_serialization_using_as_json
|
||||||
|
@association = NameKeyPostSerializer._associations[:comments]
|
||||||
|
@association.embed = :ids
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
'name_key_post' => { title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id } }
|
||||||
|
}, NameKeyPostSerializer.new(@post).as_json)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -216,6 +216,24 @@ module ActiveModel
|
|||||||
}
|
}
|
||||||
}, @user_serializer.as_json)
|
}, @user_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_associations_name_key_embedding_ids_serialization_using_serializable_hash
|
||||||
|
@association = NameKeyUserSerializer._associations[:profile]
|
||||||
|
@association.embed = :ids
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
name: 'Name 1', email: 'mail@server.com', 'profile' => @user.profile.object_id
|
||||||
|
}, NameKeyUserSerializer.new(@user).serializable_hash)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_associations_name_key_embedding_ids_serialization_using_as_json
|
||||||
|
@association = NameKeyUserSerializer._associations[:profile]
|
||||||
|
@association.embed = :ids
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
'name_key_user' => { name: 'Name 1', email: 'mail@server.com', 'profile' => @user.profile.object_id }
|
||||||
|
}, NameKeyUserSerializer.new(@user).as_json)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user