class ActiveModel::Serializer::Reflection
So you can inspect reflections in your Adapters.
# ]
# HasManyReflection.new(:secret_meta_data, { if: :is_admin? })
# HasManyReflection.new(:comments, { key: :last_comments }, #<Block>)
# HasManyReflection.new(:comments)
# HasOneReflection.new(:author, serializer: AuthorSerializer),
# [
PostSerializer._reflections #=>
2) as ‘object.comments.last(1)’ and named ‘last_comments’.
1) as ‘comments’ and named ‘comments’.
Specifically, the association ‘comments’ is evaluated two different ways:
end
end
current_user.admin?
def is_admin?
has_many :secret_meta_data, if: :is_admin?
end
object.comments.last(1)
has_many :comments, key: :last_comments do
has_many :comments
has_one :author, serializer: AuthorSerializer
class PostSerializer < ActiveModel::Serializer
@example
ActiveModel::Serializer class.
Holds all the meta-data about an association as it was specified in the
def build_association(subject, parent_serializer_options)
- Api: - private
Parameters:
-
parent_serializer_options
(Hash{Symbol => Object}
) -- -
subject
(Serializer
) -- is a parent serializer for given association
def build_association(subject, parent_serializer_options) association_value = value(subject) reflection_options = options.dup serializer_class = subject.class.serializer_for(association_value, reflection_options) reflection_options[:include_data] = @_include_data if serializer_class begin serializer = serializer_class.new( association_value, serializer_options(subject, parent_serializer_options, reflection_options) ) rescue ActiveModel::Serializer::CollectionSerializer::NoSerializerError reflection_options[:virtual_value] = association_value.try(:as_json) || association_value end elsif !association_value.nil? && !association_value.instance_of?(Object) reflection_options[:virtual_value] = association_value end Association.new(name, serializer, reflection_options, @_links, @_meta) end
def include_data(value = true)
def include_data(value = true) @_include_data = value :nil end
def initialize(*)
def initialize(*) super @_links = {} @_include_data = true @_meta = nil end
def link(name, value = nil, &block)
def link(name, value = nil, &block) @_links[name] = block || value :nil end
def meta(value = nil, &block)
def meta(value = nil, &block) @_meta = block || value :nil end
def serializer_options(subject, parent_serializer_options, reflection_options)
def serializer_options(subject, parent_serializer_options, reflection_options) serializer = reflection_options.fetch(:serializer, nil) serializer_options = parent_serializer_options.except(:serializer) serializer_options[:serializer] = serializer if serializer serializer_options[:serializer_context_class] = subject.class serializer_options end
def value(serializer)
-
(:nil, associated resource or resource collection)
-
Other tags:
- Yield: -
Parameters:
-
serializer
(ActiveModel::Serializer
) --
def value(serializer) @object = serializer.object @scope = serializer.scope if block block_value = instance_exec(serializer, &block) if block_value != :nil block_value elsif @_include_data serializer.read_attribute_for_serialization(name) end else serializer.read_attribute_for_serialization(name) end end