Skip to content

Commit dc08f0e

Browse files
committed
Merge pull request #748 from raphaelpereira/0-9-stable
Propagate serialization_options across associations
2 parents fe240d5 + fd5cd87 commit dc08f0e

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

lib/active_model/serializer.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def attributes
163163
end
164164
end
165165

166-
def associations
166+
def associations(options={})
167167
associations = self.class._associations
168168
included_associations = filter(associations.keys)
169169
associations.each_with_object({}) do |(name, association), hash|
@@ -180,7 +180,7 @@ def associations
180180
if association.embed_namespace?
181181
hash = hash[association.embed_namespace] ||= {}
182182
end
183-
hash[association.embedded_key] = serialize association
183+
hash[association.embedded_key] = serialize association, options
184184
end
185185
end
186186
end
@@ -240,8 +240,8 @@ def association_options_for_serializer(association)
240240
end
241241
end
242242

243-
def serialize(association)
244-
build_serializer(association).serializable_object
243+
def serialize(association,options={})
244+
build_serializer(association).serializable_object(options)
245245
end
246246

247247
def serialize_ids(association)
@@ -286,7 +286,7 @@ def serializable_object(options={})
286286
self.serialization_options = options
287287
return @wrap_in_array ? [] : nil if @object.nil?
288288
hash = attributes
289-
hash.merge! associations
289+
hash.merge! associations(options)
290290
hash = convert_keys(hash) if key_format.present?
291291
hash = { :type => type_name(@object), type_name(@object) => hash } if @polymorphic
292292
@wrap_in_array ? [hash] : hash

test/fixtures/poro.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ class CategorySerializer < ActiveModel::Serializer
109109
class PostSerializer < ActiveModel::Serializer
110110
attributes :title, :body
111111

112+
def title
113+
keyword = serialization_options[:highlight_keyword]
114+
title = object.read_attribute_for_serialization(:title)
115+
title = title.gsub(keyword,"'#{keyword}'") if keyword
116+
title
117+
end
118+
112119
has_many :comments
113120
end
114121

test/unit/active_model/serializer/has_many_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def serializable_object
176176

177177
def test_associations_embedding_objects_using_a_given_array_serializer
178178
@association.serializer_from_options = Class.new(ArraySerializer) do
179-
def serializable_object
179+
def serializable_object(options={})
180180
{ my_content: ['fake'] }
181181
end
182182
end

test/unit/active_model/serializer/options_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ def description
2121
serialization_options[:force_the_description]
2222
end
2323
end
24+
25+
@category = Category.new({name: 'Category 1'})
26+
@category_serializer = CategorySerializer.new(@category)
2427
end
2528

2629
def test_filtered_attributes_serialization
@@ -29,6 +32,11 @@ def test_filtered_attributes_serialization
2932
'profile' => { name: 'Name 1', description: forced_description }
3033
}, @profile_serializer.as_json(force_the_description: forced_description))
3134
end
35+
36+
def test_filtered_attributes_serialization_across_association
37+
assert_equal("'T1'",
38+
@category_serializer.as_json(highlight_keyword: 'T1')['category'][:posts][0][:title])
39+
end
3240
end
3341
end
3442
end

0 commit comments

Comments
 (0)