Skip to content

Commit f00d678

Browse files
committed
More associations
1 parent 8cdf0c2 commit f00d678

File tree

12 files changed

+84
-71
lines changed

12 files changed

+84
-71
lines changed

test/action_controller/json_api/fields_test.rb

Lines changed: 13 additions & 4 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,10 +23,10 @@ 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',
29+
@post = PostWithPublishAt.new(id: 1337, title: 'Title 1', body: 'Body 1',
2130
author: @author, comments: [@comment1, @comment2],
2231
publish_at: '2020-03-16T03:55:25.291Z')
2332
@comment1.post = @post
@@ -26,7 +35,7 @@ def setup_post
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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ 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
8+
class Post < ::Model
9+
attributes :title, :body, :publish_at
10+
associations :author, :top_comments
11+
end
912
class Author < ::Model; attributes :first_name, :last_name end
10-
class TopComment < ::Model; attributes :body, :author, :post end
13+
class TopComment < ::Model; attributes :body; associations :author, :post end
1114
class PostSerializer < ActiveModel::Serializer
1215
type 'posts'
1316
attributes :title, :body, :publish_at

test/action_controller/namespace_lookup_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module ActionController
44
module Serialization
55
class NamespaceLookupTest < ActionController::TestCase
6-
class Book < ::Model; attributes :title, :body, :writer, :chapters end
6+
class Book < ::Model; attributes :title, :body; associations :writer, :chapters end
77
class Chapter < ::Model; attributes :title end
88
class Writer < ::Model; attributes :name end
99

@@ -204,7 +204,7 @@ def namespace_set_by_request_headers
204204

205205
assert_serializer ActiveModel::Serializer::Null
206206

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

210210
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ module ActiveModelSerializers
44
module Adapter
55
class JsonApi
66
class FieldsTest < ActiveSupport::TestCase
7-
class Post < ::Model; attributes :title, :body, :author, :comments end
7+
class Post < ::Model; attributes :title, :body; associations :author, :comments end
88
class Author < ::Model; attributes :name, :birthday end
9-
class Comment < ::Model; attributes :body, :author, :post end
9+
class Comment < ::Model; attributes :body; associations :author, :post end
1010

1111
class PostSerializer < ActiveModel::Serializer
1212
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ 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
7+
class Post < ::Model; attributes :title, :body, :publish_at; associations :author, :comments end
88
class Author < ::Model; attributes :first_name, :last_name end
9-
class Comment < ::Model; attributes :body, :author, :post end
9+
class Comment < ::Model; attributes :body; associations :author, :post end
1010

1111
class PostSerializer < ActiveModel::Serializer
1212
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)