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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ gem 'addressable', '~> 2.2'
gem "codeclimate-test-reporter", group: :test, require: nil

group :development, :test do
gem 'byebug', platforms: [:mri_20, :mri_21, :mri_22]
gem 'byebug', '~> 10.0', platforms: [:mri_20, :mri_21, :mri_22]
end
7 changes: 7 additions & 0 deletions lib/json_api_client/associations/base_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ def data(url)
def from_result_set(result_set)
result_set.to_a
end

def load_records(data)
data.map do |d|
record_class = Utils.compute_type(klass, d["type"].classify)
record_class.load id: d["id"]
end
end
end
end
end
7 changes: 6 additions & 1 deletion lib/json_api_client/associations/has_one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ class Association < BaseAssociation
def from_result_set(result_set)
result_set.first
end

def load_records(data)
record_class = Utils.compute_type(klass, data["type"].classify)
Copy link

@code-bunny code-bunny Mar 26, 2019

Choose a reason for hiding this comment

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

After testing I have found there is a bug here, when the payload is in dash case running classify on the type we get unexpected results. "skill-level". classify will return Skill-level. So what we need to do here is data["type"].underscore.classify

Choose a reason for hiding this comment

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

If fixed this locally with

  JsonApiClient::Associations::HasOne::Association.class_eval do
    def load_records(data)
      record_class = JsonApiClient::Utils.compute_type(klass, data["type"].underscore.classify)
      record_class.load id: data["id"]
    end
  end

record_class.load id: data["id"]
end
end
end
end
end
end
13 changes: 12 additions & 1 deletion lib/json_api_client/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,11 @@ def included_data_for(name, relationship_definition)
def relationship_data_for(name, relationship_definition)
# look in included data
if relationship_definition.key?("data")
return included_data_for(name, relationship_definition)
if relationships.attribute_changed?(name)
return relation_objects_for(name, relationship_definition)
else
return included_data_for(name, relationship_definition)
end
end

url = relationship_definition["links"]["related"]
Expand All @@ -548,6 +552,13 @@ def relationship_data_for(name, relationship_definition)
nil
end

def relation_objects_for(name, relationship_definition)
data = relationship_definition["data"]
assoc = association_for(name)
return if data.nil? || assoc.nil?
assoc.load_records(data)
end

def method_missing(method, *args)
relationship_definition = relationship_definition_for(method)

Expand Down
46 changes: 46 additions & 0 deletions test/unit/creation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ def after_create_method
class Author < TestResource
end

class User < TestResource
has_one :skill_level

properties :first_name, type: :string
end

class SkillLevel < TestResource
property :title, type: :string
end

def stub_simple_creation
stub_request(:post, "http://example.com/articles")
.with(headers: {content_type: "application/vnd.api+json", accept: "application/vnd.api+json"}, body: {
Expand Down Expand Up @@ -341,4 +351,40 @@ def test_create_with_custom_type
assert_equal '1', file.id
end

def test_access_loaded_relationship_instance
stub_request(:get, 'http://example.com/skill_levels/1')
.to_return(headers: {content_type: 'application/vnd.api+json'}, body: {
data: {
type: 'skill_levels',
id: '1',
attributes: {
title: 'newbie'
}
}
}.to_json)

stub_request(:get, 'http://example.com/skill_levels/2')
.to_return(headers: {content_type: 'application/vnd.api+json'}, body: {
data: {
type: 'skill_levels',
id: '2',
attributes: {
title: 'pro'
}
}
}.to_json)

skill_level = SkillLevel.find(1).first
user = User.new(first_name: 'Joe', relationships: { skill_level: skill_level })

assert_equal ({'data'=>{'type'=>'skill_levels', 'id'=>'1'}}), user.relationships.skill_level
assert_kind_of SkillLevel, user.skill_level
assert_equal '1', user.skill_level.id
# test that object is cached and not recreated each time
assert_equal user.skill_level.object_id, user.skill_level.object_id

user.skill_level = SkillLevel.find(2).first
assert_equal '2', user.skill_level.id
end

end