mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Merge remote-tracking branch 'upstream/master' into fix-jsonapi-ri
This commit is contained in:
@@ -20,7 +20,6 @@ module ActiveModel
|
||||
adapter = ActiveModel::Serializer.adapter
|
||||
assert_equal ActiveModel::Serializer::Adapter::Null, adapter
|
||||
ensure
|
||||
|
||||
end
|
||||
|
||||
def test_overwrite_adapter_with_class
|
||||
|
||||
@@ -6,7 +6,7 @@ module ActiveModel
|
||||
AuthorSummarySerializer = Class.new
|
||||
class AssociationsTestSerializer < Serializer
|
||||
belongs_to :author, serializer: AuthorSummarySerializer
|
||||
has_many :comments, embed: :ids
|
||||
has_many :comments
|
||||
has_one :category
|
||||
end
|
||||
|
||||
@@ -21,7 +21,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_has_many_defines_reflection
|
||||
has_many_reflection = HasManyReflection.new(:comments, embed: :ids)
|
||||
has_many_reflection = HasManyReflection.new(:comments, {})
|
||||
|
||||
assert_includes(@reflections, has_many_reflection)
|
||||
end
|
||||
|
||||
@@ -4,7 +4,7 @@ module ActiveModel
|
||||
class Serializer
|
||||
class AssociationsTest < Minitest::Test
|
||||
class Model
|
||||
def initialize(hash={})
|
||||
def initialize(hash = {})
|
||||
@attributes = hash
|
||||
end
|
||||
|
||||
@@ -29,7 +29,7 @@ module ActiveModel
|
||||
@author.roles = []
|
||||
@blog = Blog.new({ name: 'AMS Blog' })
|
||||
@post = Post.new({ title: 'New Post', body: 'Body' })
|
||||
@tag = Tag.new({name: '#hashtagged'})
|
||||
@tag = Tag.new({ name: '#hashtagged' })
|
||||
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
||||
@post.comments = [@comment]
|
||||
@post.tags = [@tag]
|
||||
@@ -39,7 +39,7 @@ module ActiveModel
|
||||
@post.author = @author
|
||||
@author.posts = [@post]
|
||||
|
||||
@post_serializer = PostSerializer.new(@post, {custom_options: true})
|
||||
@post_serializer = PostSerializer.new(@post, { custom_options: true })
|
||||
@author_serializer = AuthorSerializer.new(@author)
|
||||
@comment_serializer = CommentSerializer.new(@comment)
|
||||
end
|
||||
@@ -52,13 +52,13 @@ module ActiveModel
|
||||
|
||||
case key
|
||||
when :posts
|
||||
assert_equal({ embed: :ids }, options)
|
||||
assert_equal({}, options)
|
||||
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
|
||||
when :bio
|
||||
assert_equal({}, options)
|
||||
assert_nil serializer
|
||||
when :roles
|
||||
assert_equal({ embed: :ids }, options)
|
||||
assert_equal({}, options)
|
||||
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
|
||||
else
|
||||
flunk "Unknown association: #{key}"
|
||||
@@ -74,7 +74,7 @@ module ActiveModel
|
||||
|
||||
assert_equal key, :tags
|
||||
assert_equal serializer, nil
|
||||
assert_equal [{ attributes: { name: "#hashtagged" }}].to_json, options[:virtual_value].to_json
|
||||
assert_equal [{ attributes: { name: '#hashtagged' } }].to_json, options[:virtual_value].to_json
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -4,25 +4,25 @@ module ActiveModel
|
||||
class Serializer
|
||||
class AttributeTest < Minitest::Test
|
||||
def setup
|
||||
@blog = Blog.new({ id: 1, name: 'AMS Hints', type: "stuff" })
|
||||
@blog = Blog.new({ id: 1, name: 'AMS Hints', type: 'stuff' })
|
||||
@blog_serializer = AlternateBlogSerializer.new(@blog)
|
||||
end
|
||||
|
||||
def test_attributes_definition
|
||||
assert_equal([:id, :title],
|
||||
@blog_serializer.class._attributes)
|
||||
@blog_serializer.class._attributes)
|
||||
end
|
||||
|
||||
def test_json_serializable_hash
|
||||
adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer)
|
||||
assert_equal({blog: { id:1, title:"AMS Hints"}}, adapter.serializable_hash)
|
||||
assert_equal({ blog: { id: 1, title: 'AMS Hints' } }, adapter.serializable_hash)
|
||||
end
|
||||
|
||||
def test_attribute_inheritance_with_key
|
||||
inherited_klass = Class.new(AlternateBlogSerializer)
|
||||
blog_serializer = inherited_klass.new(@blog)
|
||||
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(blog_serializer)
|
||||
assert_equal({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash)
|
||||
assert_equal({ :id => 1, :title => 'AMS Hints' }, adapter.serializable_hash)
|
||||
end
|
||||
|
||||
def test_multiple_calls_with_the_same_attribute
|
||||
@@ -40,7 +40,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer.new(@blog))
|
||||
assert_equal({ blog: { id: "AMS Hints" } }, adapter.serializable_hash)
|
||||
assert_equal({ blog: { id: 'AMS Hints' } }, adapter.serializable_hash)
|
||||
end
|
||||
|
||||
def test_type_attribute
|
||||
@@ -52,10 +52,10 @@ module ActiveModel
|
||||
end
|
||||
|
||||
adapter = ActiveModel::Serializer::Adapter::Json.new(attribute_serializer.new(@blog))
|
||||
assert_equal({ blog: { type: 1} }, adapter.serializable_hash)
|
||||
assert_equal({ blog: { type: 1 } }, adapter.serializable_hash)
|
||||
|
||||
adapter = ActiveModel::Serializer::Adapter::Json.new(attributes_serializer.new(@blog))
|
||||
assert_equal({ blog: { type: "stuff" } }, adapter.serializable_hash)
|
||||
assert_equal({ blog: { type: 'stuff' } }, adapter.serializable_hash)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,7 +6,7 @@ module ActiveModel
|
||||
def setup
|
||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
@profile_serializer = ProfileSerializer.new(@profile)
|
||||
@comment = Comment.new(id: 1, body: "ZOMG!!", date: "2015")
|
||||
@comment = Comment.new(id: 1, body: 'ZOMG!!', date: '2015')
|
||||
@serializer_klass = Class.new(CommentSerializer)
|
||||
@serializer_klass_with_new_attributes = Class.new(CommentSerializer) do
|
||||
attributes :date, :likes
|
||||
@@ -15,12 +15,12 @@ module ActiveModel
|
||||
|
||||
def test_attributes_definition
|
||||
assert_equal([:name, :description],
|
||||
@profile_serializer.class._attributes)
|
||||
@profile_serializer.class._attributes)
|
||||
end
|
||||
|
||||
def test_attributes_with_fields_option
|
||||
assert_equal({name: 'Name 1'},
|
||||
@profile_serializer.attributes(fields: [:name]))
|
||||
assert_equal({ name: 'Name 1' },
|
||||
@profile_serializer.attributes(fields: [:name]))
|
||||
end
|
||||
|
||||
def test_attributes_inheritance_definition
|
||||
@@ -29,8 +29,8 @@ module ActiveModel
|
||||
|
||||
def test_attributes_inheritance
|
||||
serializer = @serializer_klass.new(@comment)
|
||||
assert_equal({id: 1, body: "ZOMG!!"},
|
||||
serializer.attributes)
|
||||
assert_equal({ id: 1, body: 'ZOMG!!' },
|
||||
serializer.attributes)
|
||||
end
|
||||
|
||||
def test_attribute_inheritance_with_new_attribute_definition
|
||||
@@ -40,8 +40,8 @@ module ActiveModel
|
||||
|
||||
def test_attribute_inheritance_with_new_attribute
|
||||
serializer = @serializer_klass_with_new_attributes.new(@comment)
|
||||
assert_equal({id: 1, body: "ZOMG!!", date: "2015", likes: nil},
|
||||
serializer.attributes)
|
||||
assert_equal({ id: 1, body: 'ZOMG!!', date: '2015', likes: nil },
|
||||
serializer.attributes)
|
||||
end
|
||||
|
||||
def test_multiple_calls_with_the_same_attribute
|
||||
|
||||
@@ -9,7 +9,7 @@ module ActiveModel
|
||||
@post = Post.new(title: 'New Post', body: 'Body')
|
||||
@bio = Bio.new(id: 1, content: 'AMS Contributor')
|
||||
@author = Author.new(name: 'Joao M. D. Moura')
|
||||
@blog = Blog.new(id: 999, name: "Custom blog", writer: @author, articles: [])
|
||||
@blog = Blog.new(id: 999, name: 'Custom blog', writer: @author, articles: [])
|
||||
@role = Role.new(name: 'Great Author')
|
||||
@location = Location.new(lat: '-23.550520', lng: '-46.633309')
|
||||
@place = Place.new(name: 'Amazing Place')
|
||||
@@ -58,9 +58,9 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_cache_options_definition
|
||||
assert_equal({expires_in: 0.1, skip_digest: true}, @post_serializer.class._cache_options)
|
||||
assert_equal({ expires_in: 0.1, skip_digest: true }, @post_serializer.class._cache_options)
|
||||
assert_equal(nil, @blog_serializer.class._cache_options)
|
||||
assert_equal({expires_in: 1.day, skip_digest: true}, @comment_serializer.class._cache_options)
|
||||
assert_equal({ expires_in: 1.day, skip_digest: true }, @comment_serializer.class._cache_options)
|
||||
end
|
||||
|
||||
def test_fragment_cache_definition
|
||||
@@ -118,7 +118,7 @@ module ActiveModel
|
||||
hash = render_object_with_cache(@location)
|
||||
|
||||
assert_equal(hash, expected_result)
|
||||
assert_equal({place: 'Nowhere'}, ActionController::Base.cache_store.fetch(@location.cache_key))
|
||||
assert_equal({ place: 'Nowhere' }, ActionController::Base.cache_store.fetch(@location.cache_key))
|
||||
end
|
||||
|
||||
def test_uses_file_digest_in_cache_key
|
||||
@@ -131,20 +131,20 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_serializer_file_path_on_nix
|
||||
path = "/Users/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb"
|
||||
path = '/Users/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb'
|
||||
caller_line = "#{path}:1:in `<top (required)>'"
|
||||
assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
|
||||
end
|
||||
|
||||
def test_serializer_file_path_on_windows
|
||||
path = "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb"
|
||||
path = 'c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb'
|
||||
caller_line = "#{path}:1:in `<top (required)>'"
|
||||
assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
|
||||
end
|
||||
|
||||
def test_digest_caller_file
|
||||
contents = "puts 'AMS rocks'!"
|
||||
file = Tempfile.new("some_ruby.rb")
|
||||
file = Tempfile.new('some_ruby.rb')
|
||||
file.write(contents)
|
||||
path = file.path
|
||||
caller_line = "#{path}:1:in `<top (required)>'"
|
||||
@@ -155,6 +155,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def render_object_with_cache(obj)
|
||||
ActiveModel::SerializableResource.new(obj).serializable_hash
|
||||
end
|
||||
|
||||
@@ -3,12 +3,11 @@ require 'test_helper'
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class FieldsetTest < Minitest::Test
|
||||
|
||||
def test_fieldset_with_hash
|
||||
fieldset = ActiveModel::Serializer::Fieldset.new({'post' => ['id', 'title'], 'coment' => ['body']})
|
||||
fieldset = ActiveModel::Serializer::Fieldset.new({ 'post' => %w(id title), 'coment' => ['body'] })
|
||||
|
||||
assert_equal(
|
||||
{:post=>[:id, :title], :coment=>[:body]},
|
||||
{ :post => [:id, :title], :coment => [:body] },
|
||||
fieldset.fields
|
||||
)
|
||||
end
|
||||
@@ -17,7 +16,7 @@ module ActiveModel
|
||||
fieldset = ActiveModel::Serializer::Fieldset.new(['title'], 'post')
|
||||
|
||||
assert_equal(
|
||||
{:post => [:title]},
|
||||
{ :post => [:title] },
|
||||
fieldset.fields
|
||||
)
|
||||
end
|
||||
|
||||
@@ -7,19 +7,19 @@ module ActiveModel
|
||||
ActionController::Base.cache_store.clear
|
||||
@blog = Blog.new(id: 1,
|
||||
name: 'AMS Hints',
|
||||
writer: Author.new(id: 2, name: "Steve"),
|
||||
articles: [Post.new(id: 3, title: "AMS")])
|
||||
writer: Author.new(id: 2, name: 'Steve'),
|
||||
articles: [Post.new(id: 3, title: 'AMS')])
|
||||
end
|
||||
|
||||
def test_meta_is_present_with_root
|
||||
serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10})
|
||||
serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 })
|
||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||
expected = {
|
||||
blog: {
|
||||
id: 1,
|
||||
title: "AMS Hints"
|
||||
title: 'AMS Hints'
|
||||
},
|
||||
"meta" => {
|
||||
'meta' => {
|
||||
total: 10
|
||||
}
|
||||
}
|
||||
@@ -28,23 +28,23 @@ module ActiveModel
|
||||
|
||||
def test_meta_is_not_included_when_root_is_missing
|
||||
# load_adapter uses FlattenJson Adapter
|
||||
adapter = load_adapter(meta: {total: 10})
|
||||
adapter = load_adapter(meta: { total: 10 })
|
||||
expected = {
|
||||
id: 1,
|
||||
title: "AMS Hints"
|
||||
title: 'AMS Hints'
|
||||
}
|
||||
assert_equal expected, adapter.as_json
|
||||
end
|
||||
|
||||
def test_meta_key_is_used
|
||||
serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10}, meta_key: "haha_meta")
|
||||
serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 }, meta_key: 'haha_meta')
|
||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||
expected = {
|
||||
blog: {
|
||||
id: 1,
|
||||
title: "AMS Hints"
|
||||
title: 'AMS Hints'
|
||||
},
|
||||
"haha_meta" => {
|
||||
'haha_meta' => {
|
||||
total: 10
|
||||
}
|
||||
}
|
||||
@@ -52,33 +52,33 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta_key_is_used_with_json_api
|
||||
serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10}, meta_key: "haha_meta")
|
||||
serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 }, meta_key: 'haha_meta')
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||
expected = {
|
||||
data: {
|
||||
id: "1",
|
||||
type: "blogs",
|
||||
attributes: { title: "AMS Hints" }
|
||||
id: '1',
|
||||
type: 'blogs',
|
||||
attributes: { title: 'AMS Hints' }
|
||||
},
|
||||
"haha_meta" => { total: 10 }
|
||||
'haha_meta' => { total: 10 }
|
||||
}
|
||||
assert_equal expected, adapter.as_json
|
||||
end
|
||||
|
||||
def test_meta_is_not_present_on_arrays_without_root
|
||||
serializer = ArraySerializer.new([@blog], meta: {total: 10})
|
||||
serializer = ArraySerializer.new([@blog], meta: { total: 10 })
|
||||
# FlattenJSON doesn't have support to root
|
||||
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(serializer)
|
||||
expected = [{
|
||||
id: 1,
|
||||
name: "AMS Hints",
|
||||
name: 'AMS Hints',
|
||||
writer: {
|
||||
id: 2,
|
||||
name: "Steve"
|
||||
name: 'Steve'
|
||||
},
|
||||
articles: [{
|
||||
id: 3,
|
||||
title: "AMS",
|
||||
title: 'AMS',
|
||||
body: nil
|
||||
}]
|
||||
}]
|
||||
@@ -86,20 +86,20 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta_is_present_on_arrays_with_root
|
||||
serializer = ArraySerializer.new([@blog], meta: {total: 10}, meta_key: "haha_meta")
|
||||
serializer = ArraySerializer.new([@blog], meta: { total: 10 }, meta_key: 'haha_meta')
|
||||
# JSON adapter adds root by default
|
||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||
expected = {
|
||||
blogs: [{
|
||||
id: 1,
|
||||
name: "AMS Hints",
|
||||
name: 'AMS Hints',
|
||||
writer: {
|
||||
id: 2,
|
||||
name: "Steve"
|
||||
name: 'Steve'
|
||||
},
|
||||
articles: [{
|
||||
id: 3,
|
||||
title: "AMS",
|
||||
title: 'AMS',
|
||||
body: nil
|
||||
}]
|
||||
}],
|
||||
|
||||
@@ -3,13 +3,12 @@ require 'test_helper'
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class RootTest < Minitest::Test
|
||||
|
||||
def setup
|
||||
@virtual_value = VirtualValue.new(id: 1)
|
||||
end
|
||||
|
||||
def test_overwrite_root
|
||||
serializer = VirtualValueSerializer.new(@virtual_value, {root: 'smth'})
|
||||
serializer = VirtualValueSerializer.new(@virtual_value, { root: 'smth' })
|
||||
assert_equal('smth', serializer.json_key)
|
||||
end
|
||||
|
||||
@@ -17,7 +16,6 @@ module ActiveModel
|
||||
serializer = VirtualValueSerializer.new(@virtual_value)
|
||||
assert_equal('virtual_value', serializer.json_key)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -26,7 +26,7 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
class SerializerTest < Minitest::Test
|
||||
class SerializerTest < Minitest::Test
|
||||
class MyProfile < Profile
|
||||
end
|
||||
class CustomProfile
|
||||
|
||||
@@ -3,7 +3,6 @@ require 'test_helper'
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class UrlsTest < Minitest::Test
|
||||
|
||||
def setup
|
||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
@post = Post.new({ title: 'New Post', body: 'Body' })
|
||||
|
||||
Reference in New Issue
Block a user