mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Make schema not crash on computed attributes
We do not know the type for computed attributes, so we pick nil. Perhaps at some point we might add a :type option for attributes (or not), but in any case it's important to not crash when there are computed attributes.
This commit is contained in:
parent
9f5e1b1776
commit
6281a9149e
@ -169,9 +169,15 @@ module ActiveModel
|
||||
klass = model_class
|
||||
columns = klass.columns_hash
|
||||
|
||||
attrs = _attributes.inject({}) do |hash, (name,key)|
|
||||
column = columns[name.to_s]
|
||||
hash.merge key => column.type
|
||||
attrs = {}
|
||||
_attributes.each do |name, key|
|
||||
if column = columns[name.to_s]
|
||||
attrs[key] = column.type
|
||||
else
|
||||
# Computed attribute (method on serializer or model). We cannot
|
||||
# infer the type, so we put nil.
|
||||
attrs[key] = nil
|
||||
end
|
||||
end
|
||||
|
||||
associations = _associations.inject({}) do |hash, (attr,association_class)|
|
||||
|
||||
@ -568,7 +568,7 @@ class SerializerTest < ActiveModel::TestCase
|
||||
|
||||
def test_associations_with_as
|
||||
posts = [
|
||||
Post.new(:title => 'First Post', :body => 'text'),
|
||||
Post.new(:title => 'First Post', :body => 'text'),
|
||||
Post.new(:title => 'Second Post', :body => 'text')
|
||||
]
|
||||
user = User.new
|
||||
@ -586,15 +586,15 @@ class SerializerTest < ActiveModel::TestCase
|
||||
{:title => 'Second Post', :body => 'text', :comments => []}
|
||||
],
|
||||
:user => {
|
||||
:first_name => "Jose",
|
||||
:last_name => "Valim", :ok => true,
|
||||
:first_name => "Jose",
|
||||
:last_name => "Valim", :ok => true,
|
||||
:scope => true
|
||||
}
|
||||
}
|
||||
}, serializer.as_json)
|
||||
end
|
||||
|
||||
def test_implicity_detection_for_association_serializers
|
||||
def test_implicity_detection_for_association_serializers
|
||||
implicit_serializer = Class.new(ActiveModel::Serializer) do
|
||||
root :custom_blog
|
||||
const_set(:UserSerializer, UserSerializer)
|
||||
@ -605,7 +605,7 @@ class SerializerTest < ActiveModel::TestCase
|
||||
end
|
||||
|
||||
posts = [
|
||||
Post.new(:title => 'First Post', :body => 'text', :comments => []),
|
||||
Post.new(:title => 'First Post', :body => 'text', :comments => []),
|
||||
Post.new(:title => 'Second Post', :body => 'text', :comments => [])
|
||||
]
|
||||
user = User.new
|
||||
@ -623,8 +623,8 @@ class SerializerTest < ActiveModel::TestCase
|
||||
{:title => 'Second Post', :body => 'text', :comments => []}
|
||||
],
|
||||
:user => {
|
||||
:first_name => "Jose",
|
||||
:last_name => "Valim", :ok => true,
|
||||
:first_name => "Jose",
|
||||
:last_name => "Valim", :ok => true,
|
||||
:scope => true
|
||||
}
|
||||
}
|
||||
@ -678,13 +678,16 @@ class SerializerTest < ActiveModel::TestCase
|
||||
define_method(:model_class) do model end
|
||||
end
|
||||
|
||||
attributes :name, :age
|
||||
# Computed attribute; not a column.
|
||||
def can_edit; end
|
||||
|
||||
attributes :name, :age, :can_edit
|
||||
has_many :posts, :serializer => Class.new
|
||||
has_one :parent, :serializer => Class.new
|
||||
end
|
||||
|
||||
assert_equal serializer.schema, {
|
||||
:attributes => { :name => :string, :age => :integer },
|
||||
:attributes => { :name => :string, :age => :integer, :can_edit => nil },
|
||||
:associations => {
|
||||
:posts => { :has_many => :posts },
|
||||
:parent => { :belongs_to => :parent }
|
||||
@ -862,7 +865,7 @@ class SerializerTest < ActiveModel::TestCase
|
||||
expected = serializer_class.new(post).as_json
|
||||
assert_equal expected, hash_object
|
||||
end
|
||||
|
||||
|
||||
def test_embed_ids_include_true_with_root
|
||||
serializer_class = post_serializer
|
||||
|
||||
@ -911,7 +914,7 @@ class SerializerTest < ActiveModel::TestCase
|
||||
:author => [{ :first_name => "Jose", :last_name => "Valim" }]
|
||||
}, serializer.as_json)
|
||||
end
|
||||
|
||||
|
||||
# the point of this test is to illustrate that deeply nested serializers
|
||||
# still side-load at the root.
|
||||
def test_embed_with_include_inserts_at_root
|
||||
@ -1123,7 +1126,7 @@ class SerializerTest < ActiveModel::TestCase
|
||||
actual = attachment_serializer.new(attachment, {}).as_json
|
||||
|
||||
assert_equal({
|
||||
:name => 'logo.png',
|
||||
:name => 'logo.png',
|
||||
:url => 'http://example.com/logo.png',
|
||||
:attachable => {
|
||||
:type => :email,
|
||||
@ -1160,7 +1163,7 @@ class SerializerTest < ActiveModel::TestCase
|
||||
actual = attachment_serializer.new(attachment, {}).as_json
|
||||
|
||||
assert_equal({
|
||||
:name => 'logo.png',
|
||||
:name => 'logo.png',
|
||||
:url => 'http://example.com/logo.png',
|
||||
:attachable => {
|
||||
:type => :email,
|
||||
@ -1199,7 +1202,7 @@ class SerializerTest < ActiveModel::TestCase
|
||||
|
||||
assert_equal({
|
||||
:attachment => {
|
||||
:name => 'logo.png',
|
||||
:name => 'logo.png',
|
||||
:url => 'http://example.com/logo.png',
|
||||
:attachable => {
|
||||
:type => :email,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user