Skip to content

Commit 147ec71

Browse files
authored
Merge pull request #314 from hashrocketeer/mimic_destroy
Mimics ActiveRecord behavior when destroying resource
2 parents 3460162 + 08b91ee commit 147ec71

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

lib/json_api_client/resource.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ def _build_connection(rebuild = false)
315315
def initialize(params = {})
316316
params = params.symbolize_keys
317317
@persisted = nil
318+
@destroyed = nil
318319
self.links = self.class.linker.new(params.delete(:links) || {})
319320
self.relationships = self.class.relationship_linker.new(self.class, params.delete(:relationships) || {})
320321
self.attributes = self.class.default_attributes.merge(params)
@@ -355,14 +356,26 @@ def mark_as_persisted!
355356
#
356357
# @return [Boolean]
357358
def persisted?
358-
!!@persisted && has_attribute?(self.class.primary_key)
359+
!!@persisted && !destroyed? && has_attribute?(self.class.primary_key)
360+
end
361+
362+
# Mark the record as destroyed
363+
def mark_as_destroyed!
364+
@destroyed = true
365+
end
366+
367+
# Whether or not this record has been destroyed to the database previously
368+
#
369+
# @return [Boolean]
370+
def destroyed?
371+
!!@destroyed
359372
end
360373

361374
# Returns true if this is a new record (never persisted to the database)
362375
#
363376
# @return [Boolean]
364377
def new_record?
365-
!persisted?
378+
!persisted? && !destroyed?
366379
end
367380

368381
# When we represent this resource as a relationship, we do so with id & type
@@ -449,8 +462,7 @@ def destroy
449462
fill_errors
450463
false
451464
else
452-
self.attributes.clear
453-
self.relationships.attributes.clear
465+
mark_as_destroyed!
454466
self.relationships.last_result_set = nil
455467
_clear_cached_relationships
456468
true

test/unit/creation_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ def test_can_create_with_new_record_and_save
6969
title: "Rails is Omakase"
7070
})
7171

72+
assert article.new_record?
7273
assert article.save
7374
assert article.persisted?
75+
assert_equal(false, article.new_record?)
7476
assert_equal "1", article.id
7577
end
7678

test/unit/destroying_test.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,29 @@ class CallbackTest < TestResource
1111
end
1212

1313
def test_destroy
14-
stub_request(:delete, "http://example.com/users/6")
14+
stub_request(:get, "http://example.com/users/1")
15+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
16+
data: [
17+
{id: 1, attributes: {name: "Jeff Ching", email_address: "ching.jeff@gmail.com"}}
18+
]
19+
}.to_json)
20+
21+
users = User.find(1)
22+
user = users.first
23+
assert(user.persisted?)
24+
assert_equal(false, user.new_record?)
25+
assert_equal(false, user.destroyed?)
26+
27+
stub_request(:delete, "http://example.com/users/1")
1528
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
1629
data: []
1730
}.to_json)
1831

19-
user = User.new(id: 6)
2032
assert(user.destroy, "successful deletion should return truish value")
2133
assert_equal(false, user.persisted?)
34+
assert_equal(false, user.new_record?)
35+
assert(user.destroyed?)
36+
assert_equal(1, user.id)
2237
end
2338

2439
def test_destroy_no_content

0 commit comments

Comments
 (0)