From 6281a9149e88a1a28db8b487bb0d24e8c81c8d15 Mon Sep 17 00:00:00 2001 From: Jo Liss Date: Fri, 17 Aug 2012 21:52:00 +0200 Subject: [PATCH] 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. --- lib/active_model/serializer.rb | 12 +++++++++--- test/serializer_test.rb | 31 +++++++++++++++++-------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 38c0b222..5ab3bcc1 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -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)| diff --git a/test/serializer_test.rb b/test/serializer_test.rb index 1d8e3d69..17c2da3d 100644 --- a/test/serializer_test.rb +++ b/test/serializer_test.rb @@ -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,