mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Pass options to associations
This commit is contained in:
parent
258b5953e2
commit
71a43a432a
@ -99,11 +99,15 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
def associations(options = {})
|
||||
self.class._associations.dup.each_with_object({}) do |(name, value), hash|
|
||||
def each_association(&block)
|
||||
self.class._associations.dup.each do |name, options|
|
||||
association = object.send(name)
|
||||
serializer_class = ActiveModel::Serializer.serializer_for(association)
|
||||
hash[name] = serializer_class.new(association)
|
||||
serializer = serializer_class.new(association)
|
||||
|
||||
if block_given?
|
||||
block.call(name, serializer, options[:options])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -3,13 +3,14 @@ module ActiveModel
|
||||
class Adapter
|
||||
class Json < Adapter
|
||||
def serializable_hash(options = {})
|
||||
@hash = serializer.attributes
|
||||
@hash = serializer.attributes(options)
|
||||
|
||||
serializer.associations.each do |name, association|
|
||||
serializer.each_association do |name, association, options|
|
||||
if association.respond_to?(:each)
|
||||
@hash[name] = association.map(&:attributes)
|
||||
array_serializer = association
|
||||
@hash[name] = array_serializer.map { |item| item.attributes(options) }
|
||||
else
|
||||
@hash[name] = association.attributes
|
||||
@hash[name] = association.attributes(options)
|
||||
end
|
||||
end
|
||||
@hash
|
||||
|
||||
@ -5,31 +5,33 @@ module ActiveModel
|
||||
def serializable_hash(options = {})
|
||||
@hash = serializer.attributes
|
||||
|
||||
serializer.associations.each do |name, association|
|
||||
serializer.each_association do |name, association, options|
|
||||
@hash[:links] ||= {}
|
||||
@hash[:linked] ||= {}
|
||||
|
||||
if association.respond_to?(:each)
|
||||
add_links(name, association)
|
||||
add_links(name, association, options)
|
||||
else
|
||||
add_link(name, association)
|
||||
add_link(name, association, options)
|
||||
end
|
||||
end
|
||||
|
||||
@hash
|
||||
end
|
||||
|
||||
def add_links(name, serializers)
|
||||
def add_links(name, serializers, options)
|
||||
@hash[:links][name] ||= []
|
||||
@hash[:linked][name] ||= []
|
||||
@hash[:links][name] += serializers.map(&:id)
|
||||
@hash[:linked][name] += serializers.map(&:attributes)
|
||||
@hash[:linked][name] += serializers.map { |item| item.attributes(options) }
|
||||
end
|
||||
|
||||
def add_link(name, serializer)
|
||||
def add_link(name, serializer, options)
|
||||
plural_name = name.to_s.pluralize.to_sym
|
||||
@hash[:linked][plural_name] ||= []
|
||||
|
||||
@hash[:links][name] = serializer.id
|
||||
@hash[:linked][plural_name].push serializer.attributes
|
||||
@hash[:linked][plural_name].push serializer.attributes(options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -36,12 +36,20 @@ module ActiveModel
|
||||
|
||||
def test_has_many
|
||||
assert_equal({comments: {type: :has_many, options: {}}}, @post_serializer.class._associations)
|
||||
assert_kind_of(ActiveModel::Serializer::ArraySerializer, @post_serializer.associations[:comments])
|
||||
@post_serializer.each_association do |name, serializer, options|
|
||||
assert_equal(:comments, name)
|
||||
assert_equal({}, options)
|
||||
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
|
||||
end
|
||||
end
|
||||
|
||||
def test_has_one
|
||||
assert_equal({post: {type: :belongs_to, options: {}}}, @comment_serializer.class._associations)
|
||||
assert_kind_of(PostSerializer, @comment_serializer.associations[:post])
|
||||
@comment_serializer.each_association do |name, serializer, options|
|
||||
assert_equal(:post, name)
|
||||
assert_equal({}, options)
|
||||
assert_kind_of(PostSerializer, serializer)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user