Sha256: 5d636781bdf746f75f1fd14b0fc38f15b12c3d88449d12943377fb8697a4d458

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

require 'test_helper'
require 'pathname'

class DefaultScopeNameTest < ActionController::TestCase
  class UserSerializer < ActiveModel::Serializer
    attributes :admin?
    ActiveModelSerializers.silence_warnings do
      def admin?
        current_user.admin
      end
    end
  end

  class UserTestController < ActionController::Base
    protect_from_forgery

    before_filter { request.format = :json }

    def current_user
      User.new(id: 1, name: 'Pete', admin: false)
    end

    def render_new_user
      render json: User.new(id: 1, name: 'Pete', admin: false), serializer: UserSerializer, adapter: :json_api
    end
  end

  tests UserTestController

  def test_default_scope_name
    get :render_new_user
    assert_equal '{"data":{"id":"1","type":"users","attributes":{"admin?":false}}}', @response.body
  end
end

class SerializationScopeNameTest < ActionController::TestCase
  class AdminUserSerializer < ActiveModel::Serializer
    attributes :admin?
    ActiveModelSerializers.silence_warnings do
      def admin?
        current_admin.admin
      end
    end
  end

  class AdminUserTestController < ActionController::Base
    protect_from_forgery

    serialization_scope :current_admin
    before_filter { request.format = :json }

    def current_admin
      User.new(id: 2, name: 'Bob', admin: true)
    end

    def render_new_user
      render json: User.new(id: 1, name: 'Pete', admin: false), serializer: AdminUserSerializer, adapter: :json_api
    end
  end

  tests AdminUserTestController

  def test_override_scope_name_with_controller
    get :render_new_user
    assert_equal '{"data":{"id":"1","type":"users","attributes":{"admin?":true}}}', @response.body
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
active_model_serializers-0.10.0.rc3 test/action_controller/serialization_scope_name_test.rb
cheap_ams-0.10.11 test/action_controller/serialization_scope_name_test.rb
cheap_ams-0.10.10 test/action_controller/serialization_scope_name_test.rb