Skip to content

Commit 5acd98e

Browse files
committed
Merge pull request #639 from gauthier-delacroix/Unsuffixed-default-associations-keys
Allow JSONAPI unsuffixed associations keys
2 parents 06e3bb7 + a39d08d commit 5acd98e

File tree

6 files changed

+79
-2
lines changed

6 files changed

+79
-2
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,18 @@ end
247247
BlogSerializer.new(object, key_format: :lower_camel)
248248
```
249249

250+
## Changing the default association key type
251+
252+
You can specify that serializers use unsuffixed names as association keys by default.
253+
254+
`````ruby
255+
ActiveModel::Serializer.setup do |config|
256+
config.default_key_type = :name
257+
end
258+
````
259+
260+
This will build association keys like `comments` or `author` instead of `comment_ids` or `author_id`.
261+
250262
## Getting the old version
251263

252264
If you find that your project is already relying on the old rails to_json

lib/active_model/serializer/association/has_many.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ class HasMany < Association
55
def initialize(name, *args)
66
super
77
@root_key = @embedded_key
8-
@key ||= "#{name.to_s.singularize}_ids"
8+
@key ||= case CONFIG.default_key_type
9+
when :name then name.to_s.pluralize
10+
else "#{name.to_s.singularize}_ids"
11+
end
12+
913
end
1014

1115
def serializer_class(object, _)

lib/active_model/serializer/association/has_one.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ class HasOne < Association
55
def initialize(name, *args)
66
super
77
@root_key = @embedded_key.to_s.pluralize
8-
@key ||= "#{name}_id"
8+
@key ||= case CONFIG.default_key_type
9+
when :name then name.to_s.singularize
10+
else "#{name}_id"
11+
end
912
end
1013

1114
def serializer_class(object, options = {})

test/fixtures/poro.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,25 @@ module TestNamespace
148148
class ProfileSerializer < ::ProfileSerializer; end
149149
class UserSerializer < ::UserSerializer; end
150150
end
151+
152+
ActiveModel::Serializer.setup do |config|
153+
config.default_key_type = :name
154+
end
155+
156+
class NameKeyUserSerializer < ActiveModel::Serializer
157+
attributes :name, :email
158+
159+
has_one :profile
160+
end
161+
162+
class NameKeyPostSerializer < ActiveModel::Serializer
163+
attributes :title, :body
164+
165+
has_many :comments
166+
end
167+
168+
ActiveModel::Serializer.setup do |config|
169+
config.default_key_type = nil
170+
end
171+
172+

test/unit/active_model/serializer/has_many_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,24 @@ def test_associations_embedding_objects_using_embed_namespace
242242
}
243243
}, @post_serializer.as_json)
244244
end
245+
246+
def test_associations_name_key_embedding_ids_serialization_using_serializable_hash
247+
@association = NameKeyPostSerializer._associations[:comments]
248+
@association.embed = :ids
249+
250+
assert_equal({
251+
title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id }
252+
}, NameKeyPostSerializer.new(@post).serializable_hash)
253+
end
254+
255+
def test_associations_name_key_embedding_ids_serialization_using_as_json
256+
@association = NameKeyPostSerializer._associations[:comments]
257+
@association.embed = :ids
258+
259+
assert_equal({
260+
'name_key_post' => { title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id } }
261+
}, NameKeyPostSerializer.new(@post).as_json)
262+
end
245263
end
246264
end
247265
end

test/unit/active_model/serializer/has_one_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,24 @@ def test_associations_embedding_ids_using_embed_namespace_and_embed_in_root_key
216216
}
217217
}, @user_serializer.as_json)
218218
end
219+
220+
def test_associations_name_key_embedding_ids_serialization_using_serializable_hash
221+
@association = NameKeyUserSerializer._associations[:profile]
222+
@association.embed = :ids
223+
224+
assert_equal({
225+
name: 'Name 1', email: '[email protected]', 'profile' => @user.profile.object_id
226+
}, NameKeyUserSerializer.new(@user).serializable_hash)
227+
end
228+
229+
def test_associations_name_key_embedding_ids_serialization_using_as_json
230+
@association = NameKeyUserSerializer._associations[:profile]
231+
@association.embed = :ids
232+
233+
assert_equal({
234+
'name_key_user' => { name: 'Name 1', email: '[email protected]', 'profile' => @user.profile.object_id }
235+
}, NameKeyUserSerializer.new(@user).as_json)
236+
end
219237
end
220238
end
221239
end

0 commit comments

Comments
 (0)