Skip to content

Commit df63b59

Browse files
committed
Add key option to serializer associations
1 parent ac1991f commit df63b59

File tree

8 files changed

+109
-29
lines changed

8 files changed

+109
-29
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@ You may also use the `:serializer` option to specify a custom serializer class,
264264
has_many :comments, serializer: CommentPreviewSerializer
265265
```
266266

267+
And you can change the JSON key that the serializer should use for a particular association:
268+
269+
```ruby
270+
has_many :comments, key: :reviews
271+
```
272+
267273
The `url` declaration describes which named routes to use while generating URLs
268274
for your JSON. Not every adapter will require URLs.
269275

lib/active_model/serializer.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,9 @@ def each_association(&block)
220220
association_options[:association_options][:virtual_value] = association_value
221221
end
222222

223+
association_key = association_options[:association_options][:key] || name
223224
if block_given?
224-
block.call(name, serializer, association_options[:association_options])
225+
block.call(association_key, serializer, association_options[:association_options])
225226
end
226227
end
227228
end

lib/active_model/serializer/adapter/json.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ def serializable_hash(options = nil)
1515
serializer.attributes(options)
1616
end
1717

18-
serializer.each_association do |name, association, opts|
18+
serializer.each_association do |key, association, opts|
1919
if association.respond_to?(:each)
2020
array_serializer = association
21-
@hash[name] = array_serializer.map do |item|
21+
@hash[key] = array_serializer.map do |item|
2222
cache_check(item) do
2323
item.attributes(opts)
2424
end
2525
end
2626
else
2727
if association && association.object
28-
@hash[name] = cache_check(association) do
28+
@hash[key] = cache_check(association) do
2929
association.attributes(options)
3030
end
3131
elsif opts[:virtual_value]
32-
@hash[name] = opts[:virtual_value]
32+
@hash[key] = opts[:virtual_value]
3333
else
34-
@hash[name] = nil
34+
@hash[key] = nil
3535
end
3636
end
3737
end

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def add_included(resource_name, serializers, parent = nil)
7575
end
7676

7777
serializers.each do |serializer|
78-
serializer.each_association do |name, association, opts|
79-
add_included(name, association, resource_path) if association
78+
serializer.each_association do |key, association, opts|
79+
add_included(key, association, resource_path) if association
8080
end if include_nested_assoc? resource_path
8181
end
8282
end
@@ -131,22 +131,22 @@ def check_assoc(assoc)
131131
def add_resource_relationships(attrs, serializer, options = {})
132132
options[:add_included] = options.fetch(:add_included, true)
133133

134-
serializer.each_association do |name, association, opts|
134+
serializer.each_association do |key, association, opts|
135135
attrs[:relationships] ||= {}
136136

137137
if association.respond_to?(:each)
138-
add_relationships(attrs, name, association)
138+
add_relationships(attrs, key, association)
139139
else
140140
if opts[:virtual_value]
141-
add_relationship(attrs, name, nil, opts[:virtual_value])
141+
add_relationship(attrs, key, nil, opts[:virtual_value])
142142
else
143-
add_relationship(attrs, name, association)
143+
add_relationship(attrs, key, association)
144144
end
145145
end
146146

147147
if options[:add_included]
148148
Array(association).each do |association|
149-
add_included(name, association)
149+
add_included(key, association)
150150
end
151151
end
152152
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'test_helper'
2+
3+
module ActiveModel
4+
class Serializer
5+
class Adapter
6+
class JsonApiTest < Minitest::Test
7+
def setup
8+
ActionController::Base.cache_store.clear
9+
@author = Author.new(id: 1, name: 'Steve K.')
10+
@post = Post.new(id: 1, title: 'New Post', body: 'Body')
11+
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
12+
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
13+
@post.comments = [@first_comment, @second_comment]
14+
@first_comment.post = @post
15+
@second_comment.post = @post
16+
@post.author = @author
17+
@blog = Blog.new(id: 1, name: "My Blog!!")
18+
@post.blog = @blog
19+
20+
end
21+
22+
def test_custom_keys
23+
serializer = PostWithCustomKeysSerializer.new(@post)
24+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
25+
26+
assert_equal({
27+
reviews: { data: [
28+
{type: "comments", id: "1"},
29+
{type: "comments", id: "2"}
30+
]},
31+
writer: { data: {type: "authors", id: "1"} },
32+
site: { data: {type: "blogs", id: "1" } }
33+
}, adapter.serializable_hash[:data][:relationships])
34+
end
35+
end
36+
end
37+
end
38+
end

test/adapter/json_test.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class JsonTest < Minitest::Test
77
def setup
88
ActionController::Base.cache_store.clear
99
@author = Author.new(id: 1, name: 'Steve K.')
10-
@post = Post.new(title: 'New Post', body: 'Body')
10+
@post = Post.new(id: 1, title: 'New Post', body: 'Body')
1111
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
1212
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
1313
@post.comments = [@first_comment, @second_comment]
@@ -27,6 +27,20 @@ def test_has_many
2727
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
2828
], @adapter.serializable_hash[:post][:comments])
2929
end
30+
31+
def test_custom_keys
32+
serializer = PostWithCustomKeysSerializer.new(@post)
33+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
34+
35+
assert_equal({
36+
id: 1,
37+
reviews: [{id: 1, body: "ZOMG A COMMENT"},
38+
{id: 2, body: "ZOMG ANOTHER COMMENT"}
39+
],
40+
writer: {id: 1, name: "Steve K."},
41+
site: {id: 1, name: "My Blog!!"}
42+
}, adapter.serializable_hash[:post_with_custom_keys])
43+
end
3044
end
3145
end
3246
end

test/fixtures/poro.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,14 @@ def self.root_name
228228
has_many :tags
229229
end
230230

231+
PostWithCustomKeysSerializer = Class.new(ActiveModel::Serializer) do
232+
attributes :id
233+
234+
has_many :comments, key: :reviews
235+
belongs_to :author, key: :writer
236+
has_one :blog, key: :site
237+
end
238+
231239
VirtualValueSerializer = Class.new(ActiveModel::Serializer) do
232240
attributes :id
233241

test/serializers/associations_test.rb

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,33 @@ def test_has_many_and_has_one
5151
bio: { type: :has_one, association_options: {} } },
5252
@author_serializer.class._associations
5353
)
54-
@author_serializer.each_association do |name, serializer, options|
55-
if name == :posts
54+
@author_serializer.each_association do |key, serializer, options|
55+
if key == :posts
5656
assert_equal({embed: :ids}, options)
5757
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
58-
elsif name == :bio
58+
elsif key == :bio
5959
assert_equal({}, options)
6060
assert_nil serializer
61-
elsif name == :roles
61+
elsif key == :roles
6262
assert_equal({embed: :ids}, options)
6363
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
6464
else
65-
flunk "Unknown association: #{name}"
65+
flunk "Unknown association: #{key}"
6666
end
6767
end
6868
end
6969

7070
def test_has_many_with_no_serializer
71-
PostWithTagsSerializer.new(@post).each_association do |name, serializer, options|
72-
assert_equal name, :tags
71+
PostWithTagsSerializer.new(@post).each_association do |key, serializer, options|
72+
assert_equal key, :tags
7373
assert_equal serializer, nil
7474
assert_equal [{ attributes: { name: "#hashtagged" }}].to_json, options[:virtual_value].to_json
7575
end
7676
end
7777

7878
def test_serializer_options_are_passed_into_associations_serializers
79-
@post_serializer.each_association do |name, association|
80-
if name == :comments
79+
@post_serializer.each_association do |key, association|
80+
if key == :comments
8181
assert association.first.custom_options[:custom_options]
8282
end
8383
end
@@ -89,24 +89,24 @@ def test_belongs_to
8989
author: { type: :belongs_to, association_options: {} } },
9090
@comment_serializer.class._associations
9191
)
92-
@comment_serializer.each_association do |name, serializer, options|
93-
if name == :post
92+
@comment_serializer.each_association do |key, serializer, options|
93+
if key == :post
9494
assert_equal({}, options)
9595
assert_kind_of(PostSerializer, serializer)
96-
elsif name == :author
96+
elsif key == :author
9797
assert_equal({}, options)
9898
assert_nil serializer
9999
else
100-
flunk "Unknown association: #{name}"
100+
flunk "Unknown association: #{key}"
101101
end
102102
end
103103
end
104104

105105
def test_belongs_to_with_custom_method
106106
blog_is_present = false
107107

108-
@post_serializer.each_association do |name, serializer, options|
109-
blog_is_present = true if name == :blog
108+
@post_serializer.each_association do |key, serializer, options|
109+
blog_is_present = true if key == :blog
110110
end
111111

112112
assert blog_is_present
@@ -132,6 +132,19 @@ def test_associations_inheritance_with_new_association
132132
)
133133
assert_equal(inherited_klass._associations, expected_associations)
134134
end
135+
136+
def test_associations_custom_keys
137+
serializer = PostWithCustomKeysSerializer.new(@post)
138+
139+
expected_association_keys = []
140+
serializer.each_association do |key, serializer, options|
141+
expected_association_keys << key
142+
end
143+
144+
assert expected_association_keys.include? :reviews
145+
assert expected_association_keys.include? :writer
146+
assert expected_association_keys.include? :site
147+
end
135148
end
136149
end
137150
end

0 commit comments

Comments
 (0)