1
+ require 'test_helper'
2
+ require 'pathname'
3
+
4
+ class DefaultScopeNameTest < ActionController ::TestCase
5
+ TestUser = Struct . new ( :name , :admin )
6
+
7
+ class UserSerializer < ActiveModel ::Serializer
8
+ attributes :admin?
9
+ def admin?
10
+ current_user . admin
11
+ end
12
+ end
13
+
14
+ class UserTestController < ActionController ::Base
15
+ protect_from_forgery
16
+
17
+ before_filter { request . format = :json }
18
+
19
+ def current_user
20
+ TestUser . new ( 'Pete' , false )
21
+ end
22
+
23
+ def render_new_user
24
+ render json : TestUser . new ( 'pete' , false ) , serializer : UserSerializer , adapter : :json_api
25
+ end
26
+ end
27
+
28
+ tests UserTestController
29
+
30
+ def test_default_scope_name
31
+ get :render_new_user
32
+ assert_equal '{"users":{"admin?":false}}' , @response . body
33
+ end
34
+ end
35
+
36
+ class SerializationScopeNameTest < ActionController ::TestCase
37
+ TestUser = Struct . new ( :name , :admin )
38
+
39
+ class AdminUserSerializer < ActiveModel ::Serializer
40
+ attributes :admin?
41
+ def admin?
42
+ current_admin . admin
43
+ end
44
+ end
45
+
46
+ class AdminUserTestController < ActionController ::Base
47
+ protect_from_forgery
48
+
49
+ serialization_scope :current_admin
50
+ before_filter { request . format = :json }
51
+
52
+ def current_admin
53
+ TestUser . new ( 'Bob' , true )
54
+ end
55
+
56
+ def render_new_user
57
+ render json : TestUser . new ( 'pete' , false ) , serializer : AdminUserSerializer , adapter : :json_api
58
+ end
59
+ end
60
+
61
+ tests AdminUserTestController
62
+
63
+ def test_override_scope_name_with_controller
64
+ get :render_new_user
65
+ assert_equal '{"admin_users":{"admin?":true}}' , @response . body
66
+ end
67
+ end
0 commit comments