-
Notifications
You must be signed in to change notification settings - Fork 530
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
fix: more flexible polymorphic types lookup #1424
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,14 +90,37 @@ def self.polymorphic_types(name) | |
@poly_hash ||= {}.tap do |hash| | ||
ObjectSpace.each_object do |klass| | ||
next unless Module === klass | ||
if ActiveRecord::Base > klass | ||
is_active_record_inspectable = ActiveRecord::Base > klass | ||
is_active_record_inspectable &&= klass.respond_to?(:reflect_on_all_associations, true) | ||
is_active_record_inspectable &&= format_polymorphic_klass_type(klass).present? | ||
if is_active_record_inspectable | ||
klass.reflect_on_all_associations(:has_many).select { |r| r.options[:as] }.each do |reflection| | ||
(hash[reflection.options[:as]] ||= []) << klass.name.underscore | ||
(hash[reflection.options[:as]] ||= []) << format_polymorphic_klass_type(klass).underscore | ||
end | ||
end | ||
end | ||
end | ||
@poly_hash[name.to_sym] | ||
@poly_hash.fetch(name.to_sym) do | ||
klass = name.classify.safe_constantize | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fallback strategy I'm trying |
||
if klass.nil? | ||
warn "[POLYMORPHIC TYPE NOT FOUND] No polymorphic types found for #{name}" | ||
else | ||
polymorphic_type = format_polymorphic_klass_type(klass) | ||
warn "[POLYMORPHIC TYPE] Found polymorphic types through reflection for #{name}: #{polymorphic_type}" | ||
@poly_hash[name.to_sym] = [polymorphic_type] | ||
end | ||
end | ||
end | ||
|
||
def self.format_polymorphic_klass_type(klass) | ||
klass.name || | ||
begin | ||
klass.model_name.name | ||
rescue ArgumentError => ex | ||
# klass.base_class may be nil | ||
warn "[POLYMORPHIC TYPE] #{__callee__} #{klass} #{ex.inspect}" | ||
nil | ||
end | ||
end | ||
|
||
def resource_types | ||
|
@@ -203,7 +226,7 @@ def polymorphic_type | |
def setup_implicit_relationships_for_polymorphic_types(exclude_linkage_data: true) | ||
types = self.class.polymorphic_types(_relation_name) | ||
unless types.present? | ||
warn "No polymorphic types found for #{parent_resource.name} #{_relation_name}" | ||
warn "[POLYMORPHIC TYPE] No polymorphic types found for #{parent_resource.name} #{_relation_name}" | ||
return | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1020,17 +1020,7 @@ def polymorphic(polymorphic = true) | |
end | ||
|
||
def _polymorphic_types | ||
@poly_hash ||= {}.tap do |hash| | ||
ObjectSpace.each_object do |klass| | ||
next unless Module === klass | ||
if klass < ActiveRecord::Base | ||
klass.reflect_on_all_associations(:has_many).select{|r| r.options[:as] }.each do |reflection| | ||
(hash[reflection.options[:as]] ||= []) << klass.name.underscore | ||
end | ||
end | ||
end | ||
end | ||
@poly_hash[_polymorphic_name.to_sym] | ||
JSONAPI::Relationship.polymorphic_types(_polymorphic_name.to_sym) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lgebhardt is a refactor included in the PR as a separate commit |
||
end | ||
|
||
def _polymorphic_resource_klasses | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
require File.expand_path('../../../test_helper', __FILE__) | ||
|
||
# copied from https://github.com/cerebris/jsonapi-resources/blob/e60dc7dd2c7fdc85834163a7e706a10a8940a62b/test/integration/requests/polymorphic_test.rb | ||
# https://github.com/cerebris/jsonapi-resources/compare/bf4_fix_polymorphic_relations_lookup?expand=1 | ||
class PolymorphicTest < ActionDispatch::IntegrationTest | ||
|
||
def json_api_headers | ||
{'Accept' => JSONAPI::MEDIA_TYPE, 'CONTENT_TYPE' => JSONAPI::MEDIA_TYPE} | ||
end | ||
|
||
def test_find_party_via_contact_medium | ||
individual = Individual.create(name: 'test') | ||
contact_medium = ContactMedium.create(party: individual, name: 'test contact medium') | ||
fetched_party = contact_medium.party | ||
assert_same individual, fetched_party, "Expect an individual to have been found via contact medium model's relationship 'party'" | ||
end | ||
|
||
def test_get_individual | ||
individual = Individual.create(name: 'test') | ||
ContactMedium.create(party: individual, name: 'test contact medium') | ||
get "/individuals/#{individual.id}" | ||
assert_jsonapi_response 200 | ||
end | ||
|
||
def test_get_party_via_contact_medium | ||
individual = Individual.create(name: 'test') | ||
contact_medium = ContactMedium.create(party: individual, name: 'test contact medium') | ||
get "/contact_media/#{contact_medium.id}/party" | ||
assert_jsonapi_response 200, "Expect an individual to have been found via contact medium resource's relationship 'party'" | ||
end | ||
end | ||
|
||
# copied from https://github.com/cerebris/jsonapi-resources/pull/1349/files | ||
# require File.expand_path('../test_helper', __FILE__) | ||
# | ||
# Replace this with the code necessary to make your test fail. | ||
# class BugTest < Minitest::Test | ||
# include Rack::Test::Methods | ||
# | ||
# def json_api_headers | ||
# {'Accept' => JSONAPI::MEDIA_TYPE, 'CONTENT_TYPE' => JSONAPI::MEDIA_TYPE} | ||
# end | ||
# | ||
# def teardown | ||
# Individual.delete_all | ||
# ContactMedium.delete_all | ||
# end | ||
# | ||
# def test_find_party_via_contact_medium | ||
# individual = Individual.create(name: 'test') | ||
# contact_medium = ContactMedium.create(party: individual, name: 'test contact medium') | ||
# fetched_party = contact_medium.party | ||
# assert_same individual, fetched_party, "Expect an individual to have been found via contact medium model's relationship 'party'" | ||
# end | ||
# | ||
# def test_get_individual | ||
# individual = Individual.create(name: 'test') | ||
# ContactMedium.create(party: individual, name: 'test contact medium') | ||
# get "/individuals/#{individual.id}" | ||
# assert_last_response_status 200 | ||
# end | ||
# | ||
# def test_get_party_via_contact_medium | ||
# individual = Individual.create(name: 'test') | ||
# contact_medium = ContactMedium.create(party: individual, name: 'test contact medium') | ||
# get "/contact_media/#{contact_medium.id}/party" | ||
# assert_last_response_status 200, "Expect an individual to have been found via contact medium resource's relationship 'party'" | ||
# end | ||
# | ||
# private | ||
# | ||
# def assert_last_response_status(status, failure_reason=nil) | ||
# if last_response.status != status | ||
# json_response = JSON.parse(last_response.body) rescue last_response.body | ||
# pp json_response | ||
# end | ||
# assert_equal status, last_response.status, failure_reason | ||
# end | ||
# | ||
# def app | ||
# Rails.application | ||
# end | ||
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lgebhardt I'll add a test for this. I'm testing against our app on this branch right now and looks good