Pass options to associations

This commit is contained in:
Tema Bolshakov and Dmitry Myaskovskiy 2014-08-29 20:16:11 +04:00 committed by Tema Bolshakov
parent 258b5953e2
commit 71a43a432a
4 changed files with 31 additions and 16 deletions

View File

@ -99,11 +99,15 @@ module ActiveModel
end end
end end
def associations(options = {}) def each_association(&block)
self.class._associations.dup.each_with_object({}) do |(name, value), hash| self.class._associations.dup.each do |name, options|
association = object.send(name) association = object.send(name)
serializer_class = ActiveModel::Serializer.serializer_for(association) 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 end
end end

View File

@ -3,13 +3,14 @@ module ActiveModel
class Adapter class Adapter
class Json < Adapter class Json < Adapter
def serializable_hash(options = {}) 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) if association.respond_to?(:each)
@hash[name] = association.map(&:attributes) array_serializer = association
@hash[name] = array_serializer.map { |item| item.attributes(options) }
else else
@hash[name] = association.attributes @hash[name] = association.attributes(options)
end end
end end
@hash @hash

View File

@ -5,31 +5,33 @@ module ActiveModel
def serializable_hash(options = {}) def serializable_hash(options = {})
@hash = serializer.attributes @hash = serializer.attributes
serializer.associations.each do |name, association| serializer.each_association do |name, association, options|
@hash[:links] ||= {} @hash[:links] ||= {}
@hash[:linked] ||= {} @hash[:linked] ||= {}
if association.respond_to?(:each) if association.respond_to?(:each)
add_links(name, association) add_links(name, association, options)
else else
add_link(name, association) add_link(name, association, options)
end end
end end
@hash @hash
end end
def add_links(name, serializers) def add_links(name, serializers, options)
@hash[:links][name] ||= [] @hash[:links][name] ||= []
@hash[:linked][name] ||= [] @hash[:linked][name] ||= []
@hash[:links][name] += serializers.map(&:id) @hash[:links][name] += serializers.map(&:id)
@hash[:linked][name] += serializers.map(&:attributes) @hash[:linked][name] += serializers.map { |item| item.attributes(options) }
end end
def add_link(name, serializer) def add_link(name, serializer, options)
plural_name = name.to_s.pluralize.to_sym plural_name = name.to_s.pluralize.to_sym
@hash[:linked][plural_name] ||= [] @hash[:linked][plural_name] ||= []
@hash[:links][name] = serializer.id @hash[:links][name] = serializer.id
@hash[:linked][plural_name].push serializer.attributes @hash[:linked][plural_name].push serializer.attributes(options)
end end
end end
end end

View File

@ -36,12 +36,20 @@ module ActiveModel
def test_has_many def test_has_many
assert_equal({comments: {type: :has_many, options: {}}}, @post_serializer.class._associations) 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 end
def test_has_one def test_has_one
assert_equal({post: {type: :belongs_to, options: {}}}, @comment_serializer.class._associations) 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 end
end end