From dde14929343b0e7bb721bfce186a8be427aae26b Mon Sep 17 00:00:00 2001 From: Theodore Konukhov Date: Tue, 26 Aug 2014 18:07:31 +0200 Subject: [PATCH] tests for namespaced controlleler --- lib/active_model/array_serializer.rb | 2 +- lib/active_model/serializable.rb | 6 +++ test/fixtures/poro.rb | 9 ++++ .../namespaced_serialization_test.rb | 47 +++++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 test/integration/action_controller/namespaced_serialization_test.rb diff --git a/lib/active_model/array_serializer.rb b/lib/active_model/array_serializer.rb index 25093ace..9da8194a 100644 --- a/lib/active_model/array_serializer.rb +++ b/lib/active_model/array_serializer.rb @@ -39,7 +39,7 @@ module ActiveModel def serializable_object @object.map do |item| - serializer_for(item).serializable_object + serializer_for(item).serializable_object_with_notification end end alias_method :serializable_array, :serializable_object diff --git a/lib/active_model/serializable.rb b/lib/active_model/serializable.rb index 1faf62f1..4eb6b9f4 100644 --- a/lib/active_model/serializable.rb +++ b/lib/active_model/serializable.rb @@ -12,6 +12,12 @@ module ActiveModel end end + def serializable_object_with_notification + instrument('!serialize') do + serializable_object + end + end + def serializable_data embedded_in_root_associations.tap do |hash| if respond_to?(:meta) && meta diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index a357c24e..de3c2037 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -73,3 +73,12 @@ end class WebLogLowerCamelSerializer < WebLogSerializer format_keys :lower_camel end + +class ShortUserSerializer < ActiveModel::Serializer + attributes :name +end + +module TestNamespace + class ProfileSerializer < ::ProfileSerializer; end + class UserSerializer < ::UserSerializer; end +end diff --git a/test/integration/action_controller/namespaced_serialization_test.rb b/test/integration/action_controller/namespaced_serialization_test.rb new file mode 100644 index 00000000..4a5fbbf9 --- /dev/null +++ b/test/integration/action_controller/namespaced_serialization_test.rb @@ -0,0 +1,47 @@ +require 'test_helper' + +module ActionController + module Serialization + class NamespacedSerializationTest < ActionController::TestCase + class TestNamespace::MyController < ActionController::Base + def render_profile_with_namespace + render json: Profile.new({ name: 'Name 1', description: 'Description 1'}) + end + + def render_profiles_with_namespace + render json: [Profile.new({ name: 'Name 1', description: 'Description 1'})] + end + + def render_comment + render json: Comment.new(content: 'Comment 1') + end + + def render_comments + render json: [Comment.new(content: 'Comment 1')] + end + end + + tests TestNamespace::MyController + + def test_render_profile_with_namespace + get :render_profile_with_namespace + assert_serializer TestNamespace::ProfileSerializer + end + + def test_render_profiles_with_namespace + get :render_profiles_with_namespace + assert_serializer TestNamespace::ProfileSerializer + end + + def test_fallback_to_a_version_without_namespace + get :render_comment + assert_serializer CommentSerializer + end + + def test_array_fallback_to_a_version_without_namespace + get :render_comments + assert_serializer CommentSerializer + end + end + end +end \ No newline at end of file