Merge pull request #748 from raphaelpereira/0-9-stable

Propagate serialization_options across associations
This commit is contained in:
Alexandre de Oliveira 2015-03-30 16:31:41 -03:00
commit dc08f0ec80
4 changed files with 21 additions and 6 deletions

View File

@ -163,7 +163,7 @@ end
end end
end end
def associations def associations(options={})
associations = self.class._associations associations = self.class._associations
included_associations = filter(associations.keys) included_associations = filter(associations.keys)
associations.each_with_object({}) do |(name, association), hash| associations.each_with_object({}) do |(name, association), hash|
@ -180,7 +180,7 @@ end
if association.embed_namespace? if association.embed_namespace?
hash = hash[association.embed_namespace] ||= {} hash = hash[association.embed_namespace] ||= {}
end end
hash[association.embedded_key] = serialize association hash[association.embedded_key] = serialize association, options
end end
end end
end end
@ -240,8 +240,8 @@ end
end end
end end
def serialize(association) def serialize(association,options={})
build_serializer(association).serializable_object build_serializer(association).serializable_object(options)
end end
def serialize_ids(association) def serialize_ids(association)
@ -286,7 +286,7 @@ end
self.serialization_options = options self.serialization_options = options
return @wrap_in_array ? [] : nil if @object.nil? return @wrap_in_array ? [] : nil if @object.nil?
hash = attributes hash = attributes
hash.merge! associations hash.merge! associations(options)
hash = convert_keys(hash) if key_format.present? hash = convert_keys(hash) if key_format.present?
hash = { :type => type_name(@object), type_name(@object) => hash } if @polymorphic hash = { :type => type_name(@object), type_name(@object) => hash } if @polymorphic
@wrap_in_array ? [hash] : hash @wrap_in_array ? [hash] : hash

View File

@ -109,6 +109,13 @@ end
class PostSerializer < ActiveModel::Serializer class PostSerializer < ActiveModel::Serializer
attributes :title, :body attributes :title, :body
def title
keyword = serialization_options[:highlight_keyword]
title = object.read_attribute_for_serialization(:title)
title = title.gsub(keyword,"'#{keyword}'") if keyword
title
end
has_many :comments has_many :comments
end end

View File

@ -176,7 +176,7 @@ module ActiveModel
def test_associations_embedding_objects_using_a_given_array_serializer def test_associations_embedding_objects_using_a_given_array_serializer
@association.serializer_from_options = Class.new(ArraySerializer) do @association.serializer_from_options = Class.new(ArraySerializer) do
def serializable_object def serializable_object(options={})
{ my_content: ['fake'] } { my_content: ['fake'] }
end end
end end

View File

@ -21,6 +21,9 @@ module ActiveModel
serialization_options[:force_the_description] serialization_options[:force_the_description]
end end
end end
@category = Category.new({name: 'Category 1'})
@category_serializer = CategorySerializer.new(@category)
end end
def test_filtered_attributes_serialization def test_filtered_attributes_serialization
@ -29,6 +32,11 @@ module ActiveModel
'profile' => { name: 'Name 1', description: forced_description } 'profile' => { name: 'Name 1', description: forced_description }
}, @profile_serializer.as_json(force_the_description: forced_description)) }, @profile_serializer.as_json(force_the_description: forced_description))
end end
def test_filtered_attributes_serialization_across_association
assert_equal("'T1'",
@category_serializer.as_json(highlight_keyword: 'T1')['category'][:posts][0][:title])
end
end end
end end
end end