You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Was a nice feature in 0.8.x that could be useful with #1162 and #1270
# Return a schema hash for the current serializer. This information# can be used to generate clients for the serialized output.## The schema hash has two keys: +attributes+ and +associations+.## The +attributes+ hash looks like this:## { :name => :string, :age => :integer }## The +associations+ hash looks like this:# { :posts => { :has_many => :posts } }## If :key is used:## class PostsSerializer < ActiveModel::Serializer# has_many :posts, :key => :my_posts# end## the hash looks like this:## { :my_posts => { :has_many => :posts }## This information is extracted from the serializer's model class,# which is provided by +SerializerClass.model_class+.## The schema method uses the +columns_hash+ and +reflect_on_association+# methods, provided by default by ActiveRecord. You can implement these# methods on your custom models if you want the serializer's schema method# to work.## TODO: This is currently coupled to Active Record. We need to# figure out a way to decouple those two.defschemaklass=model_classcolumns=klass.columns_hashattrs={}_attributes.eachdo |name,key|
ifcolumn=columns[name.to_s]attrs[key]=column.typeelse# Computed attribute (method on serializer or model). We cannot# infer the type, so we put nil, unless specified in the attribute declarationifname != keyattrs[name]=keyelseattrs[key]=nilendendendassociations={}_associations.eachdo |attr,association_class|
association=association_class.new(attr,self)ifmodel_association=klass.reflect_on_association(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]=nilendend{:attributes=>attrs,:associations=>associations}end# The model class associated with this serializer.defmodel_classname.sub(/Serializer$/,'').constantizeend
The text was updated successfully, but these errors were encountered:
Was a nice feature in 0.8.x that could be useful with #1162 and #1270
The text was updated successfully, but these errors were encountered: