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.inject({}) do |hash, (name,key)| column = columns[name.to_s] hash.merge key => column.type end associations = _associations.inject({}) do |hash, (attr,association_class)| association = association_class.new(attr, self) model_association = klass.reflect_on_association(association.name) hash.merge association.key => { model_association.macro => model_association.name } end { :attributes => attrs, :associations => associations } end