Sha256: 6fd9c814600a1025de3d6684a33574ab58f833fe61d02f21fc21940d8c16a02b

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require 'test_helper'

module ActionController
  module Serialization
    class LookupProcTest < ActionController::TestCase
      module Api
        module V3
          class PostCustomSerializer < ActiveModel::Serializer
            attributes :title, :body

            belongs_to :author
          end

          class AuthorCustomSerializer < ActiveModel::Serializer
            attributes :name
          end

          class LookupProcTestController < ActionController::Base
            def implicit_namespaced_serializer
              author = Author.new(name: 'Bob')
              post = Post.new(title: 'New Post', body: 'Body', author: author)

              render json: post
            end
          end
        end
      end

      tests Api::V3::LookupProcTestController

      test 'implicitly uses namespaced serializer' do
        controller_namespace = lambda do |resource_class, _parent_serializer_class, namespace|
          "#{namespace}::#{resource_class}CustomSerializer" if namespace
        end

        with_prepended_lookup(controller_namespace) do
          get :implicit_namespaced_serializer

          assert_serializer Api::V3::PostCustomSerializer

          expected = { 'title' => 'New Post', 'body' => 'Body', 'author' => { 'name' => 'Bob' } }
          actual = JSON.parse(@response.body)

          assert_equal expected, actual
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
active_model_serializers_custom-0.10.90 test/action_controller/lookup_proc_test.rb
active_model_serializers-0.10.9 test/action_controller/lookup_proc_test.rb