mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Add serialization_scope_name_test
This commit is contained in:
parent
2962f3f64e
commit
232e367560
67
test/action_controller/serialization_scope_name_test.rb
Normal file
67
test/action_controller/serialization_scope_name_test.rb
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
require 'pathname'
|
||||||
|
|
||||||
|
class DefaultScopeNameTest < ActionController::TestCase
|
||||||
|
TestUser = Struct.new(:name, :admin)
|
||||||
|
|
||||||
|
class UserSerializer < ActiveModel::Serializer
|
||||||
|
attributes :admin?
|
||||||
|
def admin?
|
||||||
|
current_user.admin
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class UserTestController < ActionController::Base
|
||||||
|
protect_from_forgery
|
||||||
|
|
||||||
|
before_filter { request.format = :json }
|
||||||
|
|
||||||
|
def current_user
|
||||||
|
TestUser.new('Pete', false)
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_new_user
|
||||||
|
render json: TestUser.new('pete', false), serializer: UserSerializer, adapter: :json_api
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
tests UserTestController
|
||||||
|
|
||||||
|
def test_default_scope_name
|
||||||
|
get :render_new_user
|
||||||
|
assert_equal '{"users":{"admin?":false}}', @response.body
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class SerializationScopeNameTest < ActionController::TestCase
|
||||||
|
TestUser = Struct.new(:name, :admin)
|
||||||
|
|
||||||
|
class AdminUserSerializer < ActiveModel::Serializer
|
||||||
|
attributes :admin?
|
||||||
|
def admin?
|
||||||
|
current_admin.admin
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class AdminUserTestController < ActionController::Base
|
||||||
|
protect_from_forgery
|
||||||
|
|
||||||
|
serialization_scope :current_admin
|
||||||
|
before_filter { request.format = :json }
|
||||||
|
|
||||||
|
def current_admin
|
||||||
|
TestUser.new('Bob', true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_new_user
|
||||||
|
render json: TestUser.new('pete', false), serializer: AdminUserSerializer, adapter: :json_api
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
tests AdminUserTestController
|
||||||
|
|
||||||
|
def test_override_scope_name_with_controller
|
||||||
|
get :render_new_user
|
||||||
|
assert_equal '{"admin_users":{"admin?":true}}', @response.body
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user