Skip to content

Commit bc2fead

Browse files
authored
Merge pull request #389 from tilo/create-bang-methods
adding create!, update_attributes!, update! methods
2 parents bd759fb + 8a6626e commit bc2fead

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- [#389](https://github.com/JsonApiClient/json_api_client/pull/389)-adding create!, update_attributes!, update! methods
6+
57
## 1.19.0
68
- [#382](https://github.com/JsonApiClient/json_api_client/pull/382) - Add extra error classes to hande server errors.
79

lib/json_api_client/errors.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,16 @@ def initialize(code, uri)
103103
super nil, msg
104104
end
105105
end
106+
107+
class RecordNotSaved < ServerError
108+
attr_reader :record
109+
110+
def initialize(message = nil, record = nil)
111+
@record = record
112+
end
113+
def message
114+
"Record not saved"
115+
end
116+
end
106117
end
107118
end

lib/json_api_client/resource.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ def create(attributes = {})
172172
end
173173
end
174174

175+
def create!(attributes = {})
176+
new(attributes).tap do |resource|
177+
raise(Errors::RecordNotSaved.new("Failed to save the record", resource)) unless resource.save
178+
end
179+
end
180+
175181
# Within the given block, add these headers to all requests made by
176182
# the resource class
177183
#
@@ -376,6 +382,11 @@ def update_attributes(attrs = {})
376382
save
377383
end
378384

385+
def update_attributes!(attrs = {})
386+
self.attributes = attrs
387+
save ? true : raise(Errors::RecordNotSaved.new("Failed to update the record", self))
388+
end
389+
379390
# Alias to update_attributes
380391
#
381392
# @param attrs [Hash] Attributes to update
@@ -384,6 +395,10 @@ def update(attrs = {})
384395
update_attributes(attrs)
385396
end
386397

398+
def update!(attrs = {})
399+
update_attributes!(attrs)
400+
end
401+
387402
# Mark the record as persisted
388403
def mark_as_persisted!
389404
@persisted = true

test/unit/creation_test.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ def test_can_create_with_class_method
6666
assert_equal "Rails is Omakase", article.title
6767
end
6868

69+
def test_failed_create!
70+
stub_request(:post, "http://example.com/users")
71+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
72+
errors: [
73+
{
74+
status: "400",
75+
title: "Error"
76+
}
77+
]
78+
}.to_json)
79+
80+
exception = assert_raises JsonApiClient::Errors::RecordNotSaved do
81+
User.create!(name: 'Hans')
82+
end
83+
assert_equal "Record not saved", exception.message
84+
end
85+
6986
def test_changed_attributes_empty_after_create_with_class_method
7087
stub_simple_creation
7188
article = Article.create({
@@ -206,7 +223,65 @@ def test_can_create_with_new_record_with_relationships_and_save
206223
assert article.persisted?
207224
assert_equal article.comments.length, 1
208225
assert_equal "1", article.id
226+
end
209227

228+
def test_can_create_with_new_record_with_associated_relationships_and_save
229+
stub_request(:post, "http://example.com/articles")
230+
.with(headers: {content_type: "application/vnd.api+json", accept: "application/vnd.api+json"}, body: {
231+
data: {
232+
type: "articles",
233+
relationships: {
234+
author: {
235+
data: {
236+
id: 1,
237+
type: "authors"
238+
}
239+
}
240+
},
241+
attributes: {
242+
title: "Rails is Omakase"
243+
}
244+
}
245+
}.to_json)
246+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
247+
data: {
248+
type: "articles",
249+
id: "1",
250+
attributes: {
251+
title: "Rails is Omakase"
252+
},
253+
relationships: {
254+
author: {
255+
data: [
256+
{
257+
id: "1",
258+
type: "comments"
259+
}
260+
]
261+
}
262+
}
263+
},
264+
included: [
265+
{
266+
id: "1",
267+
type: "authors",
268+
}
269+
]
270+
}.to_json)
271+
272+
author_hash = {
273+
author: {
274+
data: {
275+
id: 1,
276+
type: 'authors'
277+
}
278+
}
279+
}
280+
article = Article.new({title: "Rails is Omakase", "relationships" => author_hash})
281+
282+
assert article.save
283+
assert article.persisted?
284+
assert_equal "1", article.id
210285
end
211286

212287
def test_correct_create_with_nil_attribute_value

test/unit/updating_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,36 @@ def stub_simple_fetch
3636
}.to_json)
3737
end
3838

39+
def test_failed_update!
40+
stub_simple_fetch
41+
articles = Article.find(1)
42+
article = articles.first
43+
44+
stub_request(:patch, "http://example.com/articles/1")
45+
.with(headers: {content_type: "application/vnd.api+json", accept: "application/vnd.api+json"}, body: {
46+
data: {
47+
id: "1",
48+
type: "articles",
49+
attributes: {
50+
title: "Modified title",
51+
}
52+
}
53+
}.to_json)
54+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
55+
errors: [
56+
{
57+
status: "400",
58+
title: "Error"
59+
}
60+
]
61+
}.to_json)
62+
63+
exception = assert_raises JsonApiClient::Errors::RecordNotSaved do
64+
article.update!(title: 'Modified title')
65+
end
66+
assert_equal "Record not saved", exception.message
67+
end
68+
3969
def test_can_update_found_record
4070
stub_simple_fetch
4171
articles = Article.find(1)

0 commit comments

Comments
 (0)