Skip to content

feat: Add the ability to override getter for JsonApiClient::Resource id #378

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

Merged
merged 8 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
- [378](https://github.com/JsonApiClient/json_api_client/pull/378) - Add the ability to create a subclass of JsonApiClient::Resource to have a modified id method

## 1.21.0
- [#395](https://github.com/JsonApiClient/json_api_client/pull/395) - relaxing faraday dependency to anything less than 2.0
Expand Down
2 changes: 1 addition & 1 deletion lib/json_api_client/included_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def initialize(result_set, data)
end

grouped_included_set.each do |type, resources|
grouped_included_set[type] = resources.index_by(&:id)
grouped_included_set[type] = resources.index_by { |resource| resource.attributes[:id] }
end

@data = grouped_included_set
Expand Down
181 changes: 181 additions & 0 deletions test/unit/integer_id_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# frozen_string_literal: true

require 'test_helper'

class BaseResource < JsonApiClient::Resource
self.site = 'http://example.com/'
def id
attributes[:id].to_i if attributes[:id].present?
end
end

class Actor < BaseResource
has_many :movies
end

class Movie < BaseResource
belongs_to :actor, shallow_path: true
has_one :director, shallow_path: true
end

class Director < BaseResource
has_many :movies
end

class IntegerIdTestAssociationTest < MiniTest::Test

def test_included_document_test_id_from_method_as_integer
stub_request(:get, 'http://example.com/movies/1?include=actor')
.to_return(headers: { content_type: 'application/vnd.api+json',
accept: 'application/vnd.api+json' },
body: {
data: {
id: '1',
type: 'movie',
attributes: {
actor_id: 1,
director_id: 1,
created_at: '2021-04-20T17:27:06-07:00',
updated_at: '2021-04-20T17:27:07-07:00'
},
relationships: {
actor: {
data: {
id: '1',
type: 'actor'
}
},
director: {
data: {
id: '1',
type: 'director'
}
}
}
},
included: [
{
id: '1',
type: 'actor',
attributes: {
name: 'Keanu',
updated_at: '2021-04-22T13:50:19-07:00',
created_at: '2021-04-19T16:20:13-07:00'
}
}
]
}.to_json)
movie = Movie.includes(:actor).find(1).last
assert_equal(Integer, movie.id.class)
assert_equal(1, movie.id)
assert_equal(String, movie.attributes[:id].class)
assert_equal('1', movie.attributes[:id])
assert_equal(Actor, movie.actor.class)
assert_equal(Integer, movie.actor.id.class)
assert_equal(1, movie.actor.id)
assert_equal('1', movie.actor.attributes[:id])
assert_equal(movie.actor_id, movie.actor.id)
end

def test_not_included_data_document
stub_request(:get, 'http://example.com/movies/1')
.to_return(headers: { content_type: 'application/vnd.api+json',
accept: 'application/vnd.api+json' },
body: {
data: {
id: '1',
type: 'movie',
attributes: {
actor_id: 1,
created_at: '2021-04-20T17:27:06-07:00',
updated_at: '2021-04-20T17:27:07-07:00'
},
relationships: {
actor: {
data: {
id: '1',
type: 'actor'
},
director: {
data: {
id: '1',
type: 'director'
}
}
}
}
}
}.to_json)
movie = Movie.find(1).last
assert_equal(Integer, movie.id.class)
assert_equal(1, movie.id)
assert_equal(String, movie.attributes[:id].class)
assert_equal('1', movie.attributes[:id])
assert_nil(movie.actor)
end

def test_not_included_data_document_with_relationships_links
stub_request(:get, 'http://example.com/movies/1')
.to_return(headers: { content_type: 'application/vnd.api+json',
accept: 'application/vnd.api+json' },
body: {
data: {
id: '1',
type: 'movie',
attributes: {
actor_id: 1,
created_at: '2021-04-20T17:27:06-07:00',
updated_at: '2021-04-20T17:27:07-07:00'
},
relationships: {
actor: {
links: {
self: '/movies/1',
related: '/actors/1'
}
},
director: {
links: {
self: '/movies/1',
related: '/directors/1'
}
}
}
}
}.to_json)
stub_request(:get, 'http://example.com/directors/1')
.to_return(headers: { content_type: 'application/vnd.api+json',
accept: 'application/vnd.api+json' },
body: {
data: {
id: '1',
type: 'movie',
attributes: {
actor_id: 1,
created_at: '2021-04-20T17:27:06-07:00',
updated_at: '2021-04-20T17:27:07-07:00'
},
relationships: {
actor: {
links: {
self: '/movies/1',
related: '/actors/1'
}
},
director: {
links: {
self: '/movies/1',
related: '/directors/1'
}
}
}
}
}.to_json)
movie = Movie.find(1).last
assert_equal(Integer, movie.id.class)
assert_equal(1, movie.id)
assert_equal(String, movie.attributes[:id].class)
assert_equal('1', movie.attributes[:id])
assert_equal(Director, movie.director.class)
end
end