class ActiveModel::Serializer
end
end
post.author == scope
def author?
end
hash
hash.merge!(:email => post.email) if author?
hash = super
def attributes
private
has_many :comments
attributes :title, :body
class PostSerializer < ActiveModel::Serializer
author of the post:
For example, some attributes maybe only be returned if current_user
is the
We use the scope to check if a given attribute should be serialized or not.
The object to be serialized is the +@post+ and the scope is current_user
.
PostSerializer.new(@post, current_user).to_json
one may do in a controller:
it expects to object as arguments, a resource and a scope. For example,
control how a given object is going to be serialized. On initialization,
Provides a basic serializer implementation that allows you to easily
Active Model Serializer
def as_json(options=nil)
Returns a json representation of the serializable
def as_json(options=nil) options ||= {} if root = options.fetch(:root, @options.fetch(:root, _root)) @hash = hash = {} hash.merge!(root => serializable_hash) hash else @hash = serializable_hash end end
def associate(klass, attrs) #:nodoc:
def associate(klass, attrs) #:nodoc: options = attrs.extract_options! self._associations += attrs.map do |attr| unless method_defined?(attr) class_eval "def #{attr}() object.#{attr} end", __FILE__, __LINE__ end klass.new(attr, options) end end
def association_ids
Returns a hash representation of the serializable
def association_ids hash = {} _associations.each do |association| associated_object = send(association.name) hash.merge! association.serialize_ids(associated_object, scope) end hash end
def associations
Returns a hash representation of the serializable
def associations hash = {} _associations.each do |association| associated_object = send(association.name) hash.merge! association.serialize(associated_object, scope, self, :hash => @hash) end hash end
def attribute(attr, options={})
def attribute(attr, options={}) self._attributes = _attributes.merge(attr => options[:key] || attr) end
def attributes(*attrs)
def attributes(*attrs) self._attributes = _attributes.dup attrs.each do |attr| self._attributes[attr] = attr end end
def attributes
Returns a hash representation of the serializable
def attributes hash = {} _attributes.each do |name,key| hash[key] = @object.read_attribute_for_serialization(name) end hash end
def embed(type, options={})
embed :ids, :include => true # Embed the association ids and include objects in the root
embed :ids # Embed only the association ids
embed :objects # Embed associations as full objects
Define how associations should be embedded.
def embed(type, options={}) self._embed = type self._root_embed = true if options[:include] end
def has_many(*attrs)
with the association name does not exist, the association name is
as a method which should return an array when invoked. If a method
The serializer object should implement the association name
Defines an association in the object should be rendered.
def has_many(*attrs) associate(Associations::HasMany, attrs) end
def has_one(*attrs)
with the association name does not exist, the association name is
as a method which should return an object when invoked. If a method
The serializer object should implement the association name
Defines an association in the object should be rendered.
def has_one(*attrs) associate(Associations::HasOne, attrs) end
def inherited(klass) #:nodoc:
def inherited(klass) #:nodoc: return if klass.anonymous? name = klass.name.demodulize.underscore.sub(/_serializer$/, '') klass.class_eval do alias_method name.to_sym, :object root name.to_sym unless self._root == false end end
def initialize(object, scope, options={})
def initialize(object, scope, options={}) @object, @scope, @options = object, scope, options @hash = options[:hash] end
def merge_associations(hash, associations)
Merge associations for embed case by always adding
def merge_associations(hash, associations) associations.each do |key, value| if hash[key] hash[key] |= value elsif value hash[key] = value end end end
def model_class
def model_class name.sub(/Serializer$/, '').constantize end
def root(name)
def root(name) self._root = name end
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, association| model_association = klass.reflect_on_association(association.name) hash.merge association.key => { model_association.macro => model_association.name } end { :attributes => attrs, :associations => associations } end
def serializable_hash
Returns a hash representation of the serializable
def serializable_hash if _embed == :ids merge_associations(@hash, associations) if _root_embed attributes.merge(association_ids) elsif _embed == :objects attributes.merge(associations) else attributes end end