From ea6dc424ee7cc66a2141abe1014cd762c14f63c3 Mon Sep 17 00:00:00 2001 From: Raphael Pereira Date: Sat, 6 Dec 2014 09:25:30 -0200 Subject: [PATCH 1/3] Propagate serialization_options across associations --- lib/active_model/serializer.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 7a6ee7ca..3e31a792 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -161,7 +161,7 @@ end end end - def associations + def associations(options={}) associations = self.class._associations included_associations = filter(associations.keys) associations.each_with_object({}) do |(name, association), hash| @@ -178,7 +178,7 @@ end if association.embed_namespace? hash = hash[association.embed_namespace] ||= {} end - hash[association.embedded_key] = serialize association + hash[association.embedded_key] = serialize association, options end end end @@ -236,8 +236,8 @@ end end end - def serialize(association) - build_serializer(association).serializable_object + def serialize(association,options={}) + build_serializer(association).serializable_object(options) end def serialize_ids(association) @@ -282,7 +282,7 @@ end self.serialization_options = options return @wrap_in_array ? [] : nil if @object.nil? hash = attributes - hash.merge! associations + hash.merge! associations(options) hash = convert_keys(hash) if key_format.present? hash = { :type => type_name(@object), type_name(@object) => hash } if @polymorphic @wrap_in_array ? [hash] : hash From db566b427d4cce9d75803db37d1f59e2f25f5572 Mon Sep 17 00:00:00 2001 From: Raphael Pereira Date: Sat, 6 Dec 2014 09:50:42 -0200 Subject: [PATCH 2/3] serialization_options propagation - test correction --- test/unit/active_model/serializer/has_many_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/active_model/serializer/has_many_test.rb b/test/unit/active_model/serializer/has_many_test.rb index c6699150..39c6e2d7 100644 --- a/test/unit/active_model/serializer/has_many_test.rb +++ b/test/unit/active_model/serializer/has_many_test.rb @@ -176,7 +176,7 @@ module ActiveModel def test_associations_embedding_objects_using_a_given_array_serializer @association.serializer_from_options = Class.new(ArraySerializer) do - def serializable_object + def serializable_object(options={}) { my_content: ['fake'] } end end From fd5cd879f8a7345c15f1a30d4e42bc19f052eeca Mon Sep 17 00:00:00 2001 From: Raphael Pereira Date: Sat, 28 Mar 2015 13:24:03 -0300 Subject: [PATCH 3/3] Added test case for association propagating options --- test/fixtures/poro.rb | 7 +++++++ test/unit/active_model/serializer/options_test.rb | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index 73fc0463..74db9aa1 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -113,6 +113,13 @@ end class PostSerializer < ActiveModel::Serializer 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 end diff --git a/test/unit/active_model/serializer/options_test.rb b/test/unit/active_model/serializer/options_test.rb index 2e14151f..b2c5c02e 100644 --- a/test/unit/active_model/serializer/options_test.rb +++ b/test/unit/active_model/serializer/options_test.rb @@ -21,6 +21,9 @@ module ActiveModel serialization_options[:force_the_description] end end + + @category = Category.new({name: 'Category 1'}) + @category_serializer = CategorySerializer.new(@category) end def test_filtered_attributes_serialization @@ -29,6 +32,11 @@ module ActiveModel 'profile' => { name: 'Name 1', description: forced_description } }, @profile_serializer.as_json(force_the_description: forced_description)) 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