Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/json_api_client/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ def _build_connection(rebuild = false)
def initialize(params = {})
params = params.symbolize_keys
@persisted = nil
@destroyed = nil
self.links = self.class.linker.new(params.delete(:links) || {})
self.relationships = self.class.relationship_linker.new(self.class, params.delete(:relationships) || {})
self.attributes = self.class.default_attributes.merge(params)
Expand Down Expand Up @@ -355,14 +356,26 @@ def mark_as_persisted!
#
# @return [Boolean]
def persisted?
!!@persisted && has_attribute?(self.class.primary_key)
!!@persisted && !destroyed? && has_attribute?(self.class.primary_key)
end

# Mark the record as destroyed
def mark_as_destroyed!
@destroyed = true
end

# Whether or not this record has been destroyed to the database previously
#
# @return [Boolean]
def destroyed?
!!@destroyed
end

# Returns true if this is a new record (never persisted to the database)
#
# @return [Boolean]
def new_record?
!persisted?
!persisted? && !destroyed?
end

# When we represent this resource as a relationship, we do so with id & type
Expand Down Expand Up @@ -449,8 +462,7 @@ def destroy
fill_errors
false
else
self.attributes.clear
self.relationships.attributes.clear
mark_as_destroyed!
self.relationships.last_result_set = nil
_clear_cached_relationships
true
Expand Down
2 changes: 2 additions & 0 deletions test/unit/creation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ def test_can_create_with_new_record_and_save
title: "Rails is Omakase"
})

assert article.new_record?
assert article.save
assert article.persisted?
assert_equal(false, article.new_record?)
assert_equal "1", article.id
end

Expand Down
19 changes: 17 additions & 2 deletions test/unit/destroying_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,29 @@ class CallbackTest < TestResource
end

def test_destroy
stub_request(:delete, "http://example.com/users/6")
stub_request(:get, "http://example.com/users/1")
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: [
{id: 1, attributes: {name: "Jeff Ching", email_address: "ching.jeff@gmail.com"}}
]
}.to_json)

users = User.find(1)
user = users.first
assert(user.persisted?)
assert_equal(false, user.new_record?)
assert_equal(false, user.destroyed?)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also do refute user.new_record?, but it really doesn't matter

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can change it if you'd like; we were just trying to follow the style of the existing tests. Do you want us to update?

Copy link

@workgena workgena Oct 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi 👋, recently I played with Rubocop. It suggests to use ActiveSupport method assert_not

assert_not user.new_record?

PS: more than one way of doing the same thing 😏 Its up to commits author, what to use.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hashrocketeer, nah. Thanks again for the contribution! I'll push out a new version with your code by eod.


stub_request(:delete, "http://example.com/users/1")
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: []
}.to_json)

user = User.new(id: 6)
assert(user.destroy, "successful deletion should return truish value")
assert_equal(false, user.persisted?)
assert_equal(false, user.new_record?)
assert(user.destroyed?)
assert_equal(1, user.id)
end

def test_destroy_no_content
Expand Down