class ActiveModel::Serializer::Reflection
So you can inspect reflections in your Adapters.
# }
# secret_meta_data: HasManyReflection.new(:secret_meta_data, { if: :is_admin? })
# last_comments: HasManyReflection.new(:comments, { key: :last_comments }, #<Block>)
# comments: HasManyReflection.new(:comments)
# author: 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?
end
end
Blog.find(object.blog_id)
cache_store.fetch(“cached_blog:#{object.updated_at}”) do
def cached_blog
private
end
serializer.cached_blog
meta count: object.roles.count
has_one :blog do |serializer|
has_many :secret_meta_data, if: :is_admin?
end
object.comments.last(1)
has_many :comments, key: :last_comments do
has_many :comments
belongs_to :boss, type: :users, foreign_key: :boss_id
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(parent_serializer, parent_serializer_options, include_slice = {})
- Api: - private
Parameters:
-
parent_serializer_options
(Hash{Symbol => Object}
) -- -
parent_serializer
(Serializer
) -- for given association
def build_association(parent_serializer, parent_serializer_options, include_slice = {}) association_options = { parent_serializer: parent_serializer, parent_serializer_options: parent_serializer_options, include_slice: include_slice } Association.new(self, association_options) end
def collection?
def collection? false end
def foreign_key_on
- Api: - private
def foreign_key_on :related end
def include_data(value = true)
- Api: - public
def include_data(value = true) options[:include_data_setting] = value :nil end
def include_data?(include_slice)
def include_data?(include_slice) include_data_setting = options[:include_data_setting] case include_data_setting when :if_sideloaded then include_slice.key?(options.fetch(:key, name)) when true then true when false then false else fail ArgumentError, "Unknown include_data_setting '#{include_data_setting.inspect}'" end end
def initialize(*)
def initialize(*) super options[:links] = {} options[:include_data_setting] = Serializer.config.include_data_default options[:meta] = nil @type = options.fetch(:type) do class_name = options.fetch(:class_name, name.to_s.camelize.singularize) class_name.underscore.pluralize.to_sym end @foreign_key = options.fetch(:foreign_key) do if collection? "#{name.to_s.singularize}_ids".to_sym else "#{name}_id".to_sym end end end
def link(name, value = nil, &block)
- Api: - public
def link(name, value = nil, &block) options[:links][name] = block_given? ? block : value :nil end
def meta(value = nil, &block)
- Api: - public
def meta(value = nil, &block) options[:meta] = block_given? ? block : value :nil end
def value(serializer, include_slice)
-
(:nil, associated resource or resource collection)
-
Other tags:
- Yield: -
Parameters:
-
serializer
(ActiveModel::Serializer
) --
def value(serializer, include_slice) # NOTE(BF): This method isn't thread-safe because the _reflections class attribute is not thread-safe # Therefore, when we build associations from reflections, we dup the entire reflection instance. # Better solutions much appreciated! @object = serializer.object @scope = serializer.scope block_value = instance_exec(serializer, &block) if block return unless include_data?(include_slice) if block && block_value != :nil block_value else serializer.read_attribute_for_serialization(name) end end