Add support for polymorphic association in a resource #37
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The dummy app uses resources: employee, product and pictures. The pictures have an imageable relation which can be either an employee or product resource. There is no model prototype for the imageable resources only a service.
The solution to support polymorphic associations did require an imageable service, but this service only utilizes the findRelated method which uses the URLs provided in the relationship objects of the resource.
See the changed files list. Only a minor change in the adapter#findRelated method was needed for the polymorphic association support of a related resource. However the implementation does need a service that represents the polymorphic relation, in this case imageable is the service that supports types for employee or product.
Some links for the rails api:
Below is a payload for a resource that uses polymorphic associations.
There are 3 types: Employee, Product, Picture. The Picture belongs to an Imageable resource (Employees and Products both use a has_many relation for Pictures).
In the payload below the request is for http://localhost:3000/api/v1/pictures?include=imageable so the 1st 5 of 6 pictures are returned and included are the imaginable types a few products and an employee.
The links under each picture resource's relationships objects have the url which should return the imageable resource regardless of type. E.g. http://localhost:3000/api/v1/pictures/5/imageable may be an Employee and http://localhost:3000/api/v1/pictures/4/imageable may be a Product.