Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed the test for has_one without included data #244

Closed
Closed
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
2 changes: 1 addition & 1 deletion lib/json_api_client/associations/has_one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ def from_result_set(result_set)
end
end
end
end
end
27 changes: 12 additions & 15 deletions lib/json_api_client/included_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,26 @@ def initialize(result_set, data)
end

def data_for(method_name, definition)
# this method only returns an array. It's up to the caller to decide if it's going to return
# just the first element if it's a has_one relationship.
# If data is defined, pull the record from the included data
return nil unless data = definition["data"]

if data.is_a?(Array)
# has_many link
data.map do |link_def|
record_for(link_def)
defined_data = definition["data"]
return nil unless defined_data
[defined_data].flatten.map do |link_def|
# should return a resource record of some type for this linked document
# even if there's no matching record included.
if data[link_def["type"]]
data[link_def["type"]][link_def["id"]]
else
# if there's no matching record in included then go and get it given the data
link_def["type"].underscore.classify.constantize.find(link_def["id"]).first
end
else
# has_one link
record_for(data)
end
end

def has_link?(name)
data.has_key?(name.to_s)
end

private

# should return a resource record of some type for this linked document
def record_for(link_def)
data[link_def["type"]][link_def["id"]]
end
end
end
15 changes: 11 additions & 4 deletions lib/json_api_client/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,17 @@ def method_missing(method, *args)
return nil unless relationship_definitions = relationships[method]

# look in included data
if relationship_definitions.key?("data")
return last_result_set.included.data_for(method, relationship_definitions)
end

if relationship_definitions.key?("data")
# included.data_for returns an array, if the association is a has_one, then pick the first, otherise return the whole array
if association.is_a?(JsonApiClient::Associations::HasOne::Association)
return last_result_set.included.data_for(method, relationship_definitions).try(:first)
else
return last_result_set.included.data_for(method, relationship_definitions)
end
end

# I'm very puzzled by this as we have association = association_for(method) in the first line of this method
# is it intened to be 'if association == association_for(method)'
if association = association_for(method)
# look for a defined relationship url
if relationship_definitions["links"] && url = relationship_definitions["links"]["related"]
Expand Down
16 changes: 10 additions & 6 deletions test/unit/association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Property < TestResource

class AssociationTest < MiniTest::Test

def test_load_has_one
def test_load_has_one_without_include
stub_request(:get, "http://example.com/properties/1")
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: [
Expand All @@ -48,17 +48,20 @@ def test_load_has_one
}
}
}
],
included: [
]

}.to_json)
stub_request(:get, "http://example.com/owners/1")
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: [
{
id: 1,
type: 'owner',
type: "owner",
attributes: {
name: 'Jeff Ching'
name: "Jeff Ching"
}
}
]

}.to_json)
property = Property.find(1).first
assert_equal(Owner, property.owner.class)
Expand Down Expand Up @@ -96,6 +99,7 @@ def test_load_has_one_with_include

}.to_json)
property = Property.includes(:owner).find(1).first
assert_equal(Owner, property.owner.class)
assert_equal("Jeff Ching", property.owner.name)
end

Expand Down