Skip to content

Commit 6ae54ee

Browse files
committed
More associations
1 parent 8cdf0c2 commit 6ae54ee

File tree

12 files changed

+119
-78
lines changed

12 files changed

+119
-78
lines changed

test/action_controller/json_api/fields_test.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@ module Serialization
55
class JsonApi
66
class FieldsTest < ActionController::TestCase
77
class FieldsTestController < ActionController::Base
8-
class PostSerializer < ActiveModel::Serializer
8+
class AuthorWithName < Author
9+
attributes :first_name, :last_name
10+
end
11+
class AuthorWithNameSerializer < AuthorSerializer
12+
type 'authors'
13+
end
14+
class PostWithPublishAt < Post
15+
attributes :publish_at
16+
end
17+
class PostWithPublishAtSerializer < ActiveModel::Serializer
918
type 'posts'
1019
attributes :title, :body, :publish_at
1120
belongs_to :author
@@ -14,19 +23,19 @@ class PostSerializer < ActiveModel::Serializer
1423

1524
def setup_post
1625
ActionController::Base.cache_store.clear
17-
@author = Author.new(id: 1, first_name: 'Bob', last_name: 'Jones')
26+
@author = AuthorWithName.new(id: 1, first_name: 'Bob', last_name: 'Jones')
1827
@comment1 = Comment.new(id: 7, body: 'cool', author: @author)
1928
@comment2 = Comment.new(id: 12, body: 'awesome', author: @author)
20-
@post = Post.new(id: 1337, title: 'Title 1', body: 'Body 1',
21-
author: @author, comments: [@comment1, @comment2],
22-
publish_at: '2020-03-16T03:55:25.291Z')
29+
@post = PostWithPublishAt.new(id: 1337, title: 'Title 1', body: 'Body 1',
30+
author: @author, comments: [@comment1, @comment2],
31+
publish_at: '2020-03-16T03:55:25.291Z')
2332
@comment1.post = @post
2433
@comment2.post = @post
2534
end
2635

2736
def render_fields_works_on_relationships
2837
setup_post
29-
render json: @post, serializer: PostSerializer, adapter: :json_api, fields: { posts: [:author] }
38+
render json: @post, serializer: PostWithPublishAtSerializer, adapter: :json_api, fields: { posts: [:author] }
3039
end
3140
end
3241

test/action_controller/json_api/transform_test.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ module Serialization
55
class JsonApi
66
class KeyTransformTest < ActionController::TestCase
77
class KeyTransformTestController < ActionController::Base
8-
class Post < ::Model; attributes :title, :body, :author, :top_comments, :publish_at end
9-
class Author < ::Model; attributes :first_name, :last_name end
10-
class TopComment < ::Model; attributes :body, :author, :post end
8+
class Post < ::Model
9+
attributes :title, :body, :publish_at
10+
associations :author, :top_comments
11+
end
12+
class Author < ::Model
13+
attributes :first_name, :last_name
14+
end
15+
class TopComment < ::Model
16+
attributes :body
17+
associations :author, :post
18+
end
1119
class PostSerializer < ActiveModel::Serializer
1220
type 'posts'
1321
attributes :title, :body, :publish_at

test/action_controller/namespace_lookup_test.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33
module ActionController
44
module Serialization
55
class NamespaceLookupTest < ActionController::TestCase
6-
class Book < ::Model; attributes :title, :body, :writer, :chapters end
7-
class Chapter < ::Model; attributes :title end
8-
class Writer < ::Model; attributes :name end
6+
class Book < ::Model
7+
attributes :title, :body
8+
associations :writer, :chapters
9+
end
10+
class Chapter < ::Model
11+
attributes :title
12+
end
13+
class Writer < ::Model
14+
attributes :name
15+
end
916

1017
module Api
1118
module V2
@@ -204,7 +211,7 @@ def namespace_set_by_request_headers
204211

205212
assert_serializer ActiveModel::Serializer::Null
206213

207-
expected = { 'id' => 'invalid_namespace_book_id', 'title' => 'New Post', 'body' => 'Body', 'writer' => nil, 'chapters' => nil }
214+
expected = { 'id' => 'invalid_namespace_book_id', 'title' => 'New Post', 'body' => 'Body' }
208215
actual = JSON.parse(@response.body)
209216

210217
assert_equal expected, actual

test/action_controller/serialization_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def render_fragment_changed_object_with_relationship
135135
like = Like.new(id: 1, likeable: comment, time: 3.days.ago)
136136

137137
generate_cached_serializer(like)
138-
like.likable = comment2
138+
like.likeable = comment2
139139
like.time = Time.zone.now.to_s
140140

141141
render json: like

test/adapter/json_api/fields_test.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@ module ActiveModelSerializers
44
module Adapter
55
class JsonApi
66
class FieldsTest < ActiveSupport::TestCase
7-
class Post < ::Model; attributes :title, :body, :author, :comments end
8-
class Author < ::Model; attributes :name, :birthday end
9-
class Comment < ::Model; attributes :body, :author, :post end
7+
class Post < ::Model
8+
attributes :title, :body
9+
associations :author, :comments
10+
end
11+
class Author < ::Model
12+
attributes :name, :birthday
13+
end
14+
class Comment < ::Model
15+
attributes :body
16+
associations :author, :post
17+
end
1018

1119
class PostSerializer < ActiveModel::Serializer
1220
type 'posts'

test/adapter/json_api/include_data_if_sideloaded_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Adapter
66
class JsonApi
77
class IncludeParamTest < ActiveSupport::TestCase
88
IncludeParamAuthor = Class.new(::Model) do
9-
attributes :tags, :posts
9+
associations :tags, :posts
1010
end
1111

1212
class CustomCommentLoader

test/adapter/json_api/linked_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class NestedPost < ::Model; attributes :nested_posts end
3+
class NestedPost < ::Model; associations :nested_posts end
44
class NestedPostSerializer < ActiveModel::Serializer
55
has_many :nested_posts
66
end
@@ -301,8 +301,8 @@ def test_nil_link_with_specified_serializer
301301
end
302302

303303
class NoDuplicatesTest < ActiveSupport::TestCase
304-
class Post < ::Model; attributes :author end
305-
class Author < ::Model; attributes :posts, :roles, :bio end
304+
class Post < ::Model; associations :author end
305+
class Author < ::Model; associations :posts, :roles, :bio end
306306

307307
class PostSerializer < ActiveModel::Serializer
308308
type 'posts'

test/adapter/json_api/links_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module ActiveModelSerializers
44
module Adapter
55
class JsonApi
66
class LinksTest < ActiveSupport::TestCase
7-
class LinkAuthor < ::Model; attributes :posts end
7+
class LinkAuthor < ::Model; associations :posts end
88
class LinkAuthorSerializer < ActiveModel::Serializer
99
link :self do
1010
href "http://example.com/link_author/#{object.id}"

test/adapter/json_api/transform_test.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@ module ActiveModelSerializers
44
module Adapter
55
class JsonApi
66
class KeyCaseTest < ActiveSupport::TestCase
7-
class Post < ::Model; attributes :title, :body, :publish_at, :author, :comments end
8-
class Author < ::Model; attributes :first_name, :last_name end
9-
class Comment < ::Model; attributes :body, :author, :post end
7+
class Post < ::Model
8+
attributes :title, :body, :publish_at
9+
associations :author, :comments
10+
end
11+
class Author < ::Model
12+
attributes :first_name, :last_name
13+
end
14+
class Comment < ::Model
15+
attributes :body
16+
associations :author, :post
17+
end
1018

1119
class PostSerializer < ActiveModel::Serializer
1220
type 'posts'

test/cache_test.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class InheritedRoleSerializer < RoleSerializer
5050
end
5151

5252
class Comment < ::Model
53-
attributes :body, :post, :author
53+
attributes :body
54+
associations :post, :author
5455
end
5556

5657
setup do
@@ -364,12 +365,12 @@ def test_fetch_attributes_from_cache
364365
manual_cached_attributes = ActiveModel::Serializer.cache_read_multi(serializers, adapter_instance, include_directive).with_indifferent_access
365366
assert_equal manual_cached_attributes, cached_attributes
366367

367-
assert_equal cached_attributes["#{@comment.cache_key}/#{adapter_instance.cache_key}"], Comment.new(id: 1, body: 'ZOMG A COMMENT').attributes.reject { |_, v| v.nil? }
368-
assert_equal cached_attributes["#{@comment.post.cache_key}/#{adapter_instance.cache_key}"], Post.new(id: 'post', title: 'New Post', body: 'Body').attributes.reject { |_, v| v.nil? }
368+
assert_equal cached_attributes["#{@comment.cache_key}/#{adapter_instance.cache_key}"], Comment.new(id: 1, body: 'ZOMG A COMMENT').attributes
369+
assert_equal cached_attributes["#{@comment.post.cache_key}/#{adapter_instance.cache_key}"], Post.new(id: 'post', title: 'New Post', body: 'Body').attributes
369370

370371
writer = @comment.post.blog.writer
371372
writer_cache_key = writer.cache_key
372-
assert_equal cached_attributes["#{writer_cache_key}/#{adapter_instance.cache_key}"], Author.new(id: 'author', name: 'Joao M. D. Moura').attributes.reject { |_, v| v.nil? }
373+
assert_equal cached_attributes["#{writer_cache_key}/#{adapter_instance.cache_key}"], Author.new(id: 'author', name: 'Joao M. D. Moura').attributes
373374
end
374375
end
375376
# rubocop:enable Metrics/AbcSize

0 commit comments

Comments
 (0)