Skip to content

Commit

Permalink
More associations
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Nov 21, 2016
1 parent 8cdf0c2 commit 6ae54ee
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 78 deletions.
21 changes: 15 additions & 6 deletions test/action_controller/json_api/fields_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ module Serialization
class JsonApi
class FieldsTest < ActionController::TestCase
class FieldsTestController < ActionController::Base
class PostSerializer < ActiveModel::Serializer
class AuthorWithName < Author
attributes :first_name, :last_name
end
class AuthorWithNameSerializer < AuthorSerializer
type 'authors'
end
class PostWithPublishAt < Post
attributes :publish_at
end
class PostWithPublishAtSerializer < ActiveModel::Serializer
type 'posts'
attributes :title, :body, :publish_at
belongs_to :author
Expand All @@ -14,19 +23,19 @@ class PostSerializer < ActiveModel::Serializer

def setup_post
ActionController::Base.cache_store.clear
@author = Author.new(id: 1, first_name: 'Bob', last_name: 'Jones')
@author = AuthorWithName.new(id: 1, first_name: 'Bob', last_name: 'Jones')
@comment1 = Comment.new(id: 7, body: 'cool', author: @author)
@comment2 = Comment.new(id: 12, body: 'awesome', author: @author)
@post = Post.new(id: 1337, title: 'Title 1', body: 'Body 1',
author: @author, comments: [@comment1, @comment2],
publish_at: '2020-03-16T03:55:25.291Z')
@post = PostWithPublishAt.new(id: 1337, title: 'Title 1', body: 'Body 1',
author: @author, comments: [@comment1, @comment2],
publish_at: '2020-03-16T03:55:25.291Z')
@comment1.post = @post
@comment2.post = @post
end

def render_fields_works_on_relationships
setup_post
render json: @post, serializer: PostSerializer, adapter: :json_api, fields: { posts: [:author] }
render json: @post, serializer: PostWithPublishAtSerializer, adapter: :json_api, fields: { posts: [:author] }
end
end

Expand Down
14 changes: 11 additions & 3 deletions test/action_controller/json_api/transform_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ module Serialization
class JsonApi
class KeyTransformTest < ActionController::TestCase
class KeyTransformTestController < ActionController::Base
class Post < ::Model; attributes :title, :body, :author, :top_comments, :publish_at end
class Author < ::Model; attributes :first_name, :last_name end
class TopComment < ::Model; attributes :body, :author, :post end
class Post < ::Model
attributes :title, :body, :publish_at
associations :author, :top_comments
end
class Author < ::Model
attributes :first_name, :last_name
end
class TopComment < ::Model
attributes :body
associations :author, :post
end
class PostSerializer < ActiveModel::Serializer
type 'posts'
attributes :title, :body, :publish_at
Expand Down
15 changes: 11 additions & 4 deletions test/action_controller/namespace_lookup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
module ActionController
module Serialization
class NamespaceLookupTest < ActionController::TestCase
class Book < ::Model; attributes :title, :body, :writer, :chapters end
class Chapter < ::Model; attributes :title end
class Writer < ::Model; attributes :name end
class Book < ::Model
attributes :title, :body
associations :writer, :chapters
end
class Chapter < ::Model
attributes :title
end
class Writer < ::Model
attributes :name
end

module Api
module V2
Expand Down Expand Up @@ -204,7 +211,7 @@ def namespace_set_by_request_headers

assert_serializer ActiveModel::Serializer::Null

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

assert_equal expected, actual
Expand Down
2 changes: 1 addition & 1 deletion test/action_controller/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def render_fragment_changed_object_with_relationship
like = Like.new(id: 1, likeable: comment, time: 3.days.ago)

generate_cached_serializer(like)
like.likable = comment2
like.likeable = comment2
like.time = Time.zone.now.to_s

render json: like
Expand Down
14 changes: 11 additions & 3 deletions test/adapter/json_api/fields_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ module ActiveModelSerializers
module Adapter
class JsonApi
class FieldsTest < ActiveSupport::TestCase
class Post < ::Model; attributes :title, :body, :author, :comments end
class Author < ::Model; attributes :name, :birthday end
class Comment < ::Model; attributes :body, :author, :post end
class Post < ::Model
attributes :title, :body
associations :author, :comments
end
class Author < ::Model
attributes :name, :birthday
end
class Comment < ::Model
attributes :body
associations :author, :post
end

class PostSerializer < ActiveModel::Serializer
type 'posts'
Expand Down
2 changes: 1 addition & 1 deletion test/adapter/json_api/include_data_if_sideloaded_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Adapter
class JsonApi
class IncludeParamTest < ActiveSupport::TestCase
IncludeParamAuthor = Class.new(::Model) do
attributes :tags, :posts
associations :tags, :posts
end

class CustomCommentLoader
Expand Down
6 changes: 3 additions & 3 deletions test/adapter/json_api/linked_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class NestedPost < ::Model; attributes :nested_posts end
class NestedPost < ::Model; associations :nested_posts end
class NestedPostSerializer < ActiveModel::Serializer
has_many :nested_posts
end
Expand Down Expand Up @@ -301,8 +301,8 @@ def test_nil_link_with_specified_serializer
end

class NoDuplicatesTest < ActiveSupport::TestCase
class Post < ::Model; attributes :author end
class Author < ::Model; attributes :posts, :roles, :bio end
class Post < ::Model; associations :author end
class Author < ::Model; associations :posts, :roles, :bio end

class PostSerializer < ActiveModel::Serializer
type 'posts'
Expand Down
2 changes: 1 addition & 1 deletion test/adapter/json_api/links_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ActiveModelSerializers
module Adapter
class JsonApi
class LinksTest < ActiveSupport::TestCase
class LinkAuthor < ::Model; attributes :posts end
class LinkAuthor < ::Model; associations :posts end
class LinkAuthorSerializer < ActiveModel::Serializer
link :self do
href "http://example.com/link_author/#{object.id}"
Expand Down
14 changes: 11 additions & 3 deletions test/adapter/json_api/transform_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ module ActiveModelSerializers
module Adapter
class JsonApi
class KeyCaseTest < ActiveSupport::TestCase
class Post < ::Model; attributes :title, :body, :publish_at, :author, :comments end
class Author < ::Model; attributes :first_name, :last_name end
class Comment < ::Model; attributes :body, :author, :post end
class Post < ::Model
attributes :title, :body, :publish_at
associations :author, :comments
end
class Author < ::Model
attributes :first_name, :last_name
end
class Comment < ::Model
attributes :body
associations :author, :post
end

class PostSerializer < ActiveModel::Serializer
type 'posts'
Expand Down
9 changes: 5 additions & 4 deletions test/cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class InheritedRoleSerializer < RoleSerializer
end

class Comment < ::Model
attributes :body, :post, :author
attributes :body
associations :post, :author
end

setup do
Expand Down Expand Up @@ -364,12 +365,12 @@ def test_fetch_attributes_from_cache
manual_cached_attributes = ActiveModel::Serializer.cache_read_multi(serializers, adapter_instance, include_directive).with_indifferent_access
assert_equal manual_cached_attributes, cached_attributes

assert_equal cached_attributes["#{@comment.cache_key}/#{adapter_instance.cache_key}"], Comment.new(id: 1, body: 'ZOMG A COMMENT').attributes.reject { |_, v| v.nil? }
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? }
assert_equal cached_attributes["#{@comment.cache_key}/#{adapter_instance.cache_key}"], Comment.new(id: 1, body: 'ZOMG A COMMENT').attributes
assert_equal cached_attributes["#{@comment.post.cache_key}/#{adapter_instance.cache_key}"], Post.new(id: 'post', title: 'New Post', body: 'Body').attributes

writer = @comment.post.blog.writer
writer_cache_key = writer.cache_key
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? }
assert_equal cached_attributes["#{writer_cache_key}/#{adapter_instance.cache_key}"], Author.new(id: 'author', name: 'Joao M. D. Moura').attributes
end
end
# rubocop:enable Metrics/AbcSize
Expand Down
26 changes: 23 additions & 3 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,41 @@ class AuthorSerializer < ActiveModel::Serializer
has_many :posts
end
end

class Employee < ActiveRecord::Base
has_many :pictures, as: :imageable
has_many :object_tags, as: :taggable
end

class Picture < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
has_many :object_tags, as: :taggable
class PolymorphicSimpleSerializer < ActiveModel::Serializer
attributes :id
end

class ObjectTag < ActiveRecord::Base
belongs_to :poly_tag
belongs_to :taggable, polymorphic: true
end
class PolymorphicObjectTagSerializer < ActiveModel::Serializer
attributes :id
has_many :taggable, serializer: PolymorphicSimpleSerializer, polymorphic: true
end

class PolyTag < ActiveRecord::Base
has_many :object_tags
end
class PolymorphicTagSerializer < ActiveModel::Serializer
attributes :id, :phrase
has_many :object_tags, serializer: PolymorphicObjectTagSerializer
end

class Picture < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
has_many :object_tags, as: :taggable
end
class PolymorphicHasManySerializer < ActiveModel::Serializer
attributes :id, :name
end
class PolymorphicBelongsToSerializer < ActiveModel::Serializer
attributes :id, :title
has_one :imageable, serializer: PolymorphicHasManySerializer, polymorphic: true
end
Loading

0 comments on commit 6ae54ee

Please sign in to comment.