mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Merge pull request #117 from joliss/computed-attributes
Make schema not crash on computed attributes & associations
This commit is contained in:
commit
2fc083a1fe
@ -169,16 +169,31 @@ module ActiveModel
|
|||||||
klass = model_class
|
klass = model_class
|
||||||
columns = klass.columns_hash
|
columns = klass.columns_hash
|
||||||
|
|
||||||
attrs = _attributes.inject({}) do |hash, (name,key)|
|
attrs = {}
|
||||||
column = columns[name.to_s]
|
_attributes.each do |name, key|
|
||||||
hash.merge key => column.type
|
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
|
end
|
||||||
|
|
||||||
associations = _associations.inject({}) do |hash, (attr,association_class)|
|
associations = {}
|
||||||
|
_associations.each do |attr, association_class|
|
||||||
association = association_class.new(attr, self)
|
association = association_class.new(attr, self)
|
||||||
|
|
||||||
model_association = klass.reflect_on_association(association.name)
|
if model_association = klass.reflect_on_association(association.name)
|
||||||
hash.merge association.key => { model_association.macro => model_association.name }
|
# Real association.
|
||||||
|
associations[association.key] = { model_association.macro => model_association.name }
|
||||||
|
else
|
||||||
|
# Computed association. We could infer has_many vs. has_one from
|
||||||
|
# the association class, but that would make it different from
|
||||||
|
# real associations, which read has_one vs. belongs_to from the
|
||||||
|
# model.
|
||||||
|
associations[association.key] = nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
{ :attributes => attrs, :associations => associations }
|
{ :attributes => attrs, :associations => associations }
|
||||||
|
|||||||
@ -379,7 +379,7 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
|
|
||||||
def test_associations_with_as
|
def test_associations_with_as
|
||||||
posts = [
|
posts = [
|
||||||
Post.new(:title => 'First Post', :body => 'text'),
|
Post.new(:title => 'First Post', :body => 'text'),
|
||||||
Post.new(:title => 'Second Post', :body => 'text')
|
Post.new(:title => 'Second Post', :body => 'text')
|
||||||
]
|
]
|
||||||
user = User.new
|
user = User.new
|
||||||
@ -397,15 +397,15 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
{:title => 'Second Post', :body => 'text', :comments => []}
|
{:title => 'Second Post', :body => 'text', :comments => []}
|
||||||
],
|
],
|
||||||
:user => {
|
:user => {
|
||||||
:first_name => "Jose",
|
:first_name => "Jose",
|
||||||
:last_name => "Valim", :ok => true,
|
:last_name => "Valim", :ok => true,
|
||||||
:scope => true
|
:scope => true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, serializer.as_json)
|
}, serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_implicity_detection_for_association_serializers
|
def test_implicity_detection_for_association_serializers
|
||||||
implicit_serializer = Class.new(ActiveModel::Serializer) do
|
implicit_serializer = Class.new(ActiveModel::Serializer) do
|
||||||
root :custom_blog
|
root :custom_blog
|
||||||
const_set(:UserSerializer, UserSerializer)
|
const_set(:UserSerializer, UserSerializer)
|
||||||
@ -416,7 +416,7 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
posts = [
|
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 => [])
|
Post.new(:title => 'Second Post', :body => 'text', :comments => [])
|
||||||
]
|
]
|
||||||
user = User.new
|
user = User.new
|
||||||
@ -434,8 +434,8 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
{:title => 'Second Post', :body => 'text', :comments => []}
|
{:title => 'Second Post', :body => 'text', :comments => []}
|
||||||
],
|
],
|
||||||
:user => {
|
:user => {
|
||||||
:first_name => "Jose",
|
:first_name => "Jose",
|
||||||
:last_name => "Valim", :ok => true,
|
:last_name => "Valim", :ok => true,
|
||||||
:scope => true
|
:scope => true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -489,15 +489,21 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
define_method(:model_class) do model end
|
define_method(:model_class) do model end
|
||||||
end
|
end
|
||||||
|
|
||||||
attributes :name, :age
|
# Computed attributes (not real columns or associations).
|
||||||
|
def can_edit; end
|
||||||
|
def drafts; end
|
||||||
|
|
||||||
|
attributes :name, :age, :can_edit
|
||||||
has_many :posts, :serializer => Class.new
|
has_many :posts, :serializer => Class.new
|
||||||
|
has_many :drafts, :serializer => Class.new
|
||||||
has_one :parent, :serializer => Class.new
|
has_one :parent, :serializer => Class.new
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal serializer.schema, {
|
assert_equal serializer.schema, {
|
||||||
:attributes => { :name => :string, :age => :integer },
|
:attributes => { :name => :string, :age => :integer, :can_edit => nil },
|
||||||
:associations => {
|
:associations => {
|
||||||
:posts => { :has_many => :posts },
|
:posts => { :has_many => :posts },
|
||||||
|
:drafts => nil,
|
||||||
:parent => { :belongs_to => :parent }
|
:parent => { :belongs_to => :parent }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -673,7 +679,7 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
expected = serializer_class.new(post).as_json
|
expected = serializer_class.new(post).as_json
|
||||||
assert_equal expected, hash_object
|
assert_equal expected, hash_object
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_embed_ids_include_true_with_root
|
def test_embed_ids_include_true_with_root
|
||||||
serializer_class = post_serializer
|
serializer_class = post_serializer
|
||||||
|
|
||||||
@ -722,7 +728,7 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
:author => [{ :first_name => "Jose", :last_name => "Valim" }]
|
:author => [{ :first_name => "Jose", :last_name => "Valim" }]
|
||||||
}, serializer.as_json)
|
}, serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
# the point of this test is to illustrate that deeply nested serializers
|
# the point of this test is to illustrate that deeply nested serializers
|
||||||
# still side-load at the root.
|
# still side-load at the root.
|
||||||
def test_embed_with_include_inserts_at_root
|
def test_embed_with_include_inserts_at_root
|
||||||
@ -919,7 +925,7 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
actual = attachment_serializer.new(attachment, {}).as_json
|
actual = attachment_serializer.new(attachment, {}).as_json
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:name => 'logo.png',
|
:name => 'logo.png',
|
||||||
:url => 'http://example.com/logo.png',
|
:url => 'http://example.com/logo.png',
|
||||||
:attachable => {
|
:attachable => {
|
||||||
:type => :email,
|
:type => :email,
|
||||||
@ -956,7 +962,7 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
actual = attachment_serializer.new(attachment, {}).as_json
|
actual = attachment_serializer.new(attachment, {}).as_json
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:name => 'logo.png',
|
:name => 'logo.png',
|
||||||
:url => 'http://example.com/logo.png',
|
:url => 'http://example.com/logo.png',
|
||||||
:attachable => {
|
:attachable => {
|
||||||
:type => :email,
|
:type => :email,
|
||||||
@ -995,7 +1001,7 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:attachment => {
|
:attachment => {
|
||||||
:name => 'logo.png',
|
:name => 'logo.png',
|
||||||
:url => 'http://example.com/logo.png',
|
:url => 'http://example.com/logo.png',
|
||||||
:attachable => {
|
:attachable => {
|
||||||
:type => :email,
|
:type => :email,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user