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 may only be returned if current_user
is the
We use the scope to check if a given attribute should be serialized or not.
in for authorization purposes.
The object to be serialized is the +@post+ and the current user is passed
PostSerializer.new(@post, :scope => current_user).to_json
one may do in a controller:
it expects two objects as arguments, a resource and options. 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 _serializable_hash
def _serializable_hash return nil if @object.nil? @node = attributes include_associations! if _embed @node end
def as_json(options={})
Returns a json representation of the serializable
def as_json(options={}) if root = options.fetch(:root, @options.fetch(:root, root_name)) @options[:hash] = hash = {} @options[:unique_values] = {} hash.merge!(root => serializable_hash) include_meta hash hash else serializable_hash end end
def associate(klass, attrs) #:nodoc:
def associate(klass, attrs) #:nodoc: options = attrs.extract_options! self._associations = _associations.dup attrs.each do |attr| unless method_defined?(attr) define_method attr do object.send attr end end define_include_method attr self._associations[attr] = klass.refine(attr, options) end end
def attribute(attr, options={})
def attribute(attr, options={}) self._attributes = _attributes.merge(attr.is_a?(Hash) ? attr : {attr => options[:key] || attr.to_s.gsub(/\?$/, '').to_sym}) attr = attr.keys[0] if attr.is_a? Hash unless method_defined?(attr) define_method attr do object.read_attribute_for_serialization(attr.to_sym) end end define_include_method attr # protect inheritance chains and open classes # if a serializer inherits from another OR # attributes are added later in a classes lifecycle # poison the cache define_method :_fast_attributes do raise NameError end end
def attributes(*attrs)
def attributes(*attrs) self._attributes = _attributes.dup attrs.each do |attr| if Hash === attr attr.each {|attr_real, key| attribute attr_real, :key => key } else attribute attr end end end
def attributes
Returns a hash representation of the serializable
def attributes _fast_attributes rescue NameError method = "def _fast_attributes\n" method << " h = {}\n" _attributes.each do |name,key| method << " h[:\"#{key}\"] = read_attribute_for_serialization(:\"#{name}\") if include?(:\"#{name}\")\n" end method << " h\nend" self.class.class_eval method _fast_attributes end
def build_json(controller, resource, options)
set during the request lifecycle or by the controller class, and should
settings and options for a given resource. These settings are typically
Used internally to create a new serializer object based on controller
def build_json(controller, resource, options) default_options = controller.send(:default_serializer_options) || {} options = default_options.merge(options || {}) serializer = options.delete(:serializer) || (resource.respond_to?(:active_model_serializer) && resource.active_model_serializer) return serializer unless serializer if resource.respond_to?(:to_ary) unless serializer <= ActiveModel::ArraySerializer raise ArgumentError.new("#{serializer.name} is not an ArraySerializer. " + "You may want to use the :each_serializer option instead.") end if options[:root] != false && serializer.root != false # the serializer for an Array is ActiveModel::ArraySerializer options[:root] ||= serializer.root || controller.controller_name end end options[:scope] = controller.serialization_scope unless options.has_key?(:scope) options[:scope_name] = controller._serialization_scope options[:url_options] = controller.url_options serializer.new(resource, options) end
def cached(value = true)
def cached(value = true) self.perform_caching = value end
def define_include_method(name)
def define_include_method(name) method = "include_#{name}?".to_sym INCLUDE_METHODS[name] = method unless method_defined?(method) define_method method do true end end 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 expand_cache_key(*args)
def expand_cache_key(*args) ActiveSupport::Cache.expand_cache_key(args) 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 include!(name, options={})
def include!(name, options={}) # Make sure that if a special options[:hash] was passed in, we generate # a new unique values hash and don't clobber the original. If the hash # passed in is the same as the current options hash, use the current # unique values. # # TODO: Should passing in a Hash even be public API here? unique_values = if hash = options[:hash] if @options[:hash] == hash @options[:unique_values] ||= {} else {} end else hash = @options[:hash] @options[:unique_values] ||= {} end node = options[:node] ||= @node value = options[:value] if options[:include] == nil if @options.key?(:include) options[:include] = @options[:include].include?(name) elsif @options.include?(:exclude) options[:include] = !@options[:exclude].include?(name) end end association_class = if klass = _associations[name] klass elsif value.respond_to?(:to_ary) Associations::HasMany else Associations::HasOne end association = association_class.new(name, self, options) if association.embed_ids? node[association.key] = association.serialize_ids if association.embed_in_root? && hash.nil? raise IncludeError.new(self.class, association.name) elsif association.embed_in_root? && association.embeddable? merge_association hash, association.root, association.serializables, unique_values end elsif association.embed_objects? node[association.key] = association.serialize end end
def include?(name)
def include?(name) return false if options.key?(:only) && !Array(options[:only]).include?(name) return false if options.key?(:except) && Array(options[:except]).include?(name) send INCLUDE_METHODS[name] end
def include_associations!
def include_associations! _associations.each_key do |name| include!(name) if include?(name) end end
def include_meta(hash)
def include_meta(hash) hash[meta_key] = @options[:meta] if @options.has_key?(:meta) end
def initialize(object, options={})
def initialize(object, options={}) @object, @options = object, options scope_name = @options[:scope_name] if scope_name && !respond_to?(scope_name) self.class.class_eval do define_method scope_name, lambda { scope } end end end
def instrument(name, payload = {}, &block)
Use ActiveSupport::Notifications to send events to external systems.
def instrument(name, payload = {}, &block) event_name = INSTRUMENT[name] ActiveSupport::Notifications.instrument(event_name, payload, &block) end
def merge_association(hash, key, serializables, unique_values)
avoids the need to scan through the Array looking for entries every time
a unique list of all of the objects that are already in the Array. This
In order to make this efficient, we store a :unique_values hash containing
of all tags for all comments of the post.
which has_many tags, the top-level :tags key will contain the merged list
content for all of the children. For instance, if a Post has_many comments,
In some cases, an Array of associations is built by merging the associated
def merge_association(hash, key, serializables, unique_values) already_serialized = (unique_values[key] ||= {}) serializable_hashes = (hash[key] ||= []) serializables.each do |serializable| unless already_serialized.include? serializable.object already_serialized[serializable.object] = true serializable_hashes << serializable.serializable_hash end end end
def meta_key
def meta_key @options[:meta_key].try(:to_sym) || :meta end
def model_class
def model_class name.sub(/Serializer$/, '').constantize end
def perform_caching?
def perform_caching? perform_caching && cache && respond_to?(:cache_key) end
def root(name)
def root(name) self._root = name end
def root_name
def root_name return false if self._root == false class_name = self.class.name.demodulize.underscore.sub(/_serializer$/, '').to_sym unless self.class.name.blank? if self._root == true class_name else self._root || class_name end 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.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
def scope
def scope @options[:scope] end
def serializable_hash
Returns a hash representation of the serializable
def serializable_hash if perform_caching? cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'serializable-hash']) do _serializable_hash end else _serializable_hash end end
def to_json(*args)
def to_json(*args) if perform_caching? cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'to-json']) do super end else super end end
def url_options
def url_options @options[:url_options] || {} end