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)
103103 super nil , msg
104104 end
105105 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
106117 end
107118end
Original file line number Diff line number Diff 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
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
206206 assert article . persisted?
207207 assert_equal article . comments . length , 1
208208 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 )
209254
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
210268 end
211269
212270 def test_correct_create_with_nil_attribute_value
You can’t perform that action at this time.
0 commit comments