File tree Expand file tree Collapse file tree 3 files changed +84
-0
lines changed Expand file tree Collapse file tree 3 files changed +84
-0
lines changed Original file line number Diff line number Diff line change @@ -103,5 +103,16 @@ def initialize(code, uri)
103
103
super nil , msg
104
104
end
105
105
end
106
+
107
+ class RecordNotSaved < ServerError
108
+ attr_reader :record
109
+
110
+ def initialize ( record = nil )
111
+ @record = record
112
+ msg = 'Record not saved'
113
+
114
+ super nil , msg
115
+ end
116
+ end
106
117
end
107
118
end
Original file line number Diff line number Diff line change @@ -172,6 +172,12 @@ def create(attributes = {})
172
172
end
173
173
end
174
174
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
+
175
181
# Within the given block, add these headers to all requests made by
176
182
# the resource class
177
183
#
@@ -376,6 +382,11 @@ def update_attributes(attrs = {})
376
382
save
377
383
end
378
384
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
+
379
390
# Alias to update_attributes
380
391
#
381
392
# @param attrs [Hash] Attributes to update
@@ -384,6 +395,10 @@ def update(attrs = {})
384
395
update_attributes ( attrs )
385
396
end
386
397
398
+ def update! ( attrs = { } )
399
+ update_attributes! ( attrs )
400
+ end
401
+
387
402
# Mark the record as persisted
388
403
def mark_as_persisted!
389
404
@persisted = true
Original file line number Diff line number Diff line change @@ -206,7 +206,65 @@ def test_can_create_with_new_record_with_relationships_and_save
206
206
assert article . persisted?
207
207
assert_equal article . comments . length , 1
208
208
assert_equal "1" , article . id
209
+ end
210
+
211
+ def test_can_create_with_new_record_with_associated_relationships_and_save
212
+ stub_request ( :post , "http://example.com/articles" )
213
+ . with ( headers : { content_type : "application/vnd.api+json" , accept : "application/vnd.api+json" } , body : {
214
+ data : {
215
+ type : "articles" ,
216
+ relationships : {
217
+ author : {
218
+ data : {
219
+ id : 1 ,
220
+ type : "authors"
221
+ }
222
+ }
223
+ } ,
224
+ attributes : {
225
+ title : "Rails is Omakase"
226
+ }
227
+ }
228
+ } . to_json )
229
+ . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
230
+ data : {
231
+ type : "articles" ,
232
+ id : "1" ,
233
+ attributes : {
234
+ title : "Rails is Omakase"
235
+ } ,
236
+ relationships : {
237
+ author : {
238
+ data : [
239
+ {
240
+ id : "1" ,
241
+ type : "comments"
242
+ }
243
+ ]
244
+ }
245
+ }
246
+ } ,
247
+ included : [
248
+ {
249
+ id : "1" ,
250
+ type : "authors" ,
251
+ }
252
+ ]
253
+ } . to_json )
209
254
255
+ author_hash = {
256
+ author : {
257
+ data : {
258
+ id : 1 ,
259
+ type : 'authors'
260
+ }
261
+ }
262
+ }
263
+ article = Article . new ( { title : "Rails is Omakase" , "relationships" => author_hash } )
264
+
265
+ assert article . save
266
+ assert article . persisted?
267
+ assert_equal "1" , article . id
210
268
end
211
269
212
270
def test_correct_create_with_nil_attribute_value
You can’t perform that action at this time.
0 commit comments