class ActiveModel::Serializer
def schema
TODO: This is currently coupled to Active Record. We need to
to work.
methods on your custom models if you want the serializer's schema method
methods, provided by default by ActiveRecord. You can implement these
The schema method uses the +columns_hash+ and +reflect_on_association+
which is provided by +SerializerClass.model_class+.
This information is extracted from the serializer's model class,
{ :my_posts => { :has_many => :posts }
the hash looks like this:
end
has_many :posts, :key => :my_posts
class PostsSerializer < ActiveModel::Serializer
If :key is used:
{ :posts => { :has_many => :posts } }
The +associations+ hash looks like this:
{ :name => :string, :age => :integer }
The +attributes+ hash looks like this:
The schema hash has two keys: +attributes+ and +associations+.
can be used to generate clients for the serialized output.
Return a schema hash for the current serializer. This information
def schema klass = model_class columns = klass.columns_hash 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, unless specified in the attribute declaration if name != key attrs[name] = key else attrs[key] = nil end end end associations = {} _associations.each do |attr, association_class| association = association_class.new(attr, self) if model_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] = nil end end { :attributes => attrs, :associations => associations } end