@@ -72,33 +72,33 @@ def id
72
72
73
73
def test_defined_type
74
74
actual = with_jsonapi_inflection :plural do
75
- actual_resource_identifier_object ( WithDefinedTypeSerializer )
75
+ actual_resource_identifier_object ( WithDefinedTypeSerializer , @model )
76
76
end
77
- expected = { id : expected_model_id , type : 'with-defined-types' }
77
+ expected = { id : expected_model_id ( @model ) , type : 'with-defined-types' }
78
78
assert_equal actual , expected
79
79
end
80
80
81
81
def test_defined_type_not_inflected
82
82
actual = with_jsonapi_inflection :singular do
83
- actual_resource_identifier_object ( WithDefinedTypeSerializer )
83
+ actual_resource_identifier_object ( WithDefinedTypeSerializer , @model )
84
84
end
85
- expected = { id : expected_model_id , type : 'with-defined-types' }
85
+ expected = { id : expected_model_id ( @model ) , type : 'with-defined-types' }
86
86
assert_equal actual , expected
87
87
end
88
88
89
89
def test_singular_type
90
90
actual = with_jsonapi_inflection :singular do
91
- actual_resource_identifier_object ( AuthorSerializer )
91
+ actual_resource_identifier_object ( AuthorSerializer , @model )
92
92
end
93
- expected = { id : expected_model_id , type : 'author' }
93
+ expected = { id : expected_model_id ( @model ) , type : 'author' }
94
94
assert_equal actual , expected
95
95
end
96
96
97
97
def test_plural_type
98
98
actual = with_jsonapi_inflection :plural do
99
- actual_resource_identifier_object ( AuthorSerializer )
99
+ actual_resource_identifier_object ( AuthorSerializer , @model )
100
100
end
101
- expected = { id : expected_model_id , type : 'authors' }
101
+ expected = { id : expected_model_id ( @model ) , type : 'authors' }
102
102
assert_equal actual , expected
103
103
end
104
104
@@ -123,45 +123,69 @@ def test_type_with_namespace
123
123
end
124
124
125
125
def test_id_defined_on_object
126
- actual = actual_resource_identifier_object ( AuthorSerializer )
127
- expected = { id : @model . id . to_s , type : expected_model_type }
126
+ actual = actual_resource_identifier_object ( AuthorSerializer , @model )
127
+ expected = { id : @model . id . to_s , type : expected_model_type ( @model ) }
128
128
assert_equal actual , expected
129
129
end
130
130
131
131
def test_blank_id
132
- @model . id = nil
133
- actual = actual_resource_identifier_object ( AuthorSerializer )
134
- expected = { type : expected_model_type }
132
+ model = Author . new ( id : nil , name : 'Steve K.' )
133
+ actual = actual_resource_identifier_object ( AuthorSerializer , model )
134
+ expected = { type : expected_model_type ( model ) }
135
+ assert_equal actual , expected
136
+ end
137
+
138
+ def test_for_type_with_id
139
+ id = 1
140
+ actual = ResourceIdentifier . for_type_with_id ( 'admin_user' , id , { } )
141
+ expected = { id : "1" , type : 'admin-users' }
142
+ assert_equal actual , expected
143
+ end
144
+
145
+ def test_for_type_with_id_given_blank_id
146
+ id = ""
147
+ actual = ResourceIdentifier . for_type_with_id ( 'admin_user' , id , { } )
148
+ expected = { type : 'admin-users' }
149
+ assert_equal actual , expected
150
+ end
151
+
152
+ def test_for_type_with_id_inflected
153
+ id = 2
154
+ actual = with_jsonapi_inflection :singular do
155
+ ResourceIdentifier . for_type_with_id ( 'admin_users' , id , { } )
156
+ end
157
+ expected = { id : "2" , type : 'admin-user' }
135
158
assert_equal actual , expected
136
159
end
137
160
138
161
def test_id_defined_on_serializer
139
- actual = actual_resource_identifier_object ( WithDefinedIdSerializer )
140
- expected = { id : 'special_id' , type : expected_model_type }
162
+ actual = actual_resource_identifier_object ( WithDefinedIdSerializer , @model )
163
+ expected = { id : 'special_id' , type : expected_model_type ( @model ) }
141
164
assert_equal actual , expected
142
165
end
143
166
144
167
def test_id_defined_on_fragmented
145
- actual = actual_resource_identifier_object ( FragmentedSerializer )
146
- expected = { id : 'special_id' , type : expected_model_type }
168
+ actual = actual_resource_identifier_object ( FragmentedSerializer , @model )
169
+ expected = { id : 'special_id' , type : expected_model_type ( @model ) }
147
170
assert_equal actual , expected
148
171
end
149
172
150
173
private
151
174
152
- def actual_resource_identifier_object ( serializer_class )
153
- serializer = serializer_class . new ( @ model)
175
+ def actual_resource_identifier_object ( serializer_class , model )
176
+ serializer = serializer_class . new ( model )
154
177
resource_identifier = ResourceIdentifier . new ( serializer , nil )
155
178
resource_identifier . as_json
156
179
end
157
180
158
- def expected_model_type
159
- inflection = ActiveModelSerializers . config . jsonapi_resource_type
160
- @model . class . model_name . send ( inflection )
181
+ def expected_model_type ( model , inflection = ActiveModelSerializers . config . jsonapi_resource_type )
182
+ with_jsonapi_inflection inflection do
183
+ model . class . model_name . send ( inflection )
184
+ end
161
185
end
162
186
163
- def expected_model_id
164
- @ model. id . to_s
187
+ def expected_model_id ( model )
188
+ model . id . to_s
165
189
end
166
190
end
167
191
end
0 commit comments