Skip to content

Commit 5bc5f2c

Browse files
authored
Merge pull request #307 from senid231/290-new-model-allow-symbolized-relationship-key
#290 symbolize params keys on model initialize
2 parents 010414c + c098c66 commit 5bc5f2c

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

lib/json_api_client/resource.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,10 @@ def _build_connection(rebuild = false)
309309
#
310310
# @param params [Hash] Attributes, links, and relationships
311311
def initialize(params = {})
312+
params = params.symbolize_keys
312313
@persisted = nil
313-
self.links = self.class.linker.new(params.delete("links") || {})
314-
self.relationships = self.class.relationship_linker.new(self.class, params.delete("relationships") || {})
314+
self.links = self.class.linker.new(params.delete(:links) || {})
315+
self.relationships = self.class.relationship_linker.new(self.class, params.delete(:relationships) || {})
315316
self.attributes = self.class.default_attributes.merge(params)
316317

317318
self.class.schema.each_property do |property|

test/unit/creation_test.rb

+37
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,41 @@ def test_callbacks_on_update
271271
assert_equal 100, callback_test.bar
272272
end
273273

274+
def test_create_with_relationships_in_payload
275+
stub_request(:post, 'http://example.com/articles')
276+
.with(headers: {content_type: 'application/vnd.api+json', accept: 'application/vnd.api+json'}, body: {
277+
data: {
278+
type: 'articles',
279+
attributes: {
280+
title: 'Rails is Omakase'
281+
},
282+
relationships: {
283+
comments: {
284+
data: [
285+
{
286+
id: '2',
287+
type: 'comments'
288+
}
289+
]
290+
}
291+
}
292+
}
293+
}.to_json)
294+
.to_return(headers: {content_type: 'application/vnd.api+json'}, body: {
295+
data: {
296+
type: 'articles',
297+
id: '1',
298+
attributes: {
299+
title: 'Rails is Omakase'
300+
}
301+
}
302+
}.to_json)
303+
304+
article = Article.new(title: 'Rails is Omakase', relationships: {comments: [Comment.new(id: 2)]})
305+
306+
assert article.save
307+
assert article.persisted?
308+
assert_equal "1", article.id
309+
end
310+
274311
end

0 commit comments

Comments
 (0)